Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[PHPUnit 10] Rename assertObjectHasAttribute() to assertObjectHasProperty(), add AssertIssetToAssertObjectHasPropertyRector #338

Merged
merged 6 commits into from
Jun 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 0 additions & 11 deletions config/sets/level/deprecated-level-set.php

This file was deleted.

8 changes: 6 additions & 2 deletions config/sets/phpunit100.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
use Rector\PHPUnit\PHPUnit100\Rector\Class_\AddProphecyTraitRector;
use Rector\PHPUnit\PHPUnit100\Rector\Class_\PublicDataProviderClassMethodRector;
use Rector\PHPUnit\PHPUnit100\Rector\Class_\StaticDataProviderClassMethodRector;
use Rector\PHPUnit\PHPUnit100\Rector\MethodCall\PropertyExistsWithoutAssertRector;
use Rector\PHPUnit\PHPUnit100\Rector\MethodCall\AssertIssetToAssertObjectHasPropertyRector;
use Rector\PHPUnit\PHPUnit100\Rector\MethodCall\RemoveSetMethodsMethodCallRector;
use Rector\PHPUnit\Rector\StmtsAwareInterface\WithConsecutiveRector;
use Rector\PHPUnit\Set\PHPUnitSetList;
Expand All @@ -17,9 +17,9 @@
$rectorConfig->sets([PHPUnitSetList::ANNOTATIONS_TO_ATTRIBUTES]);

$rectorConfig->rules([
AssertIssetToAssertObjectHasPropertyRector::class,
StaticDataProviderClassMethodRector::class,
PublicDataProviderClassMethodRector::class,
PropertyExistsWithoutAssertRector::class,
AddProphecyTraitRector::class,
WithConsecutiveRector::class,
RemoveSetMethodsMethodCallRector::class,
Expand All @@ -29,6 +29,10 @@
// https://github.com/sebastianbergmann/phpunit/issues/4087
new MethodCallRename('PHPUnit\Framework\Assert', 'assertRegExp', 'assertMatchesRegularExpression'),

// https://github.com/sebastianbergmann/phpunit/issues/5220
new MethodCallRename('PHPUnit\Framework\Assert', 'assertObjectHasAttribute', 'assertObjectHasProperty'),
new MethodCallRename('PHPUnit\Framework\Assert', 'assertObjectNotHasAttribute', 'assertObjectHasNotProperty'),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


new MethodCallRename(
'PHPUnit\Framework\MockObject\Rule\InvocationOrder',
'getInvocationCount',
Expand Down
10 changes: 10 additions & 0 deletions config/sets/phpunit110.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

use Rector\Config\RectorConfig;
use Rector\PHPUnit\PHPUnit110\Rector\Class_\NamedArgumentForDataProviderRector;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->rules([NamedArgumentForDataProviderRector::class]);
};
44 changes: 22 additions & 22 deletions docs/rector_rules_overview.md
Original file line number Diff line number Diff line change
Expand Up @@ -270,22 +270,37 @@ Turns instanceof comparisons to their method name alternatives in PHPUnit TestCa

<br>

## AssertIssetToSpecificMethodRector
## AssertIssetToAssertObjectHasPropertyRector

Turns isset comparisons to their method name alternatives in PHPUnit TestCase
Change `"isset()"` property check, to `assertObjectHasProperty()` method

- class: [`Rector\PHPUnit\CodeQuality\Rector\MethodCall\AssertIssetToSpecificMethodRector`](../rules/CodeQuality/Rector/MethodCall/AssertIssetToSpecificMethodRector.php)
- class: [`Rector\PHPUnit\PHPUnit100\Rector\MethodCall\AssertIssetToAssertObjectHasPropertyRector`](../rules/PHPUnit100/Rector/MethodCall/AssertIssetToAssertObjectHasPropertyRector.php)

```diff
-$this->assertTrue(isset($anything->foo));
+$this->assertObjectHasAttribute("foo", $anything);
use PHPUnit\Framework\TestCase;

final class SomeTest extends TestCase
{
public function test()
{
$object = new stdClass();
- $this->assertTrue(isset($object->someProperty));
+ $this->assertObjectHasProperty('someProperty', $object);
}
}
```

<br>

## AssertIssetToSpecificMethodRector

Turns `assertTrue()` + `isset()` comparisons to more precise `assertArrayHasKey()` method

- class: [`Rector\PHPUnit\CodeQuality\Rector\MethodCall\AssertIssetToSpecificMethodRector`](../rules/CodeQuality/Rector/MethodCall/AssertIssetToSpecificMethodRector.php)

```diff
-$this->assertFalse(isset($anything["foo"]), "message");
+$this->assertArrayNotHasKey("foo", $anything, "message");
-$this->assertTrue(isset($anything["foo"]), "message");
+$this->assertArrayHasKey("foo", $anything, "message");
```

<br>
Expand Down Expand Up @@ -751,21 +766,6 @@ Changes PHPUnit calls from self::assert*() to `$this->assert*()`

<br>

## PropertyExistsWithoutAssertRector

Turns PHPUnit TestCase assertObjectHasAttribute into `property_exists` comparisons

- class: [`Rector\PHPUnit\PHPUnit100\Rector\MethodCall\PropertyExistsWithoutAssertRector`](../rules/PHPUnit100/Rector/MethodCall/PropertyExistsWithoutAssertRector.php)

```diff
-$this->assertClassHasAttribute("property", "Class");
-$this->assertClassNotHasAttribute("property", "Class");
+$this->assertFalse(property_exists(new Class, "property"));
+$this->assertTrue(property_exists(new Class, "property"));
```

<br>

## PublicDataProviderClassMethodRector

Change data provider methods to public
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ final class MyIssetTest extends \PHPUnit\Framework\TestCase
{
public function test()
{
$this->assertTrue(isset($node->value1));
$this->assertFalse(isset($node->value2), 'message');
$this->assertTrue(isset($node['value1']), 'message');
$this->assertFalse(isset($node['value2']));
}
Expand All @@ -23,8 +21,6 @@ final class MyIssetTest extends \PHPUnit\Framework\TestCase
{
public function test()
{
$this->assertObjectHasAttribute('value1', $node);
$this->assertObjectNotHasAttribute('value2', $node, 'message');
$this->assertArrayHasKey('value1', $node, 'message');
$this->assertArrayNotHasKey('value2', $node);
}
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace Rector\PHPUnit\Tests\CodeQuality\Rector\MethodCall\AssertIssetToSpecificMethodRector\Fixture;

final class SkipProperty extends \PHPUnit\Framework\TestCase
{
public function test()
{
$this->assertTrue(isset($node->value1));
$this->assertFalse(isset($node->value2), 'message');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

declare(strict_types=1);

namespace Rector\PHPUnit\Tests\PHPUnit100\Rector\MethodCall\PropertyExistsWithoutAssertRector;
namespace Rector\PHPUnit\Tests\PHPUnit100\Rector\MethodCall\AssertIssetToAssertObjectHasPropertyRector;

use Iterator;
use PHPUnit\Framework\Attributes\DataProvider;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;

final class PropertyExistsWithoutAssertRectorTest extends AbstractRectorTestCase
final class AssertIssetToAssertObjectHasPropertyRectorTest extends AbstractRectorTestCase
{
#[DataProvider('provideData')]
public function test(string $filePath): void
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
<?php

namespace Rector\PHPUnit\Tests\CodeQuality\Rector\MethodCall\AssertIssetToSpecificMethodRector\Fixture;
namespace Rector\PHPUnit\Tests\PHPUnit100\Rector\MethodCall\AssertIssetToAssertObjectHasPropertyRector\Fixture;

final class CustomIsset {
final class CustomIsset
{
public function __isset($property) {
return false;
}
}

final class SkipCustomIsset extends \PHPUnit\Framework\TestCase
final class SkipMagicIsset extends \PHPUnit\Framework\TestCase
{
public function test()
{
$foo = new CustomIsset();
$this->assertTrue(isset($foo->bar));
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace Rector\PHPUnit\Tests\PHPUnit100\Rector\MethodCall\AssertIssetToAssertObjectHasPropertyRector\Fixture;

use PHPUnit\Framework\TestCase;

final class SomeIssetToProperty extends TestCase
{
public function test()
{
$object = new \stdClass();
$this->assertTrue(isset($object->someProperty));
}
}

?>
-----
<?php

namespace Rector\PHPUnit\Tests\PHPUnit100\Rector\MethodCall\AssertIssetToAssertObjectHasPropertyRector\Fixture;

use PHPUnit\Framework\TestCase;

final class SomeIssetToProperty extends TestCase
{
public function test()
{
$object = new \stdClass();
$this->assertObjectHasAttribute('someProperty', $object);
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

declare(strict_types=1);

use Rector\Config\RectorConfig;
use Rector\PHPUnit\PHPUnit100\Rector\MethodCall\AssertIssetToAssertObjectHasPropertyRector;

return RectorConfig::configure()
->withRules([AssertIssetToAssertObjectHasPropertyRector::class]);

This file was deleted.

This file was deleted.

This file was deleted.

Loading
Loading