Skip to content

Commit 5508ef4

Browse files
authored
Merge pull request #5320 from kenjis/add-rectore-phpunit-rule
[Rector] add PHPUnitSetList::PHPUNIT_SPECIFIC_METHOD
2 parents 6e7df6e + c4f0fbb commit 5508ef4

27 files changed

+72
-56
lines changed

rector.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@
4242
use Rector\Php71\Rector\FuncCall\CountOnNullRector;
4343
use Rector\Php73\Rector\FuncCall\JsonThrowOnErrorRector;
4444
use Rector\Php73\Rector\FuncCall\StringifyStrNeedlesRector;
45+
use Rector\PHPUnit\Rector\MethodCall\AssertFalseStrposToContainsRector;
46+
use Rector\PHPUnit\Rector\MethodCall\AssertIssetToSpecificMethodRector;
47+
use Rector\PHPUnit\Set\PHPUnitSetList;
4548
use Rector\Set\ValueObject\LevelSetList;
4649
use Rector\Set\ValueObject\SetList;
4750
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
@@ -53,6 +56,7 @@
5356
return static function (ContainerConfigurator $containerConfigurator): void {
5457
$containerConfigurator->import(SetList::DEAD_CODE);
5558
$containerConfigurator->import(LevelSetList::UP_TO_PHP_73);
59+
$containerConfigurator->import(PHPUnitSetList::PHPUNIT_SPECIFIC_METHOD);
5660

5761
$parameters = $containerConfigurator->parameters();
5862

@@ -109,6 +113,18 @@
109113

110114
// use mt_rand instead of random_int on purpose on non-cryptographically random
111115
RandomFunctionRector::class,
116+
117+
// $this->assertTrue(isset($bar['foo']))
118+
// and $this->assertArrayHasKey('foo', $bar)
119+
// or $this->assertObjectHasAttribute('foo', $bar);
120+
// are not the same
121+
AssertIssetToSpecificMethodRector::class => [
122+
__DIR__ . '/tests/system/Entity/EntityTest.php',
123+
__DIR__ . '/tests/system/Session/SessionTest.php',
124+
],
125+
126+
// assertContains() to string can't be used in PHPUnit 9.1
127+
AssertFalseStrposToContainsRector::class,
112128
]);
113129

114130
// auto import fully qualified class names

tests/system/API/ResponseTraitTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -488,7 +488,7 @@ public function testXMLFormatter()
488488
$this->formatter = new XMLFormatter();
489489
$controller = $this->makeController();
490490

491-
$this->assertSame('CodeIgniter\Format\XMLFormatter', get_class($this->formatter));
491+
$this->assertInstanceOf('CodeIgniter\Format\XMLFormatter', $this->formatter);
492492

493493
$controller->respondCreated(['id' => 3], 'A Custom Reason');
494494
$expected = <<<'EOH'

tests/system/CLI/ConsoleTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public function testHeader()
6767
$console = new Console($this->app);
6868
$console->showHeader();
6969
$result = CITestStreamFilter::$buffer;
70-
$this->assertTrue(strpos($result, sprintf('CodeIgniter v%s Command Line Tool', CodeIgniter::CI_VERSION)) > 0);
70+
$this->assertGreaterThan(0, strpos($result, sprintf('CodeIgniter v%s Command Line Tool', CodeIgniter::CI_VERSION)));
7171
}
7272

7373
public function testNoHeader()

tests/system/CommonSingleServiceTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public function testSingleServiceWithNoParamsSupplied(string $service): void
3434
$service1 = single_service($service);
3535
$service2 = single_service($service);
3636

37-
$this->assertSame(get_class($service1), get_class($service2));
37+
$this->assertInstanceOf(get_class($service1), $service2);
3838
$this->assertNotSame($service1, $service2);
3939
}
4040

@@ -62,7 +62,7 @@ public function testSingleServiceWithAtLeastOneParamSupplied(string $service): v
6262
$service1 = single_service($service, ...$params);
6363
$service2 = single_service($service, ...$params);
6464

65-
$this->assertSame(get_class($service1), get_class($service2));
65+
$this->assertInstanceOf(get_class($service1), $service2);
6666
$this->assertNotSame($service1, $service2);
6767

6868
if ($service === 'commands') {
@@ -77,7 +77,7 @@ public function testSingleServiceWithAllParamsSupplied(): void
7777

7878
// Assert that even passing true as last param this will
7979
// not create a shared instance.
80-
$this->assertSame(get_class($cache1), get_class($cache2));
80+
$this->assertInstanceOf(get_class($cache1), $cache2);
8181
$this->assertNotSame($cache1, $cache2);
8282
}
8383

tests/system/Config/ConfigTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public function testCreateSharedInstance()
4141
$Config = Config::get('DocTypes');
4242
$Config2 = Config::get('Config\\DocTypes');
4343

44-
$this->assertTrue($Config === $Config2);
44+
$this->assertSame($Config2, $Config);
4545
}
4646

4747
public function testCreateNonConfig()

tests/system/Config/ServicesTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,7 @@ public function testReset()
316316
$response2 = service('response');
317317
$this->assertInstanceOf(MockResponse::class, $response2);
318318

319-
$this->assertTrue($response !== $response2);
319+
$this->assertNotSame($response2, $response);
320320
}
321321

322322
/**

tests/system/Cookie/CookieTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -268,13 +268,13 @@ public function testArrayAccessOfCookie(): void
268268
{
269269
$cookie = new Cookie('cookie', 'monster');
270270

271-
$this->assertTrue(isset($cookie['expire']));
271+
$this->assertArrayHasKey('expire', $cookie);
272272
$this->assertSame($cookie['expire'], $cookie->getExpiresTimestamp());
273-
$this->assertTrue(isset($cookie['httponly']));
273+
$this->assertArrayHasKey('httponly', $cookie);
274274
$this->assertSame($cookie['httponly'], $cookie->isHTTPOnly());
275-
$this->assertTrue(isset($cookie['samesite']));
275+
$this->assertArrayHasKey('samesite', $cookie);
276276
$this->assertSame($cookie['samesite'], $cookie->getSameSite());
277-
$this->assertTrue(isset($cookie['path']));
277+
$this->assertArrayHasKey('path', $cookie);
278278
$this->assertSame($cookie['path'], $cookie->getPath());
279279

280280
$this->expectException(InvalidArgumentException::class);

tests/system/Database/Live/ConnectTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,6 @@ public function testConnectWithFailover()
103103
$db1 = Database::connect($this->tests);
104104

105105
$this->assertSame($this->tests['failover'][0]['DBDriver'], $this->getPrivateProperty($db1, 'DBDriver'));
106-
$this->assertTrue(count($db1->listTables()) >= 0);
106+
$this->assertGreaterThanOrEqual(0, count($db1->listTables()));
107107
}
108108
}

tests/system/Database/Live/DbUtilsTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ public function testUtilsListDatabases()
7979
if (in_array($this->db->DBDriver, ['MySQLi', 'Postgre', 'SQLSRV'], true)) {
8080
$databases = $util->listDatabases();
8181

82-
$this->assertTrue(in_array($this->db->getDatabase(), $databases, true));
82+
$this->assertContains($this->db->getDatabase(), $databases);
8383
} elseif ($this->db->DBDriver === 'SQLite3') {
8484
$this->expectException(DatabaseException::class);
8585
$this->expectExceptionMessage('Unsupported feature of the database platform you are using.');

tests/system/Database/Live/GetTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,8 @@ public function testGetFieldNames()
7878
{
7979
$jobs = $this->db->table('job')->get()->getFieldNames();
8080

81-
$this->assertTrue(in_array('name', $jobs, true));
82-
$this->assertTrue(in_array('description', $jobs, true));
81+
$this->assertContains('name', $jobs);
82+
$this->assertContains('description', $jobs);
8383
}
8484

8585
public function testGetFieldData()

tests/system/Database/Live/PretendTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public function testPretendReturnsQueryObject()
3636
->table('user')
3737
->get();
3838

39-
$this->assertFalse($result instanceof Query);
39+
$this->assertNotInstanceOf(Query::class, $result);
4040

4141
$result = $this->db->pretend(true)
4242
->table('user')

tests/system/Database/Live/SQLite/AlterTableTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -133,9 +133,9 @@ public function testDropColumnSuccess()
133133

134134
$columns = $this->db->getFieldNames('foo');
135135

136-
$this->assertFalse(in_array('name', $columns, true));
137-
$this->assertTrue(in_array('id', $columns, true));
138-
$this->assertTrue(in_array('email', $columns, true));
136+
$this->assertNotContains('name', $columns);
137+
$this->assertContains('id', $columns);
138+
$this->assertContains('email', $columns);
139139
}
140140

141141
public function testDropColumnMaintainsKeys()

tests/system/Filters/FiltersTest.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -553,7 +553,7 @@ public function testShortCircuit()
553553
$uri = 'admin/foo/bar';
554554
$response = $filters->run($uri, 'before');
555555

556-
$this->assertTrue($response instanceof ResponseInterface);
556+
$this->assertInstanceOf(ResponseInterface::class, $response);
557557
$this->assertSame('http://google.com', $response->csp);
558558
}
559559

@@ -737,7 +737,7 @@ public function testAddFilter()
737737
$filters = $filters->initialize('admin/foo/bar');
738738
$filters = $filters->getFilters();
739739

740-
$this->assertTrue(in_array('some_alias', $filters['before'], true));
740+
$this->assertContains('some_alias', $filters['before']);
741741
}
742742

743743
public function testAddFilterSection()
@@ -753,7 +753,7 @@ public function testAddFilterSection()
753753
->initialize('admin/foo/bar')
754754
->getFilters();
755755

756-
$this->assertTrue(in_array('another', $list['before'], true));
756+
$this->assertContains('another', $list['before']);
757757
}
758758

759759
public function testInitializeTwice()
@@ -770,7 +770,7 @@ public function testInitializeTwice()
770770
->initialize()
771771
->getFilters();
772772

773-
$this->assertTrue(in_array('another', $list['before'], true));
773+
$this->assertContains('another', $list['before']);
774774
}
775775

776776
public function testEnableFilter()
@@ -791,7 +791,7 @@ public function testEnableFilter()
791791
$filters->enableFilter('google', 'before');
792792
$filters = $filters->getFilters();
793793

794-
$this->assertTrue(in_array('google', $filters['before'], true));
794+
$this->assertContains('google', $filters['before']);
795795
}
796796

797797
public function testEnableFilterWithArguments()
@@ -813,7 +813,7 @@ public function testEnableFilterWithArguments()
813813
$filters->enableFilter('role:admin , super', 'after');
814814
$found = $filters->getFilters();
815815

816-
$this->assertTrue(in_array('role', $found['before'], true));
816+
$this->assertContains('role', $found['before']);
817817
$this->assertSame(['admin', 'super'], $filters->getArguments('role'));
818818
$this->assertSame(['role' => ['admin', 'super']], $filters->getArguments());
819819

@@ -845,7 +845,7 @@ public function testEnableFilterWithNoArguments()
845845
$filters->enableFilter('role', 'after');
846846
$found = $filters->getFilters();
847847

848-
$this->assertTrue(in_array('role', $found['before'], true));
848+
$this->assertContains('role', $found['before']);
849849

850850
$response = $filters->run('admin/foo/bar', 'before');
851851

tests/system/HTTP/IncomingRequestTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -544,8 +544,8 @@ public function testWithFalseBody()
544544
// Use `false` here to simulate file_get_contents returning a false value
545545
$request = new IncomingRequest(new App(), new URI(), false, new UserAgent());
546546

547-
$this->assertTrue($request->getBody() !== false);
548-
$this->assertTrue($request->getBody() === null);
547+
$this->assertNotFalse($request->getBody());
548+
$this->assertNull($request->getBody());
549549
}
550550

551551
/**

tests/system/HTTP/ResponseTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public function testConstructWithCSPEnabled()
6363
$config->CSPEnabled = true;
6464
$response = new Response($config);
6565

66-
$this->assertTrue($response instanceof Response);
66+
$this->assertInstanceOf(Response::class, $response);
6767
}
6868

6969
public function testSetStatusCodeSetsReason()
@@ -327,7 +327,7 @@ public function testJSONWithArray()
327327
$response->setJSON($body);
328328

329329
$this->assertSame($expected, $response->getJSON());
330-
$this->assertTrue(strpos($response->getHeaderLine('content-type'), 'application/json') !== false);
330+
$this->assertNotFalse(strpos($response->getHeaderLine('content-type'), 'application/json'));
331331
}
332332

333333
public function testJSONGetFromNormalBody()
@@ -364,7 +364,7 @@ public function testXMLWithArray()
364364
$response->setXML($body);
365365

366366
$this->assertSame($expected, $response->getXML());
367-
$this->assertTrue(strpos($response->getHeaderLine('content-type'), 'application/xml') !== false);
367+
$this->assertNotFalse(strpos($response->getHeaderLine('content-type'), 'application/xml'));
368368
}
369369

370370
public function testXMLGetFromNormalBody()

tests/system/I18n/TimeTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1023,7 +1023,7 @@ public function testCreateFromInstance()
10231023
{
10241024
$datetime = new DateTime();
10251025
$time = Time::createFromInstance($datetime);
1026-
$this->assertTrue($time instanceof Time);
1026+
$this->assertInstanceOf(Time::class, $time);
10271027
$this->assertTrue($time->sameAs($datetime));
10281028
}
10291029

tests/system/Images/BaseHandlerTest.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ protected function setUp(): void
5858
public function testNew()
5959
{
6060
$handler = Services::image('gd', null, false);
61-
$this->assertTrue($handler instanceof BaseHandler);
61+
$this->assertInstanceOf(BaseHandler::class, $handler);
6262
}
6363

6464
public function testWithFile()
@@ -68,7 +68,7 @@ public function testWithFile()
6868
$handler->withFile($path);
6969

7070
$image = $handler->getFile();
71-
$this->assertTrue($image instanceof Image);
71+
$this->assertInstanceOf(Image::class, $image);
7272
$this->assertSame(155, $image->origWidth);
7373
$this->assertSame($path, $image->getPathname());
7474
}
@@ -104,15 +104,15 @@ public function testFileTypes()
104104
$handler = Services::image('gd', null, false);
105105
$handler->withFile($this->start . 'ci-logo.png');
106106
$image = $handler->getFile();
107-
$this->assertTrue($image instanceof Image);
107+
$this->assertInstanceOf(Image::class, $image);
108108

109109
$handler->withFile($this->start . 'ci-logo.jpeg');
110110
$image = $handler->getFile();
111-
$this->assertTrue($image instanceof Image);
111+
$this->assertInstanceOf(Image::class, $image);
112112

113113
$handler->withFile($this->start . 'ci-logo.gif');
114114
$image = $handler->getFile();
115-
$this->assertTrue($image instanceof Image);
115+
$this->assertInstanceOf(Image::class, $image);
116116
}
117117

118118
// Something handled by our Image

tests/system/Images/ImageMagickHandlerTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,8 @@ public function testGetVersion()
5858
// make sure that the call worked
5959
$this->assertNotFalse($version);
6060
// we should have a numeric version, greater than 6
61-
$this->assertTrue(version_compare($version, '6.0.0') >= 0);
62-
$this->assertTrue(version_compare($version, '99.0.0') < 0);
61+
$this->assertGreaterThanOrEqual(0, version_compare($version, '6.0.0'));
62+
$this->assertLessThan(0, version_compare($version, '99.0.0'));
6363
}
6464

6565
public function testImageProperties()

tests/system/Language/LanguageTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ public function testLanguageFileLoading()
165165
$this->lang = new SecondMockLanguage('en');
166166

167167
$this->lang->loadem('More', 'en');
168-
$this->assertTrue(in_array('More', $this->lang->loaded(), true));
168+
$this->assertContains('More', $this->lang->loaded());
169169

170170
$this->lang->loadem('More', 'en');
171171
$this->assertCount(1, $this->lang->loaded()); // should only be there once
@@ -176,11 +176,11 @@ public function testLanguageFileLoadingReturns()
176176
$this->lang = new SecondMockLanguage('en');
177177

178178
$result = $this->lang->loadem('More', 'en', true);
179-
$this->assertFalse(in_array('More', $this->lang->loaded(), true));
179+
$this->assertNotContains('More', $this->lang->loaded());
180180
$this->assertCount(3, $result);
181181

182182
$result = $this->lang->loadem('More', 'en');
183-
$this->assertTrue(in_array('More', $this->lang->loaded(), true));
183+
$this->assertContains('More', $this->lang->loaded());
184184
$this->assertCount(1, $this->lang->loaded());
185185
}
186186

tests/system/Log/LoggerTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ public function testLogInterpolatesFileAndLine()
203203

204204
$logs = TestHandler::getLogs();
205205

206-
$this->assertTrue(strpos($logs[0], $expected) > 1);
206+
$this->assertGreaterThan(1, strpos($logs[0], $expected));
207207
}
208208

209209
public function testLogInterpolatesExceptions()
@@ -222,7 +222,7 @@ public function testLogInterpolatesExceptions()
222222
$logs = TestHandler::getLogs();
223223

224224
$this->assertCount(1, $logs);
225-
$this->assertTrue(strpos($logs[0], $expected) === 0);
225+
$this->assertSame(0, strpos($logs[0], $expected));
226226
}
227227

228228
public function testEmergencyLogsCorrectly()

tests/system/Models/InsertModelTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public function testInsertBatchValidationFail(): void
7474
$this->assertFalse($this->model->insertBatch($jobData));
7575

7676
$error = $this->model->errors();
77-
$this->assertTrue(isset($error['description']));
77+
$this->assertArrayHasKey('description', $error);
7878
}
7979

8080
public function testInsertBatchSetsIntTimestamps(): void

tests/system/Models/MiscellaneousModelTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ public function testGetValidationMessagesForReplace(): void
9393
$this->assertFalse($this->model->replace($jobData));
9494

9595
$error = $this->model->errors();
96-
$this->assertTrue(isset($error['description']));
96+
$this->assertArrayHasKey('description', $error);
9797
}
9898

9999
public function testUndefinedTypeInTransformDataToArray(): void

tests/system/Models/UpdateModelTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ public function testUpdateBatchValidationFail(): void
153153
$this->assertFalse($this->model->updateBatch($data, 'name'));
154154

155155
$error = $this->model->errors();
156-
$this->assertTrue(isset($error['country']));
156+
$this->assertArrayHasKey('country', $error);
157157
}
158158

159159
public function testUpdateBatchWithEntity(): void

0 commit comments

Comments
 (0)