diff --git a/tests/compatibility.php b/tests/compatibility.php index ee617ab2ed..f7fd3f29d8 100644 --- a/tests/compatibility.php +++ b/tests/compatibility.php @@ -11,6 +11,22 @@ namespace PHPUnit\Framework\Constraint { namespace PHPUnit\Framework { if (!class_exists('PHPUnit\Framework\TestCase') && class_exists('PHPUnit_Framework_TestCase')) { - abstract class TestCase extends \PHPUnit_Framework_TestCase {} + abstract class TestCase extends \PHPUnit_Framework_TestCase { + /** + * @param string $exception + */ + public function expectException($exception) + { + $this->setExpectedException($exception); + } + + /** + * @param string $message + */ + public function expectExceptionMessage($message) + { + $this->setExpectedException($this->getExpectedException(), $message); + } + } } } diff --git a/tests/framework/BaseYiiTest.php b/tests/framework/BaseYiiTest.php index d045dff8bd..38df4fb6af 100644 --- a/tests/framework/BaseYiiTest.php +++ b/tests/framework/BaseYiiTest.php @@ -113,7 +113,9 @@ class BaseYiiTest extends TestCase */ public function testLog() { - $logger = $this->getMock('yii\\log\\Logger', ['log']); + $logger = $this->getMockBuilder('yii\\log\\Logger') + ->setMethods(['log']) + ->getMock(); BaseYii::setLogger($logger); $logger->expects($this->exactly(6)) diff --git a/tests/framework/base/BehaviorTest.php b/tests/framework/base/BehaviorTest.php index c5ec181b13..a54f7131ae 100644 --- a/tests/framework/base/BehaviorTest.php +++ b/tests/framework/base/BehaviorTest.php @@ -141,7 +141,7 @@ class BehaviorTest extends TestCase { $bar = new BarClass(); $behavior = new BarBehavior(); - $this->setExpectedException('yii\base\UnknownMethodException'); + $this->expectException('yii\base\UnknownMethodException'); $this->assertFalse($bar->hasMethod('nomagicBehaviorMethod')); $bar->attachBehavior('bar', $behavior); diff --git a/tests/framework/base/ComponentTest.php b/tests/framework/base/ComponentTest.php index da04122607..dc01e91de0 100644 --- a/tests/framework/base/ComponentTest.php +++ b/tests/framework/base/ComponentTest.php @@ -96,7 +96,7 @@ class ComponentTest extends TestCase public function testGetProperty() { $this->assertSame('default', $this->component->Text); - $this->setExpectedException('yii\base\UnknownPropertyException'); + $this->expectException('yii\base\UnknownPropertyException'); $value2 = $this->component->Caption; } @@ -105,7 +105,7 @@ class ComponentTest extends TestCase $value = 'new value'; $this->component->Text = $value; $this->assertEquals($value, $this->component->Text); - $this->setExpectedException('yii\base\UnknownPropertyException'); + $this->expectException('yii\base\UnknownPropertyException'); $this->component->NewMember = $value; } @@ -130,7 +130,7 @@ class ComponentTest extends TestCase public function testCallUnknownMethod() { - $this->setExpectedException('yii\base\UnknownMethodException'); + $this->expectException('yii\base\UnknownMethodException'); $this->component->unknownMethod(); } @@ -150,7 +150,7 @@ class ComponentTest extends TestCase public function testUnsetReadonly() { - $this->setExpectedException('yii\base\InvalidCallException'); + $this->expectException('yii\base\InvalidCallException'); unset($this->component->object); } @@ -244,7 +244,7 @@ class ComponentTest extends TestCase $this->assertSame($behavior, $component->detachBehavior('a')); $this->assertFalse($component->hasProperty('p')); - $this->setExpectedException('yii\base\UnknownMethodException'); + $this->expectException('yii\base\UnknownMethodException'); $component->test(); $p = 'as b'; @@ -305,10 +305,8 @@ class ComponentTest extends TestCase public function testSetReadOnlyProperty() { - $this->setExpectedException( - '\yii\base\InvalidCallException', - 'Setting read-only property: yiiunit\framework\base\NewComponent::object' - ); + $this->expectException('\yii\base\InvalidCallException'); + $this->expectExceptionMessage('Setting read-only property: yiiunit\framework\base\NewComponent::object'); $this->component->object = 'z'; } @@ -336,10 +334,8 @@ class ComponentTest extends TestCase public function testWriteOnlyProperty() { - $this->setExpectedException( - '\yii\base\InvalidCallException', - 'Getting write-only property: yiiunit\framework\base\NewComponent::writeOnly' - ); + $this->expectException('\yii\base\InvalidCallException'); + $this->expectExceptionMessage('Getting write-only property: yiiunit\framework\base\NewComponent::writeOnly'); $this->component->writeOnly; } diff --git a/tests/framework/base/DynamicModelTest.php b/tests/framework/base/DynamicModelTest.php index 8388a7f6aa..d4ca3397f5 100644 --- a/tests/framework/base/DynamicModelTest.php +++ b/tests/framework/base/DynamicModelTest.php @@ -72,7 +72,7 @@ class DynamicModelTest extends TestCase $model = new DynamicModel(compact('name', 'email')); $this->assertEquals($email, $model->email); $this->assertEquals($name, $model->name); - $this->setExpectedException('yii\base\UnknownPropertyException'); + $this->expectException('yii\base\UnknownPropertyException'); $age = $model->age; } diff --git a/tests/framework/base/ModelTest.php b/tests/framework/base/ModelTest.php index 351e07de26..0617c01689 100644 --- a/tests/framework/base/ModelTest.php +++ b/tests/framework/base/ModelTest.php @@ -427,8 +427,8 @@ class ModelTest extends TestCase public function testCreateValidators() { - $this->setExpectedException('yii\base\InvalidConfigException', - 'Invalid validation rule: a rule must specify both attribute names and validator type.'); + $this->expectException('yii\base\InvalidConfigException'); + $this->expectExceptionMessage('Invalid validation rule: a rule must specify both attribute names and validator type.'); $invalid = new InvalidRulesModel(); $invalid->createValidators(); diff --git a/tests/framework/base/ObjectTest.php b/tests/framework/base/ObjectTest.php index 401d39d7df..10320f96eb 100644 --- a/tests/framework/base/ObjectTest.php +++ b/tests/framework/base/ObjectTest.php @@ -61,7 +61,7 @@ class ObjectTest extends TestCase public function testGetProperty() { $this->assertSame('default', $this->object->Text); - $this->setExpectedException('yii\base\UnknownPropertyException'); + $this->expectException('yii\base\UnknownPropertyException'); $value2 = $this->object->Caption; } @@ -70,13 +70,13 @@ class ObjectTest extends TestCase $value = 'new value'; $this->object->Text = $value; $this->assertEquals($value, $this->object->Text); - $this->setExpectedException('yii\base\UnknownPropertyException'); + $this->expectException('yii\base\UnknownPropertyException'); $this->object->NewMember = $value; } public function testSetReadOnlyProperty() { - $this->setExpectedException('yii\base\InvalidCallException'); + $this->expectException('yii\base\InvalidCallException'); $this->object->object = 'test'; } @@ -106,13 +106,13 @@ class ObjectTest extends TestCase public function testUnsetReadOnlyProperty() { - $this->setExpectedException('yii\base\InvalidCallException'); + $this->expectException('yii\base\InvalidCallException'); unset($this->object->object); } public function testCallUnknownMethod() { - $this->setExpectedException('yii\base\UnknownMethodException'); + $this->expectException('yii\base\UnknownMethodException'); $this->object->unknownMethod(); } @@ -148,10 +148,8 @@ class ObjectTest extends TestCase public function testReadingWriteOnlyProperty() { - $this->setExpectedException( - 'yii\base\InvalidCallException', - 'Getting write-only property: yiiunit\framework\base\NewObject::writeOnly' - ); + $this->expectException('yii\base\InvalidCallException'); + $this->expectExceptionMessage('Getting write-only property: yiiunit\framework\base\NewObject::writeOnly'); $this->object->writeOnly; } } diff --git a/tests/framework/base/SecurityTest.php b/tests/framework/base/SecurityTest.php index debce77665..c872f4387d 100644 --- a/tests/framework/base/SecurityTest.php +++ b/tests/framework/base/SecurityTest.php @@ -887,7 +887,8 @@ TEXT; { static::$functions = ['random_bytes' => false, 'openssl_random_pseudo_bytes' => false, 'mcrypt_create_iv' => false ]; static::$fopen = false; - $this->setExpectedException('yii\base\Exception', 'Unable to generate a random key'); + $this->expectException('yii\base\Exception'); + $this->expectExceptionMessage('Unable to generate a random key'); $this->security->generateRandomKey(42); } @@ -899,7 +900,8 @@ TEXT; { static::$functions = ['random_bytes' => false, 'openssl_random_pseudo_bytes' => false, 'mcrypt_create_iv' => false ]; static::$fread = false; - $this->setExpectedException('yii\base\Exception', 'Unable to generate a random key'); + $this->expectException('yii\base\Exception'); + $this->expectExceptionMessage('Unable to generate a random key'); $this->security->generateRandomKey(42); } @@ -933,7 +935,8 @@ TEXT; } // there is no /dev/urandom on windows so we expect this to fail if (DIRECTORY_SEPARATOR === '\\' && $functions['random_bytes'] === false && $functions['openssl_random_pseudo_bytes'] === false && $functions['mcrypt_create_iv'] === false ) { - $this->setExpectedException('yii\base\Exception', 'Unable to generate a random key'); + $this->expectException('yii\base\Exception'); + $this->expectExceptionMessage('Unable to generate a random key'); } // Function mcrypt_create_iv() is deprecated since PHP 7.1 if (version_compare(PHP_VERSION, '7.1.0alpha', '>=') && $functions['random_bytes'] === false && $functions['mcrypt_create_iv'] === true) { diff --git a/tests/framework/console/ControllerTest.php b/tests/framework/console/ControllerTest.php index 041bc84484..fda6929411 100644 --- a/tests/framework/console/ControllerTest.php +++ b/tests/framework/console/ControllerTest.php @@ -61,7 +61,8 @@ class ControllerTest extends TestCase $params = ['avaliable']; $message = Yii::t('yii', 'Missing required arguments: {params}', ['params' => implode(', ', ['missing'])]); - $this->setExpectedException('yii\console\Exception', $message); + $this->expectException('yii\console\Exception'); + $this->expectExceptionMessage($message); $result = $controller->runAction('aksi3', $params); } diff --git a/tests/framework/console/RequestTest.php b/tests/framework/console/RequestTest.php index c4c1bee188..b9ec1635a2 100644 --- a/tests/framework/console/RequestTest.php +++ b/tests/framework/console/RequestTest.php @@ -140,7 +140,8 @@ class RequestTest extends TestCase public function testResolve($params, $expected, $expectedException = null) { if (isset($expectedException)) { - $this->setExpectedException($expectedException[0], $expectedException[1]); + $this->expectException($expectedException[0]); + $this->expectExceptionMessage($expectedException[1]); } $request = new Request(); diff --git a/tests/framework/console/controllers/AssetControllerTest.php b/tests/framework/console/controllers/AssetControllerTest.php index 7e5f2eb4c2..9e3026b949 100644 --- a/tests/framework/console/controllers/AssetControllerTest.php +++ b/tests/framework/console/controllers/AssetControllerTest.php @@ -67,7 +67,10 @@ class AssetControllerTest extends TestCase */ protected function createAssetController() { - $module = $this->getMock('yii\\base\\Module', ['fake'], ['console']); + $module = $this->getMockBuilder('yii\\base\\Module') + ->setMethods(['fake']) + ->setConstructorArgs(['console']) + ->getMock(); $assetController = new AssetControllerMock('asset', $module); $assetController->interactive = false; $assetController->jsCompressor = 'cp {from} {to}'; @@ -436,7 +439,8 @@ EOL; // Assert : $expectedExceptionMessage = ": {$namespace}\AssetA -> {$namespace}\AssetB -> {$namespace}\AssetC -> {$namespace}\AssetA"; - $this->setExpectedException('yii\console\Exception', $expectedExceptionMessage); + $this->expectException('yii\console\Exception'); + $this->expectExceptionMessage($expectedExceptionMessage); // When : $this->runAssetControllerAction('compress', [$configFile, $bundleFile]); diff --git a/tests/framework/console/controllers/BaseMessageControllerTest.php b/tests/framework/console/controllers/BaseMessageControllerTest.php index b22af5b883..1b7251dfd3 100644 --- a/tests/framework/console/controllers/BaseMessageControllerTest.php +++ b/tests/framework/console/controllers/BaseMessageControllerTest.php @@ -55,7 +55,10 @@ abstract class BaseMessageControllerTest extends TestCase */ protected function createMessageController() { - $module = $this->getMock('yii\\base\\Module', ['fake'], ['console']); + $module = $this->getMockBuilder('yii\\base\\Module') + ->setMethods(['fake']) + ->setConstructorArgs(['console']) + ->getMock(); $messageController = new MessageControllerMock('message', $module); $messageController->interactive = false; @@ -146,7 +149,7 @@ abstract class BaseMessageControllerTest extends TestCase public function testConfigFileNotExist() { - $this->setExpectedException('yii\\console\\Exception'); + $this->expectException('yii\\console\\Exception'); $this->runMessageControllerAction('extract', ['not_existing_file.php']); } diff --git a/tests/framework/console/controllers/HelpControllerTest.php b/tests/framework/console/controllers/HelpControllerTest.php index e508b16662..0364e26714 100644 --- a/tests/framework/console/controllers/HelpControllerTest.php +++ b/tests/framework/console/controllers/HelpControllerTest.php @@ -27,7 +27,10 @@ class HelpControllerTest extends TestCase */ protected function createController() { - $module = $this->getMock('yii\\base\\Module', ['fake'], ['console']); + $module = $this->getMockBuilder('yii\\base\\Module') + ->setMethods(['fake']) + ->setConstructorArgs(['console']) + ->getMock(); return new BufferedHelpController('help', $module); } diff --git a/tests/framework/console/controllers/MigrateControllerTestTrait.php b/tests/framework/console/controllers/MigrateControllerTestTrait.php index 1aae086d5e..acbb904f8c 100644 --- a/tests/framework/console/controllers/MigrateControllerTestTrait.php +++ b/tests/framework/console/controllers/MigrateControllerTestTrait.php @@ -60,7 +60,10 @@ trait MigrateControllerTestTrait */ protected function createMigrateController(array $config = []) { - $module = $this->getMock('yii\\base\\Module', ['fake'], ['console']); + $module = $this->getMockBuilder('yii\\base\\Module') + ->setMethods(['fake']) + ->setConstructorArgs(['console']) + ->getMock(); $class = $this->migrateControllerClass; $migrateController = new $class('migrate', $module); $migrateController->interactive = false; diff --git a/tests/framework/db/ActiveRecordTest.php b/tests/framework/db/ActiveRecordTest.php index 1e9a6b53a3..f31d76a8fc 100644 --- a/tests/framework/db/ActiveRecordTest.php +++ b/tests/framework/db/ActiveRecordTest.php @@ -1172,7 +1172,7 @@ abstract class ActiveRecordTest extends DatabaseTestCase $record = Document::findOne(1); $record->content = 'Rewrite attempt content'; $record->version = 0; - $this->setExpectedException('yii\db\StaleObjectException'); + $this->expectException('yii\db\StaleObjectException'); $record->save(false); } diff --git a/tests/framework/db/CommandTest.php b/tests/framework/db/CommandTest.php index 80804bd6fa..1f29c39a08 100644 --- a/tests/framework/db/CommandTest.php +++ b/tests/framework/db/CommandTest.php @@ -71,7 +71,7 @@ abstract class CommandTest extends DatabaseTestCase $this->assertEquals(1, $command->queryScalar()); $command = $db->createCommand('bad SQL'); - $this->setExpectedException('\yii\db\Exception'); + $this->expectException('\yii\db\Exception'); $command->execute(); } @@ -132,7 +132,7 @@ abstract class CommandTest extends DatabaseTestCase $this->assertFalse($command->queryScalar()); $command = $db->createCommand('bad SQL'); - $this->setExpectedException('\yii\db\Exception'); + $this->expectException('\yii\db\Exception'); $command->query(); } @@ -649,7 +649,7 @@ SQL; public function testIntegrityViolation() { - $this->setExpectedException('\yii\db\IntegrityException'); + $this->expectException('\yii\db\IntegrityException'); $db = $this->getConnection(); diff --git a/tests/framework/db/ConnectionTest.php b/tests/framework/db/ConnectionTest.php index c31d6e3e4e..761be2af36 100644 --- a/tests/framework/db/ConnectionTest.php +++ b/tests/framework/db/ConnectionTest.php @@ -35,7 +35,7 @@ abstract class ConnectionTest extends DatabaseTestCase $connection = new Connection; $connection->dsn = 'unknown::memory:'; - $this->setExpectedException('yii\db\Exception'); + $this->expectException('yii\db\Exception'); $connection->open(); } diff --git a/tests/framework/di/InstanceTest.php b/tests/framework/di/InstanceTest.php index 3f91f48b11..6b8c059a58 100644 --- a/tests/framework/di/InstanceTest.php +++ b/tests/framework/di/InstanceTest.php @@ -100,7 +100,8 @@ class InstanceTest extends TestCase 'dsn' => 'test', ]); - $this->setExpectedException('yii\base\InvalidConfigException', '"db" refers to a yii\db\Connection component. yii\base\Widget is expected.'); + $this->expectException('yii\base\InvalidConfigException'); + $this->expectExceptionMessage('"db" refers to a yii\db\Connection component. yii\base\Widget is expected.'); Instance::ensure('db', 'yii\base\Widget', $container); Instance::ensure(['class' => 'yii\db\Connection', 'dsn' => 'test'], 'yii\base\Widget', $container); @@ -108,13 +109,15 @@ class InstanceTest extends TestCase public function testExceptionInvalidDataType() { - $this->setExpectedException('yii\base\InvalidConfigException', 'Invalid data type: yii\db\Connection. yii\base\Widget is expected.'); + $this->expectException('yii\base\InvalidConfigException'); + $this->expectExceptionMessage('Invalid data type: yii\db\Connection. yii\base\Widget is expected.'); Instance::ensure(new Connection, 'yii\base\Widget'); } public function testExceptionComponentIsNotSpecified() { - $this->setExpectedException('yii\base\InvalidConfigException', 'The required component is not specified.'); + $this->expectException('yii\base\InvalidConfigException'); + $this->expectExceptionMessage('The required component is not specified.'); Instance::ensure(''); } @@ -175,10 +178,8 @@ PHP public function testRestoreAfterVarExportRequiresId() { - $this->setExpectedException( - 'yii\base\InvalidConfigException', - 'Failed to instantiate class "Instance". Required parameter "id" is missing' - ); + $this->expectException('yii\base\InvalidConfigException'); + $this->expectExceptionMessage('Failed to instantiate class "Instance". Required parameter "id" is missing'); Instance::__set_state([]); } diff --git a/tests/framework/filters/AccessRuleTest.php b/tests/framework/filters/AccessRuleTest.php index 3a8394e6a0..5e5cef0f64 100644 --- a/tests/framework/filters/AccessRuleTest.php +++ b/tests/framework/filters/AccessRuleTest.php @@ -34,7 +34,9 @@ class AccessRuleTest extends \yiiunit\TestCase protected function mockRequest($method = 'GET') { /** @var Request $request */ - $request = $this->getMockBuilder('\yii\web\Request')->setMethods(['getMethod'])->getMock(); + $request = $this->getMockBuilder('\yii\web\Request') + ->setMethods(['getMethod']) + ->getMock(); $request->method('getMethod')->willReturn($method); return $request; } diff --git a/tests/framework/filters/HostControlTest.php b/tests/framework/filters/HostControlTest.php index ed53d4d5a6..9ec462a014 100644 --- a/tests/framework/filters/HostControlTest.php +++ b/tests/framework/filters/HostControlTest.php @@ -148,7 +148,8 @@ class HostControlTest extends TestCase public function testErrorHandlerWithDefaultHost() { - $this->setExpectedException('yii\web\NotFoundHttpException', 'Page not found.'); + $this->expectException('yii\web\NotFoundHttpException'); + $this->expectExceptionMessage('Page not found.'); $filter = new HostControl(); $filter->allowedHosts = ['example.com']; diff --git a/tests/framework/filters/auth/AuthMethodTest.php b/tests/framework/filters/auth/AuthMethodTest.php index c02bd448ee..f79e274446 100644 --- a/tests/framework/filters/auth/AuthMethodTest.php +++ b/tests/framework/filters/auth/AuthMethodTest.php @@ -31,7 +31,9 @@ class AuthMethodTest extends TestCase */ protected function createFilter($authenticateCallback) { - $filter = $this->getMock(AuthMethod::className(), ['authenticate']); + $filter = $this->getMockBuilder(AuthMethod::className()) + ->setMethods(['authenticate']) + ->getMock(); $filter->method('authenticate')->willReturnCallback($authenticateCallback); return $filter; @@ -58,7 +60,7 @@ class AuthMethodTest extends TestCase $this->assertTrue($filter->beforeAction($action)); $filter = $this->createFilter(function () {return null;}); - $this->setExpectedException('yii\web\UnauthorizedHttpException'); + $this->expectException('yii\web\UnauthorizedHttpException'); $this->assertTrue($filter->beforeAction($action)); } diff --git a/tests/framework/helpers/FileHelperTest.php b/tests/framework/helpers/FileHelperTest.php index 082d473ac8..2b97f1593f 100644 --- a/tests/framework/helpers/FileHelperTest.php +++ b/tests/framework/helpers/FileHelperTest.php @@ -293,7 +293,7 @@ class FileHelperTest extends TestCase $dirName => [], ]); - $this->setExpectedException('yii\base\InvalidParamException'); + $this->expectException('yii\base\InvalidParamException'); $dirName = $this->testFilePath . DIRECTORY_SEPARATOR . 'test_dir'; FileHelper::copyDirectory($dirName, $dirName); @@ -309,7 +309,7 @@ class FileHelperTest extends TestCase 'backup' => ['data' => []] ]); - $this->setExpectedException('yii\base\InvalidParamException'); + $this->expectException('yii\base\InvalidParamException'); FileHelper::copyDirectory( $this->testFilePath . DIRECTORY_SEPARATOR . 'backup', diff --git a/tests/framework/helpers/HtmlTest.php b/tests/framework/helpers/HtmlTest.php index 443f416e02..af84a69655 100644 --- a/tests/framework/helpers/HtmlTest.php +++ b/tests/framework/helpers/HtmlTest.php @@ -1309,7 +1309,7 @@ EOD; public function testAttributeNameValidation($name, $expected) { if (!isset($expected)) { - $this->setExpectedException('yii\base\InvalidParamException'); + $this->expectException('yii\base\InvalidParamException'); Html::getAttributeName($name); } else { $this->assertEquals($expected, Html::getAttributeName($name)); @@ -1323,7 +1323,7 @@ EOD; */ public function testAttributeNameException($name) { - $this->setExpectedException('yii\base\InvalidParamException'); + $this->expectException('yii\base\InvalidParamException'); Html::getAttributeName($name); } } diff --git a/tests/framework/helpers/JsonTest.php b/tests/framework/helpers/JsonTest.php index 7d59e36395..a88ef943a6 100644 --- a/tests/framework/helpers/JsonTest.php +++ b/tests/framework/helpers/JsonTest.php @@ -147,7 +147,7 @@ class JsonTest extends TestCase // exception $json = '{"a":1,"b":2'; - $this->setExpectedException('yii\base\InvalidParamException'); + $this->expectException('yii\base\InvalidParamException'); Json::decode($json); } diff --git a/tests/framework/helpers/UrlTest.php b/tests/framework/helpers/UrlTest.php index 543d3d33b2..505e70c375 100644 --- a/tests/framework/helpers/UrlTest.php +++ b/tests/framework/helpers/UrlTest.php @@ -108,7 +108,7 @@ class UrlTest extends TestCase // In case there is no controller, an exception should be thrown for relative route $this->removeMockedAction(); - $this->setExpectedException('yii\base\InvalidParamException'); + $this->expectException('yii\base\InvalidParamException'); Url::toRoute('site/view'); } @@ -217,7 +217,7 @@ class UrlTest extends TestCase //In case there is no controller, throw an exception $this->removeMockedAction(); - $this->setExpectedException('yii\base\InvalidParamException'); + $this->expectException('yii\base\InvalidParamException'); Url::to(['site/view']); } diff --git a/tests/framework/i18n/FallbackMessageFormatterTest.php b/tests/framework/i18n/FallbackMessageFormatterTest.php index 97765d52e8..4b2de7c86a 100644 --- a/tests/framework/i18n/FallbackMessageFormatterTest.php +++ b/tests/framework/i18n/FallbackMessageFormatterTest.php @@ -219,7 +219,7 @@ _MSG_ { $pattern = 'Number {'.self::N.', number, percent}'; $formatter = new FallbackMessageFormatter(); - $this->setExpectedException('yii\base\NotSupportedException'); + $this->expectException('yii\base\NotSupportedException'); $formatter->fallbackFormat($pattern, [self::N => self::N_VALUE], 'en-US'); } @@ -227,7 +227,7 @@ _MSG_ { $pattern = 'Number {'.self::N.', number, currency}'; $formatter = new FallbackMessageFormatter(); - $this->setExpectedException('yii\base\NotSupportedException'); + $this->expectException('yii\base\NotSupportedException'); $formatter->fallbackFormat($pattern, [self::N => self::N_VALUE], 'en-US'); } } diff --git a/tests/framework/i18n/FormatterDateTest.php b/tests/framework/i18n/FormatterDateTest.php index 354f8542f4..70758d84b4 100644 --- a/tests/framework/i18n/FormatterDateTest.php +++ b/tests/framework/i18n/FormatterDateTest.php @@ -44,7 +44,7 @@ class FormatterDateTest extends TestCase $this->assertSame(date('M j, Y', $value), $this->formatter->format($value, 'date')); $this->assertSame(date('M j, Y', $value), $this->formatter->format($value, 'DATE')); $this->assertSame(date('Y/m/d', $value), $this->formatter->format($value, ['date', 'php:Y/m/d'])); - $this->setExpectedException('\yii\base\InvalidParamException'); + $this->expectException('\yii\base\InvalidParamException'); $this->assertSame(date('Y-m-d', $value), $this->formatter->format($value, 'data')); } diff --git a/tests/framework/i18n/FormatterTest.php b/tests/framework/i18n/FormatterTest.php index 5fd885ab70..fb41b7c0d8 100644 --- a/tests/framework/i18n/FormatterTest.php +++ b/tests/framework/i18n/FormatterTest.php @@ -47,7 +47,7 @@ class FormatterTest extends TestCase $this->assertSame(date('M j, Y', $value), $this->formatter->format($value, 'date')); $this->assertSame(date('M j, Y', $value), $this->formatter->format($value, 'DATE')); $this->assertSame(date('Y/m/d', $value), $this->formatter->format($value, ['date', 'php:Y/m/d'])); - $this->setExpectedException('\yii\base\InvalidParamException'); + $this->expectException('\yii\base\InvalidParamException'); $this->assertSame(date('Y-m-d', $value), $this->formatter->format($value, 'data')); } diff --git a/tests/framework/log/EmailTargetTest.php b/tests/framework/log/EmailTargetTest.php index 26897aa7f2..ac2eb48a6b 100644 --- a/tests/framework/log/EmailTargetTest.php +++ b/tests/framework/log/EmailTargetTest.php @@ -70,15 +70,18 @@ class EmailTargetTest extends TestCase $message->expects($this->once())->method('send')->with($this->equalTo($this->mailer)); $message->expects($this->once())->method('setSubject')->with($this->equalTo('Hello world')); - $mailTarget = $this->getMock('yii\\log\\EmailTarget', ['formatMessage'], [ - [ - 'mailer' => $this->mailer, - 'message'=> [ - 'to' => 'developer@example.com', - 'subject' => 'Hello world' + $mailTarget = $this->getMockBuilder('yii\\log\\EmailTarget') + ->setMethods(['formatMessage']) + ->setConstructorArgs([ + [ + 'mailer' => $this->mailer, + 'message'=> [ + 'to' => 'developer@example.com', + 'subject' => 'Hello world' + ] ] - ] - ]); + ]) + ->getMock(); $mailTarget->messages = $messages; $mailTarget->expects($this->exactly(2))->method('formatMessage')->willReturnMap( @@ -111,14 +114,17 @@ class EmailTargetTest extends TestCase $message->expects($this->once())->method('send')->with($this->equalTo($this->mailer)); $message->expects($this->once())->method('setSubject')->with($this->equalTo('Application Log')); - $mailTarget = $this->getMock('yii\\log\\EmailTarget', ['formatMessage'], [ - [ - 'mailer' => $this->mailer, - 'message'=> [ - 'to' => 'developer@example.com', + $mailTarget = $this->getMockBuilder('yii\\log\\EmailTarget') + ->setMethods(['formatMessage']) + ->setConstructorArgs([ + [ + 'mailer' => $this->mailer, + 'message'=> [ + 'to' => 'developer@example.com', + ] ] - ] - ]); + ]) + ->getMock(); $mailTarget->messages = $messages; $mailTarget->expects($this->exactly(2))->method('formatMessage')->willReturnMap( diff --git a/tests/framework/log/LoggerTest.php b/tests/framework/log/LoggerTest.php index b9441e7191..267aaa07af 100644 --- a/tests/framework/log/LoggerTest.php +++ b/tests/framework/log/LoggerTest.php @@ -26,7 +26,9 @@ class LoggerTest extends TestCase protected function setUp() { $this->logger = new Logger(); - $this->dispatcher = $this->getMock('yii\\log\\Dispatcher', ['dispatch']); + $this->dispatcher = $this->getMockBuilder('yii\\log\\Dispatcher') + ->setMethods(['dispatch']) + ->getMock(); } /** @@ -80,7 +82,9 @@ class LoggerTest extends TestCase */ public function testLogWithFlush() { - $logger = $this->getMock('yii\\log\\Logger', ['flush']); + $logger = $this->getMockBuilder('yii\\log\\Logger') + ->setMethods(['flush']) + ->getMock(); $logger->flushInterval = 1; $logger->expects($this->exactly(1))->method('flush'); $logger->log('test1', Logger::LEVEL_INFO); @@ -91,7 +95,7 @@ class LoggerTest extends TestCase */ public function testFlushWithoutDispatcher() { - $dispatcher = $this->getMock('\stdClass'); + $dispatcher = $this->getMockBuilder('\stdClass')->getMock(); $dispatcher->expects($this->never())->method($this->anything()); $this->logger->messages = ['anything']; @@ -141,7 +145,9 @@ class LoggerTest extends TestCase ['duration' => 30], ]; - $logger = $this->getMock('yii\\log\\Logger', ['getProfiling']); + $logger = $this->getMockBuilder('yii\\log\\Logger') + ->setMethods(['getProfiling']) + ->getMock(); $logger->method('getProfiling')->willReturn($timings); $logger->expects($this->once()) ->method('getProfiling') @@ -271,7 +277,9 @@ class LoggerTest extends TestCase { $messages = ['anyData']; $returnValue = 'return value'; - $logger = $this->getMock('yii\\log\\Logger', ['calculateTimings']); + $logger = $this->getMockBuilder('yii\\log\\Logger') + ->setMethods(['calculateTimings']) + ->getMock(); $logger->messages = $messages; $logger->method('calculateTimings')->willReturn($returnValue); @@ -295,7 +303,9 @@ class LoggerTest extends TestCase 'duration' => 5, ] ]; - $logger = $this->getMock('yii\\log\\Logger', ['calculateTimings']); + $logger = $this->getMockBuilder('yii\\log\\Logger') + ->setMethods(['calculateTimings']) + ->getMock(); $logger->messages = $messages; $logger->method('calculateTimings')->willReturn($returnValue); @@ -332,7 +342,9 @@ class LoggerTest extends TestCase /** * Matched by category name */ - $logger = $this->getMock('yii\\log\\Logger', ['calculateTimings']); + $logger = $this->getMockBuilder('yii\\log\\Logger') + ->setMethods(['calculateTimings']) + ->getMock(); $logger->messages = $messages; $logger->method('calculateTimings')->willReturn($returnValue); @@ -342,7 +354,9 @@ class LoggerTest extends TestCase /** * Matched by prefix */ - $logger = $this->getMock('yii\\log\\Logger', ['calculateTimings']); + $logger = $this->getMockBuilder('yii\\log\\Logger') + ->setMethods(['calculateTimings']) + ->getMock(); $logger->messages = $messages; $logger->method('calculateTimings')->willReturn($returnValue); @@ -388,7 +402,9 @@ class LoggerTest extends TestCase /** * Exclude by category name */ - $logger = $this->getMock('yii\\log\\Logger', ['calculateTimings']); + $logger = $this->getMockBuilder('yii\\log\\Logger') + ->setMethods(['calculateTimings']) + ->getMock(); $logger->messages = $messages; $logger->method('calculateTimings')->willReturn($returnValue); @@ -398,7 +414,9 @@ class LoggerTest extends TestCase /** * Exclude by category prefix */ - $logger = $this->getMock('yii\\log\\Logger', ['calculateTimings']); + $logger = $this->getMockBuilder('yii\\log\\Logger') + ->setMethods(['calculateTimings']) + ->getMock(); $logger->messages = $messages; $logger->method('calculateTimings')->willReturn($returnValue); diff --git a/tests/framework/log/SyslogTargetTest.php b/tests/framework/log/SyslogTargetTest.php index 4a61b82033..29c83f00dd 100644 --- a/tests/framework/log/SyslogTargetTest.php +++ b/tests/framework/log/SyslogTargetTest.php @@ -50,7 +50,9 @@ namespace yiiunit\framework\log { */ protected function setUp() { - $this->syslogTarget = $this->getMock('yii\\log\\SyslogTarget', ['getMessagePrefix']); + $this->syslogTarget = $this->getMockBuilder('yii\\log\\SyslogTarget') + ->setMethods(['getMessagePrefix']) + ->getMock(); } /** @@ -70,8 +72,9 @@ namespace yiiunit\framework\log { ['profile begin message', Logger::LEVEL_PROFILE_BEGIN], ['profile end message', Logger::LEVEL_PROFILE_END], ]; - $syslogTarget = $this - ->getMock('yii\\log\\SyslogTarget', ['openlog', 'syslog', 'formatMessage', 'closelog']); + $syslogTarget = $this->getMockBuilder('yii\\log\\SyslogTarget') + ->setMethods(['openlog', 'syslog', 'formatMessage', 'closelog']) + ->getMock(); $syslogTarget->identity = $identity; $syslogTarget->options = $options; diff --git a/tests/framework/log/TargetTest.php b/tests/framework/log/TargetTest.php index dcc1ab3cde..67316da32e 100644 --- a/tests/framework/log/TargetTest.php +++ b/tests/framework/log/TargetTest.php @@ -138,7 +138,8 @@ class TargetTest extends TestCase $target->setLevels(['trace']); $this->assertEquals(Logger::LEVEL_TRACE, $target->getLevels()); - $this->setExpectedException('yii\\base\\InvalidConfigException', 'Unrecognized level: unknown level'); + $this->expectException('yii\\base\\InvalidConfigException'); + $this->expectExceptionMessage('Unrecognized level: unknown level'); $target->setLevels(['info', 'unknown level']); } @@ -156,7 +157,8 @@ class TargetTest extends TestCase $target->setLevels(Logger::LEVEL_TRACE); $this->assertEquals(Logger::LEVEL_TRACE, $target->getLevels()); - $this->setExpectedException('yii\\base\\InvalidConfigException', 'Incorrect 128 value'); + $this->expectException('yii\\base\\InvalidConfigException'); + $this->expectExceptionMessage('Incorrect 128 value'); $target->setLevels(128); } } diff --git a/tests/framework/mail/BaseMailerTest.php b/tests/framework/mail/BaseMailerTest.php index 2e457bb65d..11d6bea1da 100644 --- a/tests/framework/mail/BaseMailerTest.php +++ b/tests/framework/mail/BaseMailerTest.php @@ -292,7 +292,9 @@ TEXT { $message = new Message(); - $mailerMock = $this->getMockBuilder('yiiunit\framework\mail\Mailer')->setMethods(['beforeSend', 'afterSend'])->getMock(); + $mailerMock = $this->getMockBuilder('yiiunit\framework\mail\Mailer') + ->setMethods(['beforeSend', 'afterSend']) + ->getMock(); $mailerMock->expects($this->once())->method('beforeSend')->with($message)->will($this->returnValue(true)); $mailerMock->expects($this->once())->method('afterSend')->with($message, true); $mailerMock->send($message); diff --git a/tests/framework/validators/CompareValidatorTest.php b/tests/framework/validators/CompareValidatorTest.php index 5e3d888106..181e98abe5 100644 --- a/tests/framework/validators/CompareValidatorTest.php +++ b/tests/framework/validators/CompareValidatorTest.php @@ -19,7 +19,7 @@ class CompareValidatorTest extends TestCase public function testValidateValueException() { - $this->setExpectedException('yii\base\InvalidConfigException'); + $this->expectException('yii\base\InvalidConfigException'); $val = new CompareValidator; $val->validate('val'); } diff --git a/tests/framework/validators/FilterValidatorTest.php b/tests/framework/validators/FilterValidatorTest.php index 1b77047e50..b554e48f13 100644 --- a/tests/framework/validators/FilterValidatorTest.php +++ b/tests/framework/validators/FilterValidatorTest.php @@ -19,7 +19,7 @@ class FilterValidatorTest extends TestCase public function testAssureExceptionOnInit() { - $this->setExpectedException('yii\base\InvalidConfigException'); + $this->expectException('yii\base\InvalidConfigException'); new FilterValidator(); } diff --git a/tests/framework/validators/IpValidatorTest.php b/tests/framework/validators/IpValidatorTest.php index 04cd90e7ed..99ebb8184a 100644 --- a/tests/framework/validators/IpValidatorTest.php +++ b/tests/framework/validators/IpValidatorTest.php @@ -23,8 +23,8 @@ class IpValidatorTest extends TestCase public function testInitException() { - $this->setExpectedException('yii\base\InvalidConfigException', - 'Both IPv4 and IPv6 checks can not be disabled at the same time'); + $this->expectException('yii\base\InvalidConfigException'); + $this->expectExceptionMessage('Both IPv4 and IPv6 checks can not be disabled at the same time'); new IpValidator(['ipv4' => false, 'ipv6' => false]); } diff --git a/tests/framework/validators/RangeValidatorTest.php b/tests/framework/validators/RangeValidatorTest.php index 15b9eff585..f882eb1153 100644 --- a/tests/framework/validators/RangeValidatorTest.php +++ b/tests/framework/validators/RangeValidatorTest.php @@ -19,7 +19,8 @@ class RangeValidatorTest extends TestCase public function testInitException() { - $this->setExpectedException('yii\base\InvalidConfigException', 'The "range" property must be set.'); + $this->expectException('yii\base\InvalidConfigException'); + $this->expectExceptionMessage('The "range" property must be set.'); new RangeValidator(['range' => 'not an array']); } diff --git a/tests/framework/validators/RegularExpressionValidatorTest.php b/tests/framework/validators/RegularExpressionValidatorTest.php index e9c3ce7557..adbf7be84b 100644 --- a/tests/framework/validators/RegularExpressionValidatorTest.php +++ b/tests/framework/validators/RegularExpressionValidatorTest.php @@ -48,7 +48,7 @@ class RegularExpressionValidatorTest extends TestCase public function testInitException() { - $this->setExpectedException('yii\base\InvalidConfigException'); + $this->expectException('yii\base\InvalidConfigException'); $val = new RegularExpressionValidator(); $val->validate('abc'); } diff --git a/tests/framework/validators/UniqueValidatorTest.php b/tests/framework/validators/UniqueValidatorTest.php index c77369d167..afdb0c44b4 100644 --- a/tests/framework/validators/UniqueValidatorTest.php +++ b/tests/framework/validators/UniqueValidatorTest.php @@ -139,7 +139,7 @@ abstract class UniqueValidatorTest extends DatabaseTestCase public function testValidateAttributeAttributeNotInTableException() { - $this->setExpectedException('yii\db\Exception'); + $this->expectException('yii\db\Exception'); $val = new UniqueValidator(); $m = new ValidatorTestMainModel(); $val->validateAttribute($m, 'testMainVal'); diff --git a/tests/framework/validators/ValidatorTest.php b/tests/framework/validators/ValidatorTest.php index 4f648874d4..eb3ab05422 100644 --- a/tests/framework/validators/ValidatorTest.php +++ b/tests/framework/validators/ValidatorTest.php @@ -168,10 +168,8 @@ class ValidatorTest extends TestCase public function testValidateValue() { - $this->setExpectedException( - 'yii\base\NotSupportedException', - TestValidator::className() . ' does not support validateValue().' - ); + $this->expectException('yii\base\NotSupportedException'); + $this->expectExceptionMessage(TestValidator::className() . ' does not support validateValue().'); $val = new TestValidator(); $val->validate('abc'); } diff --git a/tests/framework/web/AssetBundleTest.php b/tests/framework/web/AssetBundleTest.php index 430da08bbe..d2e927460c 100644 --- a/tests/framework/web/AssetBundleTest.php +++ b/tests/framework/web/AssetBundleTest.php @@ -299,13 +299,13 @@ EOF; if ($jqAlreadyRegistered) { TestJqueryAsset::register($view); } - $this->setExpectedException('yii\\base\\InvalidConfigException'); + $this->expectException('yii\\base\\InvalidConfigException'); TestAssetBundle::register($view); } public function testCircularDependency() { - $this->setExpectedException('yii\\base\\InvalidConfigException'); + $this->expectException('yii\\base\\InvalidConfigException'); TestAssetCircleA::register($this->getView()); } diff --git a/tests/framework/web/CacheSessionTest.php b/tests/framework/web/CacheSessionTest.php index e61ced98aa..3cb9383a26 100644 --- a/tests/framework/web/CacheSessionTest.php +++ b/tests/framework/web/CacheSessionTest.php @@ -30,7 +30,7 @@ class CacheSessionTest extends \yiiunit\TestCase public function testInvalidCache() { - $this->setExpectedException('\Exception'); + $this->expectException('\Exception'); new CacheSession(['cache' => 'invalid']); } diff --git a/tests/framework/web/ResponseTest.php b/tests/framework/web/ResponseTest.php index 56967df313..866a43a259 100644 --- a/tests/framework/web/ResponseTest.php +++ b/tests/framework/web/ResponseTest.php @@ -75,7 +75,7 @@ class ResponseTest extends \yiiunit\TestCase */ public function testSendFileWrongRanges($rangeHeader) { - $this->setExpectedException('yii\web\RangeNotSatisfiableHttpException'); + $this->expectException('yii\web\RangeNotSatisfiableHttpException'); $dataFile = \Yii::getAlias('@yiiunit/data/web/data.txt'); $_SERVER['HTTP_RANGE'] = 'bytes=' . $rangeHeader; diff --git a/tests/framework/web/UserTest.php b/tests/framework/web/UserTest.php index f9dcc35660..dd457a3fee 100644 --- a/tests/framework/web/UserTest.php +++ b/tests/framework/web/UserTest.php @@ -268,7 +268,7 @@ class UserTest extends TestCase $this->reset(); $_SERVER['HTTP_ACCEPT'] = 'text/json;q=0.1'; - $this->setExpectedException('yii\\web\\ForbiddenHttpException'); + $this->expectException('yii\\web\\ForbiddenHttpException'); $user->loginRequired(); } @@ -291,7 +291,7 @@ class UserTest extends TestCase $this->mockWebApplication($appConfig); $this->reset(); $_SERVER['HTTP_ACCEPT'] = 'text/json,q=0.1'; - $this->setExpectedException('yii\\web\\ForbiddenHttpException'); + $this->expectException('yii\\web\\ForbiddenHttpException'); Yii::$app->user->loginRequired(); } diff --git a/tests/framework/widgets/BreadcrumbsTest.php b/tests/framework/widgets/BreadcrumbsTest.php index 75124c1457..17a816e984 100644 --- a/tests/framework/widgets/BreadcrumbsTest.php +++ b/tests/framework/widgets/BreadcrumbsTest.php @@ -88,7 +88,7 @@ class BreadcrumbsTest extends \yiiunit\TestCase { $link = ['url' => 'http://localhost/yii2']; $method = $this->reflectMethod(); - $this->setExpectedException('yii\base\InvalidConfigException'); + $this->expectException('yii\base\InvalidConfigException'); $method->invoke($this->breadcrumbs, $link, $this->breadcrumbs->itemTemplate); }