Skip to content

Commit

Permalink
feature PHP-CS-Fixer#1449 PhpUnitConstructFixer - Fix more use cases …
Browse files Browse the repository at this point in the history
…(SpacePossum)

This PR was squashed before being merged into the 1.11 branch (closes PHP-CS-Fixer#1449).

Discussion
----------

PhpUnitConstructFixer - Fix more use cases

Fix the following (Not) cases as well.

```php
<?php $this->assertNotSame(false, $a);
<?php $this->assertNotFalse($a);
```

```php
<?php $this->assertNotSame(true, $a);
<?php $this->assertNotTrue($a);
```

```php
<?php $this->assertNotSame(false, $a);
<?php $this->assertNotFalse($a);
```

```php
<?php $this->assertNotSame(true, $a);
<?php $this->assertNotTrue($a);
```
Replaces PHP-CS-Fixer#1380 (which was targeted at master and more of a mess).

Commits
-------

0cbf0ad PhpUnitConstructFixer - Fix more use cases
  • Loading branch information
keradus committed Oct 16, 2015
2 parents 1c67c1d + 0cbf0ad commit b7b14bb
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 48 deletions.
29 changes: 10 additions & 19 deletions Symfony/CS/Fixer/Contrib/PhpUnitConstructFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,27 +94,13 @@ public function getPriority()

private function fixAssertNegative(Tokens $tokens, $index, $method)
{
$sequence = $tokens->findSequence(
array(
array(T_VARIABLE, '$this'),
array(T_OBJECT_OPERATOR, '->'),
array(T_STRING, $method),
'(',
array(T_STRING, 'null'),
',',
),
$index
static $map = array(
'false' => 'assertNotFalse',
'null' => 'assertNotNull',
'true' => 'assertNotTrue',
);

if (null === $sequence) {
return;
}

$sequenceIndexes = array_keys($sequence);
$tokens[$sequenceIndexes[2]]->setContent('assertNotNull');
$tokens->clearRange($sequenceIndexes[4], $tokens->getNextNonWhitespace($sequenceIndexes[5]) - 1);

return $sequenceIndexes[5];
return $this->fixAssert($map, $tokens, $index, $method);
}

private function fixAssertPositive(Tokens $tokens, $index, $method)
Expand All @@ -125,6 +111,11 @@ private function fixAssertPositive(Tokens $tokens, $index, $method)
'true' => 'assertTrue',
);

return $this->fixAssert($map, $tokens, $index, $method);
}

private function fixAssert(array $map, Tokens $tokens, $index, $method)
{
$sequence = $tokens->findSequence(
array(
array(T_VARIABLE, '$this'),
Expand Down
52 changes: 23 additions & 29 deletions Symfony/CS/Tests/Fixer/Contrib/PhpUnitConstructFixerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,36 +62,8 @@ public function testFix($expected, $input = null)

public function provideTestFixCases()
{
return array(
$cases = array(
array('<?php $sth->assertSame(true, $foo);'),
array(
'<?php $this->assertTrue($a);',
'<?php $this->assertSame(true, $a);',
),
array(
'<?php $this->assertTrue($a , "true" . $bar);',
'<?php $this->assertSame(true , $a , "true" . $bar);',
),
array(
'<?php $this->assertFalse( $a, "false" . $bar);',
'<?php $this->assertSame( false, $a, "false" . $bar);',
),
array(
'<?php $this->assertNull( $a , "null" . $bar);',
'<?php $this->assertSame( null, $a , "null" . $bar);',
),
array(
'<?php $this->assertNotNull( $a , "notNull" . $bar);',
'<?php $this->assertNotSame( null, $a , "notNull" . $bar);',
),
array(
'<?php $this->assertFalse( $a, "false" . $bar);',
'<?php $this->assertEquals( false, $a, "false" . $bar);',
),
array(
'<?php $this->assertNotNull( $a , "notNull" . $bar);',
'<?php $this->assertNotEquals( null, $a , "notNull" . $bar);',
),
array(
'<?php
$this->assertTrue(
Expand All @@ -106,5 +78,27 @@ public function provideTestFixCases()
);',
),
);

return array_merge(
$cases,
$this->generateCases('<?php $this->assert%s%s($a); //%s %s', '<?php $this->assert%s(%s, $a); //%s %s'),
$this->generateCases('<?php $this->assert%s%s($a, "%s", "%s");', '<?php $this->assert%s(%s, $a, "%s", "%s");')
);
}

private function generateCases($expectedTemplate, $inputTemplate)
{
$cases = array();
$functionTypes = array('Same' => true, 'NotSame' => false, 'Equals' => true, 'NotEquals' => false);
foreach (array('true', 'false', 'null') as $type) {
foreach ($functionTypes as $method => $positive) {
$cases[] = array(
sprintf($expectedTemplate, $positive ? '' : 'Not', ucfirst($type), $method, $type),
sprintf($inputTemplate, $method, $type, $method, $type),
);
}
}

return $cases;
}
}

0 comments on commit b7b14bb

Please sign in to comment.