Skip to content

Commit 9f0b614

Browse files
committed
Simplifying tests thanks to PHPStan's TestCase available as part of src/ directory in dist package
1 parent 8b7a598 commit 9f0b614

File tree

6 files changed

+28
-102
lines changed

6 files changed

+28
-102
lines changed

phpstan.neon

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@ includes:
55

66
parameters:
77
excludes_analyse:
8-
- %rootDir%/../../../tests/notAutoloaded/*
8+
- %rootDir%/../../../tests/data/*

tests/Reflection/Nette/HtmlClassReflectionExtensionTest.php

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,18 @@
22

33
namespace PHPStan\Reflection\Nette;
44

5-
use PHPStan\Reflection\ClassReflection;
6-
7-
class HtmlClassReflectionExtensionTest extends \PHPUnit\Framework\TestCase
5+
class HtmlClassReflectionExtensionTest extends \PHPStan\Testing\TestCase
86
{
97

8+
/** @var \PHPStan\Broker\Broker */
9+
private $broker;
10+
1011
/** @var \PHPStan\Reflection\Nette\HtmlClassReflectionExtension */
1112
private $extension;
1213

1314
protected function setUp()
1415
{
16+
$this->broker = $this->createBroker();
1517
$this->extension = new HtmlClassReflectionExtension();
1618
}
1719

@@ -39,15 +41,13 @@ public function dataHasMethod(): array
3941
*/
4042
public function testHasMethod(string $className, bool $result)
4143
{
42-
$classReflection = $this->createMock(ClassReflection::class);
43-
$classReflection->method('getName')->will($this->returnValue($className));
44+
$classReflection = $this->broker->getClass($className);
4445
$this->assertSame($result, $this->extension->hasMethod($classReflection, 'href'));
4546
}
4647

4748
public function testGetMethod()
4849
{
49-
$classReflection = $this->createMock(ClassReflection::class);
50-
$classReflection->method('getName')->will($this->returnValue(\Nette\Utils\Html::class));
50+
$classReflection = $this->broker->getClass(\Nette\Utils\Html::class);
5151
$methodReflection = $this->extension->getMethod($classReflection, 'href');
5252
$this->assertSame('href', $methodReflection->getName());
5353
$this->assertSame($classReflection, $methodReflection->getDeclaringClass());
@@ -83,15 +83,13 @@ public function dataHasProperty(): array
8383
*/
8484
public function testHasProperty(string $className, bool $result)
8585
{
86-
$classReflection = $this->createMock(ClassReflection::class);
87-
$classReflection->method('getName')->will($this->returnValue($className));
86+
$classReflection = $this->broker->getClass($className);
8887
$this->assertSame($result, $this->extension->hasProperty($classReflection, 'href'));
8988
}
9089

9190
public function testGetProperty()
9291
{
93-
$classReflection = $this->createMock(ClassReflection::class);
94-
$classReflection->method('getName')->will($this->returnValue(\Nette\Utils\Html::class));
92+
$classReflection = $this->broker->getClass(\Nette\Utils\Html::class);
9593
$propertyReflection = $this->extension->getProperty($classReflection, 'href');
9694
$this->assertSame($classReflection, $propertyReflection->getDeclaringClass());
9795
$this->assertFalse($propertyReflection->isStatic());

tests/Reflection/Nette/NetteObjectClassReflectionExtensionTest.php

Lines changed: 7 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,18 @@
22

33
namespace PHPStan\Reflection\Nette;
44

5-
use PHPStan\Reflection\ClassReflection;
6-
use PHPStan\Reflection\Php\PhpMethodReflection;
7-
use PHPStan\Reflection\Php\PhpPropertyReflection;
8-
9-
class NetteObjectClassReflectionExtensionTest extends \PHPUnit\Framework\TestCase
5+
class NetteObjectClassReflectionExtensionTest extends \PHPStan\Testing\TestCase
106
{
117

8+
/** @var \PHPStan\Broker\Broker */
9+
private $broker;
10+
1211
/** @var \PHPStan\Reflection\Nette\NetteObjectClassReflectionExtension */
1312
private $extension;
1413

1514
protected function setUp()
1615
{
16+
$this->broker = $this->createBroker();
1717
$this->extension = new NetteObjectClassReflectionExtension();
1818
}
1919

@@ -56,7 +56,7 @@ public function dataHasMethod(): array
5656
*/
5757
public function testHasMethod(string $className, string $method, bool $result)
5858
{
59-
$classReflection = $this->mockClassReflection(new \ReflectionClass($className));
59+
$classReflection = $this->broker->getClass($className);
6060
$this->assertSame($result, $this->extension->hasMethod($classReflection, $method));
6161
}
6262

@@ -99,59 +99,8 @@ public function dataHasProperty(): array
9999
*/
100100
public function testHasProperty(string $className, string $property, bool $result)
101101
{
102-
$classReflection = $this->mockClassReflection(new \ReflectionClass($className));
102+
$classReflection = $this->broker->getClass($className);
103103
$this->assertSame($result, $this->extension->hasProperty($classReflection, $property));
104104
}
105105

106-
private function mockClassReflection(\ReflectionClass $class): ClassReflection
107-
{
108-
$classReflection = $this->createMock(ClassReflection::class);
109-
$classReflection->method('getNativeReflection')->will($this->returnValue($class));
110-
$classReflection->method('hasNativeProperty')->will(
111-
$this->returnCallback(
112-
function (string $property) use ($class): bool {
113-
return $class->hasProperty($property);
114-
}
115-
)
116-
);
117-
$classReflection->method('getNativeProperty')->will(
118-
$this->returnCallback(
119-
function (string $property) use ($class): PhpPropertyReflection {
120-
return $this->mockPropertyReflection($class->getProperty($property));
121-
}
122-
)
123-
);
124-
$classReflection->method('hasNativeMethod')->will(
125-
$this->returnCallback(
126-
function (string $method) use ($class): bool {
127-
return $class->hasMethod($method);
128-
}
129-
)
130-
);
131-
$classReflection->method('getNativeMethod')->will(
132-
$this->returnCallback(
133-
function (string $method) use ($class): PhpMethodReflection {
134-
return $this->mockMethodReflection($class->getMethod($method));
135-
}
136-
)
137-
);
138-
139-
return $classReflection;
140-
}
141-
142-
private function mockMethodReflection(\ReflectionMethod $method): PhpMethodReflection
143-
{
144-
$methodReflection = $this->createMock(PhpMethodReflection::class);
145-
$methodReflection->method('isPublic')->will($this->returnValue($method->isPublic()));
146-
$methodReflection->method('isStatic')->will($this->returnValue($method->isStatic()));
147-
return $methodReflection;
148-
}
149-
150-
private function mockPropertyReflection(\ReflectionProperty $property): PhpPropertyReflection
151-
{
152-
$propertyReflection = $this->createMock(PhpPropertyReflection::class);
153-
$propertyReflection->method('isPublic')->will($this->returnValue($property->isPublic()));
154-
return $propertyReflection;
155-
}
156-
157106
}

tests/Rule/Nette/DoNotExtendNetteObjectRuleTest.php

Lines changed: 11 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2,54 +2,33 @@
22

33
namespace PHPStan\Rule\Nette;
44

5-
use PhpParser\Node;
6-
use PHPStan\Analyser\Scope;
7-
use PHPStan\Broker\Broker;
8-
use PHPStan\Reflection\ClassReflection;
5+
use PHPStan\Rules\Rule;
96

10-
class DoNotExtendNetteObjectRuleTest extends \PHPUnit\Framework\TestCase
7+
class DoNotExtendNetteObjectRuleTest extends \PHPStan\Testing\RuleTestCase
118
{
129

13-
/** @var \PHPStan\Rule\Nette\DoNotExtendNetteObjectRule */
14-
private $rule;
15-
16-
protected function setUp()
10+
protected function getRule(): Rule
1711
{
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);
12+
return new DoNotExtendNetteObjectRule($this->createBroker());
2813
}
2914

3015
public function testSmartObjectChild()
3116
{
32-
$scope = $this->createMock(Scope::class);
33-
$node = $this->createMock(Node\Stmt\Class_::class);
34-
$node->namespacedName = new Node\Name('PHPStan\Tests\SmartObjectChild');
35-
36-
$result = $this->rule->processNode($node, $scope);
37-
38-
$this->assertEmpty($result);
17+
$this->analyse([__DIR__ . '/../../data/SmartObjectChild.php'], []);
3918
}
4019

4120
public function testNetteObjectChild()
4221
{
4322
if (PHP_VERSION_ID >= 70200) {
4423
$this->markTestSkipped('PHP 7.2 is incompatible with Nette\Object.');
4524
}
46-
$scope = $this->createMock(Scope::class);
47-
$node = $this->createMock(Node\Stmt\Class_::class);
48-
$node->namespacedName = new Node\Name('PHPStan\Tests\NetteObjectChild');
49-
50-
$result = $this->rule->processNode($node, $scope);
5125

52-
$this->assertSame(['Class PHPStan\Tests\NetteObjectChild extends Nette\Object - it\'s better to use Nette\SmartObject trait.'], $result);
26+
$this->analyse([__DIR__ . '/../../data/NetteObjectChild.php'], [
27+
[
28+
'Class PHPStan\Tests\NetteObjectChild extends Nette\Object - it\'s better to use Nette\SmartObject trait.',
29+
4,
30+
],
31+
]);
5332
}
5433

5534
}

0 commit comments

Comments
 (0)