diff --git a/rector.php b/rector.php index 4a5fde83a45c..829ec303e0f4 100644 --- a/rector.php +++ b/rector.php @@ -42,6 +42,9 @@ use Rector\Php71\Rector\FuncCall\CountOnNullRector; use Rector\Php73\Rector\FuncCall\JsonThrowOnErrorRector; use Rector\Php73\Rector\FuncCall\StringifyStrNeedlesRector; +use Rector\PHPUnit\Rector\MethodCall\AssertFalseStrposToContainsRector; +use Rector\PHPUnit\Rector\MethodCall\AssertIssetToSpecificMethodRector; +use Rector\PHPUnit\Set\PHPUnitSetList; use Rector\Set\ValueObject\LevelSetList; use Rector\Set\ValueObject\SetList; use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator; @@ -53,6 +56,7 @@ return static function (ContainerConfigurator $containerConfigurator): void { $containerConfigurator->import(SetList::DEAD_CODE); $containerConfigurator->import(LevelSetList::UP_TO_PHP_73); + $containerConfigurator->import(PHPUnitSetList::PHPUNIT_SPECIFIC_METHOD); $parameters = $containerConfigurator->parameters(); @@ -109,6 +113,18 @@ // use mt_rand instead of random_int on purpose on non-cryptographically random RandomFunctionRector::class, + + // $this->assertTrue(isset($bar['foo'])) + // and $this->assertArrayHasKey('foo', $bar) + // or $this->assertObjectHasAttribute('foo', $bar); + // are not the same + AssertIssetToSpecificMethodRector::class => [ + __DIR__ . '/tests/system/Entity/EntityTest.php', + __DIR__ . '/tests/system/Session/SessionTest.php', + ], + + // assertContains() to string can't be used in PHPUnit 9.1 + AssertFalseStrposToContainsRector::class, ]); // auto import fully qualified class names diff --git a/tests/system/API/ResponseTraitTest.php b/tests/system/API/ResponseTraitTest.php index 958be5434689..1db28686eafe 100644 --- a/tests/system/API/ResponseTraitTest.php +++ b/tests/system/API/ResponseTraitTest.php @@ -488,7 +488,7 @@ public function testXMLFormatter() $this->formatter = new XMLFormatter(); $controller = $this->makeController(); - $this->assertSame('CodeIgniter\Format\XMLFormatter', get_class($this->formatter)); + $this->assertInstanceOf('CodeIgniter\Format\XMLFormatter', $this->formatter); $controller->respondCreated(['id' => 3], 'A Custom Reason'); $expected = <<<'EOH' diff --git a/tests/system/CLI/ConsoleTest.php b/tests/system/CLI/ConsoleTest.php index a5b3dd1e9783..087c28c12072 100644 --- a/tests/system/CLI/ConsoleTest.php +++ b/tests/system/CLI/ConsoleTest.php @@ -67,7 +67,7 @@ public function testHeader() $console = new Console($this->app); $console->showHeader(); $result = CITestStreamFilter::$buffer; - $this->assertTrue(strpos($result, sprintf('CodeIgniter v%s Command Line Tool', CodeIgniter::CI_VERSION)) > 0); + $this->assertGreaterThan(0, strpos($result, sprintf('CodeIgniter v%s Command Line Tool', CodeIgniter::CI_VERSION))); } public function testNoHeader() diff --git a/tests/system/CommonSingleServiceTest.php b/tests/system/CommonSingleServiceTest.php index bc2f4a1f60c8..7d078a6f9a2e 100644 --- a/tests/system/CommonSingleServiceTest.php +++ b/tests/system/CommonSingleServiceTest.php @@ -34,7 +34,7 @@ public function testSingleServiceWithNoParamsSupplied(string $service): void $service1 = single_service($service); $service2 = single_service($service); - $this->assertSame(get_class($service1), get_class($service2)); + $this->assertInstanceOf(get_class($service1), $service2); $this->assertNotSame($service1, $service2); } @@ -62,7 +62,7 @@ public function testSingleServiceWithAtLeastOneParamSupplied(string $service): v $service1 = single_service($service, ...$params); $service2 = single_service($service, ...$params); - $this->assertSame(get_class($service1), get_class($service2)); + $this->assertInstanceOf(get_class($service1), $service2); $this->assertNotSame($service1, $service2); if ($service === 'commands') { @@ -77,7 +77,7 @@ public function testSingleServiceWithAllParamsSupplied(): void // Assert that even passing true as last param this will // not create a shared instance. - $this->assertSame(get_class($cache1), get_class($cache2)); + $this->assertInstanceOf(get_class($cache1), $cache2); $this->assertNotSame($cache1, $cache2); } diff --git a/tests/system/Config/ConfigTest.php b/tests/system/Config/ConfigTest.php index 7f64a4851205..a784280bdc12 100644 --- a/tests/system/Config/ConfigTest.php +++ b/tests/system/Config/ConfigTest.php @@ -41,7 +41,7 @@ public function testCreateSharedInstance() $Config = Config::get('DocTypes'); $Config2 = Config::get('Config\\DocTypes'); - $this->assertTrue($Config === $Config2); + $this->assertSame($Config2, $Config); } public function testCreateNonConfig() diff --git a/tests/system/Config/ServicesTest.php b/tests/system/Config/ServicesTest.php index 77a0c77097fc..7fed04f53aa3 100644 --- a/tests/system/Config/ServicesTest.php +++ b/tests/system/Config/ServicesTest.php @@ -316,7 +316,7 @@ public function testReset() $response2 = service('response'); $this->assertInstanceOf(MockResponse::class, $response2); - $this->assertTrue($response !== $response2); + $this->assertNotSame($response2, $response); } /** diff --git a/tests/system/Cookie/CookieTest.php b/tests/system/Cookie/CookieTest.php index 781373755529..02a820218868 100644 --- a/tests/system/Cookie/CookieTest.php +++ b/tests/system/Cookie/CookieTest.php @@ -268,13 +268,13 @@ public function testArrayAccessOfCookie(): void { $cookie = new Cookie('cookie', 'monster'); - $this->assertTrue(isset($cookie['expire'])); + $this->assertArrayHasKey('expire', $cookie); $this->assertSame($cookie['expire'], $cookie->getExpiresTimestamp()); - $this->assertTrue(isset($cookie['httponly'])); + $this->assertArrayHasKey('httponly', $cookie); $this->assertSame($cookie['httponly'], $cookie->isHTTPOnly()); - $this->assertTrue(isset($cookie['samesite'])); + $this->assertArrayHasKey('samesite', $cookie); $this->assertSame($cookie['samesite'], $cookie->getSameSite()); - $this->assertTrue(isset($cookie['path'])); + $this->assertArrayHasKey('path', $cookie); $this->assertSame($cookie['path'], $cookie->getPath()); $this->expectException(InvalidArgumentException::class); diff --git a/tests/system/Database/Live/ConnectTest.php b/tests/system/Database/Live/ConnectTest.php index 5ee18a6f25eb..743b5450cca3 100644 --- a/tests/system/Database/Live/ConnectTest.php +++ b/tests/system/Database/Live/ConnectTest.php @@ -103,6 +103,6 @@ public function testConnectWithFailover() $db1 = Database::connect($this->tests); $this->assertSame($this->tests['failover'][0]['DBDriver'], $this->getPrivateProperty($db1, 'DBDriver')); - $this->assertTrue(count($db1->listTables()) >= 0); + $this->assertGreaterThanOrEqual(0, count($db1->listTables())); } } diff --git a/tests/system/Database/Live/DbUtilsTest.php b/tests/system/Database/Live/DbUtilsTest.php index c9b93242f4d1..2e8beda5f82f 100644 --- a/tests/system/Database/Live/DbUtilsTest.php +++ b/tests/system/Database/Live/DbUtilsTest.php @@ -79,7 +79,7 @@ public function testUtilsListDatabases() if (in_array($this->db->DBDriver, ['MySQLi', 'Postgre', 'SQLSRV'], true)) { $databases = $util->listDatabases(); - $this->assertTrue(in_array($this->db->getDatabase(), $databases, true)); + $this->assertContains($this->db->getDatabase(), $databases); } elseif ($this->db->DBDriver === 'SQLite3') { $this->expectException(DatabaseException::class); $this->expectExceptionMessage('Unsupported feature of the database platform you are using.'); diff --git a/tests/system/Database/Live/GetTest.php b/tests/system/Database/Live/GetTest.php index 2682f12e57ca..63f5ee9afd0c 100644 --- a/tests/system/Database/Live/GetTest.php +++ b/tests/system/Database/Live/GetTest.php @@ -78,8 +78,8 @@ public function testGetFieldNames() { $jobs = $this->db->table('job')->get()->getFieldNames(); - $this->assertTrue(in_array('name', $jobs, true)); - $this->assertTrue(in_array('description', $jobs, true)); + $this->assertContains('name', $jobs); + $this->assertContains('description', $jobs); } public function testGetFieldData() diff --git a/tests/system/Database/Live/PretendTest.php b/tests/system/Database/Live/PretendTest.php index b313f41fb7cb..3eae20e97cd2 100644 --- a/tests/system/Database/Live/PretendTest.php +++ b/tests/system/Database/Live/PretendTest.php @@ -36,7 +36,7 @@ public function testPretendReturnsQueryObject() ->table('user') ->get(); - $this->assertFalse($result instanceof Query); + $this->assertNotInstanceOf(Query::class, $result); $result = $this->db->pretend(true) ->table('user') diff --git a/tests/system/Database/Live/SQLite/AlterTableTest.php b/tests/system/Database/Live/SQLite/AlterTableTest.php index 029c764269a8..2816008a2a54 100644 --- a/tests/system/Database/Live/SQLite/AlterTableTest.php +++ b/tests/system/Database/Live/SQLite/AlterTableTest.php @@ -133,9 +133,9 @@ public function testDropColumnSuccess() $columns = $this->db->getFieldNames('foo'); - $this->assertFalse(in_array('name', $columns, true)); - $this->assertTrue(in_array('id', $columns, true)); - $this->assertTrue(in_array('email', $columns, true)); + $this->assertNotContains('name', $columns); + $this->assertContains('id', $columns); + $this->assertContains('email', $columns); } public function testDropColumnMaintainsKeys() diff --git a/tests/system/Filters/FiltersTest.php b/tests/system/Filters/FiltersTest.php index 6f5a94941f81..be5e04c5098e 100644 --- a/tests/system/Filters/FiltersTest.php +++ b/tests/system/Filters/FiltersTest.php @@ -553,7 +553,7 @@ public function testShortCircuit() $uri = 'admin/foo/bar'; $response = $filters->run($uri, 'before'); - $this->assertTrue($response instanceof ResponseInterface); + $this->assertInstanceOf(ResponseInterface::class, $response); $this->assertSame('http://google.com', $response->csp); } @@ -737,7 +737,7 @@ public function testAddFilter() $filters = $filters->initialize('admin/foo/bar'); $filters = $filters->getFilters(); - $this->assertTrue(in_array('some_alias', $filters['before'], true)); + $this->assertContains('some_alias', $filters['before']); } public function testAddFilterSection() @@ -753,7 +753,7 @@ public function testAddFilterSection() ->initialize('admin/foo/bar') ->getFilters(); - $this->assertTrue(in_array('another', $list['before'], true)); + $this->assertContains('another', $list['before']); } public function testInitializeTwice() @@ -770,7 +770,7 @@ public function testInitializeTwice() ->initialize() ->getFilters(); - $this->assertTrue(in_array('another', $list['before'], true)); + $this->assertContains('another', $list['before']); } public function testEnableFilter() @@ -791,7 +791,7 @@ public function testEnableFilter() $filters->enableFilter('google', 'before'); $filters = $filters->getFilters(); - $this->assertTrue(in_array('google', $filters['before'], true)); + $this->assertContains('google', $filters['before']); } public function testEnableFilterWithArguments() @@ -813,7 +813,7 @@ public function testEnableFilterWithArguments() $filters->enableFilter('role:admin , super', 'after'); $found = $filters->getFilters(); - $this->assertTrue(in_array('role', $found['before'], true)); + $this->assertContains('role', $found['before']); $this->assertSame(['admin', 'super'], $filters->getArguments('role')); $this->assertSame(['role' => ['admin', 'super']], $filters->getArguments()); @@ -845,7 +845,7 @@ public function testEnableFilterWithNoArguments() $filters->enableFilter('role', 'after'); $found = $filters->getFilters(); - $this->assertTrue(in_array('role', $found['before'], true)); + $this->assertContains('role', $found['before']); $response = $filters->run('admin/foo/bar', 'before'); diff --git a/tests/system/HTTP/IncomingRequestTest.php b/tests/system/HTTP/IncomingRequestTest.php index 1e975359a120..b467c9ca3e1a 100644 --- a/tests/system/HTTP/IncomingRequestTest.php +++ b/tests/system/HTTP/IncomingRequestTest.php @@ -544,8 +544,8 @@ public function testWithFalseBody() // Use `false` here to simulate file_get_contents returning a false value $request = new IncomingRequest(new App(), new URI(), false, new UserAgent()); - $this->assertTrue($request->getBody() !== false); - $this->assertTrue($request->getBody() === null); + $this->assertNotFalse($request->getBody()); + $this->assertNull($request->getBody()); } /** diff --git a/tests/system/HTTP/ResponseTest.php b/tests/system/HTTP/ResponseTest.php index dc50aa432140..64eab48dc0fa 100644 --- a/tests/system/HTTP/ResponseTest.php +++ b/tests/system/HTTP/ResponseTest.php @@ -63,7 +63,7 @@ public function testConstructWithCSPEnabled() $config->CSPEnabled = true; $response = new Response($config); - $this->assertTrue($response instanceof Response); + $this->assertInstanceOf(Response::class, $response); } public function testSetStatusCodeSetsReason() @@ -327,7 +327,7 @@ public function testJSONWithArray() $response->setJSON($body); $this->assertSame($expected, $response->getJSON()); - $this->assertTrue(strpos($response->getHeaderLine('content-type'), 'application/json') !== false); + $this->assertNotFalse(strpos($response->getHeaderLine('content-type'), 'application/json')); } public function testJSONGetFromNormalBody() @@ -364,7 +364,7 @@ public function testXMLWithArray() $response->setXML($body); $this->assertSame($expected, $response->getXML()); - $this->assertTrue(strpos($response->getHeaderLine('content-type'), 'application/xml') !== false); + $this->assertNotFalse(strpos($response->getHeaderLine('content-type'), 'application/xml')); } public function testXMLGetFromNormalBody() diff --git a/tests/system/I18n/TimeTest.php b/tests/system/I18n/TimeTest.php index e1e683c98d07..205a98098724 100644 --- a/tests/system/I18n/TimeTest.php +++ b/tests/system/I18n/TimeTest.php @@ -1023,7 +1023,7 @@ public function testCreateFromInstance() { $datetime = new DateTime(); $time = Time::createFromInstance($datetime); - $this->assertTrue($time instanceof Time); + $this->assertInstanceOf(Time::class, $time); $this->assertTrue($time->sameAs($datetime)); } diff --git a/tests/system/Images/BaseHandlerTest.php b/tests/system/Images/BaseHandlerTest.php index 708e4ef50d3a..8504779c44eb 100644 --- a/tests/system/Images/BaseHandlerTest.php +++ b/tests/system/Images/BaseHandlerTest.php @@ -58,7 +58,7 @@ protected function setUp(): void public function testNew() { $handler = Services::image('gd', null, false); - $this->assertTrue($handler instanceof BaseHandler); + $this->assertInstanceOf(BaseHandler::class, $handler); } public function testWithFile() @@ -68,7 +68,7 @@ public function testWithFile() $handler->withFile($path); $image = $handler->getFile(); - $this->assertTrue($image instanceof Image); + $this->assertInstanceOf(Image::class, $image); $this->assertSame(155, $image->origWidth); $this->assertSame($path, $image->getPathname()); } @@ -104,15 +104,15 @@ public function testFileTypes() $handler = Services::image('gd', null, false); $handler->withFile($this->start . 'ci-logo.png'); $image = $handler->getFile(); - $this->assertTrue($image instanceof Image); + $this->assertInstanceOf(Image::class, $image); $handler->withFile($this->start . 'ci-logo.jpeg'); $image = $handler->getFile(); - $this->assertTrue($image instanceof Image); + $this->assertInstanceOf(Image::class, $image); $handler->withFile($this->start . 'ci-logo.gif'); $image = $handler->getFile(); - $this->assertTrue($image instanceof Image); + $this->assertInstanceOf(Image::class, $image); } // Something handled by our Image diff --git a/tests/system/Images/ImageMagickHandlerTest.php b/tests/system/Images/ImageMagickHandlerTest.php index 02d02e305634..c79283b3f8fe 100644 --- a/tests/system/Images/ImageMagickHandlerTest.php +++ b/tests/system/Images/ImageMagickHandlerTest.php @@ -58,8 +58,8 @@ public function testGetVersion() // make sure that the call worked $this->assertNotFalse($version); // we should have a numeric version, greater than 6 - $this->assertTrue(version_compare($version, '6.0.0') >= 0); - $this->assertTrue(version_compare($version, '99.0.0') < 0); + $this->assertGreaterThanOrEqual(0, version_compare($version, '6.0.0')); + $this->assertLessThan(0, version_compare($version, '99.0.0')); } public function testImageProperties() diff --git a/tests/system/Language/LanguageTest.php b/tests/system/Language/LanguageTest.php index fcf4beea6257..0a501ea7947e 100644 --- a/tests/system/Language/LanguageTest.php +++ b/tests/system/Language/LanguageTest.php @@ -165,7 +165,7 @@ public function testLanguageFileLoading() $this->lang = new SecondMockLanguage('en'); $this->lang->loadem('More', 'en'); - $this->assertTrue(in_array('More', $this->lang->loaded(), true)); + $this->assertContains('More', $this->lang->loaded()); $this->lang->loadem('More', 'en'); $this->assertCount(1, $this->lang->loaded()); // should only be there once @@ -176,11 +176,11 @@ public function testLanguageFileLoadingReturns() $this->lang = new SecondMockLanguage('en'); $result = $this->lang->loadem('More', 'en', true); - $this->assertFalse(in_array('More', $this->lang->loaded(), true)); + $this->assertNotContains('More', $this->lang->loaded()); $this->assertCount(3, $result); $result = $this->lang->loadem('More', 'en'); - $this->assertTrue(in_array('More', $this->lang->loaded(), true)); + $this->assertContains('More', $this->lang->loaded()); $this->assertCount(1, $this->lang->loaded()); } diff --git a/tests/system/Log/LoggerTest.php b/tests/system/Log/LoggerTest.php index b092cb31041b..a97e16a51f1a 100644 --- a/tests/system/Log/LoggerTest.php +++ b/tests/system/Log/LoggerTest.php @@ -203,7 +203,7 @@ public function testLogInterpolatesFileAndLine() $logs = TestHandler::getLogs(); - $this->assertTrue(strpos($logs[0], $expected) > 1); + $this->assertGreaterThan(1, strpos($logs[0], $expected)); } public function testLogInterpolatesExceptions() @@ -222,7 +222,7 @@ public function testLogInterpolatesExceptions() $logs = TestHandler::getLogs(); $this->assertCount(1, $logs); - $this->assertTrue(strpos($logs[0], $expected) === 0); + $this->assertSame(0, strpos($logs[0], $expected)); } public function testEmergencyLogsCorrectly() diff --git a/tests/system/Models/InsertModelTest.php b/tests/system/Models/InsertModelTest.php index 521dec8fbadd..d08db6f87ac8 100644 --- a/tests/system/Models/InsertModelTest.php +++ b/tests/system/Models/InsertModelTest.php @@ -74,7 +74,7 @@ public function testInsertBatchValidationFail(): void $this->assertFalse($this->model->insertBatch($jobData)); $error = $this->model->errors(); - $this->assertTrue(isset($error['description'])); + $this->assertArrayHasKey('description', $error); } public function testInsertBatchSetsIntTimestamps(): void diff --git a/tests/system/Models/MiscellaneousModelTest.php b/tests/system/Models/MiscellaneousModelTest.php index e70ff51c4789..c513c023ab2c 100644 --- a/tests/system/Models/MiscellaneousModelTest.php +++ b/tests/system/Models/MiscellaneousModelTest.php @@ -93,7 +93,7 @@ public function testGetValidationMessagesForReplace(): void $this->assertFalse($this->model->replace($jobData)); $error = $this->model->errors(); - $this->assertTrue(isset($error['description'])); + $this->assertArrayHasKey('description', $error); } public function testUndefinedTypeInTransformDataToArray(): void diff --git a/tests/system/Models/UpdateModelTest.php b/tests/system/Models/UpdateModelTest.php index 888c019eca1e..5b6c9ca75972 100644 --- a/tests/system/Models/UpdateModelTest.php +++ b/tests/system/Models/UpdateModelTest.php @@ -153,7 +153,7 @@ public function testUpdateBatchValidationFail(): void $this->assertFalse($this->model->updateBatch($data, 'name')); $error = $this->model->errors(); - $this->assertTrue(isset($error['country'])); + $this->assertArrayHasKey('country', $error); } public function testUpdateBatchWithEntity(): void diff --git a/tests/system/Models/ValidationModelTest.php b/tests/system/Models/ValidationModelTest.php index b97532a09d68..0d60d36524ac 100644 --- a/tests/system/Models/ValidationModelTest.php +++ b/tests/system/Models/ValidationModelTest.php @@ -284,7 +284,7 @@ public function testRequiredWithValidationTrue(): void 'description' => 'just because we have to', ]; - $this->assertTrue($this->model->insert($data) !== false); + $this->assertNotFalse($this->model->insert($data)); } /** diff --git a/tests/system/Test/FeatureTestTraitTest.php b/tests/system/Test/FeatureTestTraitTest.php index 70952b89b96d..572ebe3d4244 100644 --- a/tests/system/Test/FeatureTestTraitTest.php +++ b/tests/system/Test/FeatureTestTraitTest.php @@ -366,7 +366,7 @@ public function testSetupRequestBodyWithParams() $request = $this->withBodyFormat('json')->setRequestBody($request, ['foo1' => 'bar1']); $this->assertJsonStringEqualsJsonString(json_encode(['foo1' => 'bar1']), $request->getBody()); - $this->assertTrue($request->header('Content-Type')->getValue() === 'application/json'); + $this->assertSame('application/json', $request->header('Content-Type')->getValue()); } public function testSetupRequestBodyWithXml() @@ -380,7 +380,7 @@ public function testSetupRequestBodyWithXml() '; $this->assertSame($expectedXml, $request->getBody()); - $this->assertTrue($request->header('Content-Type')->getValue() === 'application/xml'); + $this->assertSame('application/xml', $request->header('Content-Type')->getValue()); } public function testSetupRequestBodyWithBody() diff --git a/tests/system/Test/TestResponseTest.php b/tests/system/Test/TestResponseTest.php index 5bb3da21f1e7..cef63bd63af4 100644 --- a/tests/system/Test/TestResponseTest.php +++ b/tests/system/Test/TestResponseTest.php @@ -152,7 +152,7 @@ public function testAssertRedirectFail() { $this->getTestResponse('
Open
') !== false); - $this->assertTrue(strpos($content, 'Open
')); + $this->assertNotFalse(strpos($content, '