|
| 1 | +<?php declare(strict_types = 1); |
| 2 | + |
| 3 | +namespace PHPStan\Rule\Nette; |
| 4 | + |
| 5 | +use PhpParser\Node; |
| 6 | +use PHPStan\Analyser\Scope; |
| 7 | +use PHPStan\Broker\Broker; |
| 8 | +use PHPStan\Reflection\ClassReflection; |
| 9 | + |
| 10 | +class DoNotExtendNetteObjectRuleTest extends \PHPUnit\Framework\TestCase |
| 11 | +{ |
| 12 | + |
| 13 | + /** @var \PHPStan\Rule\Nette\DoNotExtendNetteObjectRule */ |
| 14 | + private $rule; |
| 15 | + |
| 16 | + protected function setUp() |
| 17 | + { |
| 18 | + $broker = $this->createMock(Broker::class); |
| 19 | + $broker->method('hasClass')->will($this->returnValue(true)); |
| 20 | + $broker->method('getClass')->will($this->returnCallback(function (string $className) { |
| 21 | + $nativeReflection = new \ReflectionClass($className); |
| 22 | + $classReflection = $this->createMock(ClassReflection::class); |
| 23 | + $classReflection->method('getNativeReflection')->will($this->returnValue($nativeReflection)); |
| 24 | + return $classReflection; |
| 25 | + })); |
| 26 | + |
| 27 | + $this->rule = new DoNotExtendNetteObjectRule($broker); |
| 28 | + } |
| 29 | + |
| 30 | + public function testSmartObjectChild() |
| 31 | + { |
| 32 | + $scope = $this->createMock(Scope::class); |
| 33 | + $node = $this->createMock(Node::class); |
| 34 | + $node->namespacedName = 'PHPStan\Tests\SmartObjectChild'; |
| 35 | + |
| 36 | + $result = $this->rule->processNode($node, $scope); |
| 37 | + |
| 38 | + $this->assertEmpty($result); |
| 39 | + } |
| 40 | + |
| 41 | + public function testNetteObjectChild() |
| 42 | + { |
| 43 | + if (PHP_VERSION_ID >= 70200) { |
| 44 | + $this->markTestSkipped('PHP 7.2 is incompatible with Nette\Object.'); |
| 45 | + } |
| 46 | + $scope = $this->createMock(Scope::class); |
| 47 | + $node = $this->createMock(Node::class); |
| 48 | + $node->namespacedName = 'PHPStan\Tests\NetteObjectChild'; |
| 49 | + |
| 50 | + $result = $this->rule->processNode($node, $scope); |
| 51 | + |
| 52 | + $this->assertSame(['Class PHPStan\Tests\NetteObjectChild extends Nette\Object - it\'s better to use Nette\SmartObject trait.'], $result); |
| 53 | + } |
| 54 | + |
| 55 | +} |
0 commit comments