Skip to content

Commit de29fd4

Browse files
committed
Added some tests
1 parent 5bbcbe1 commit de29fd4

7 files changed

+167
-12
lines changed

phpstan.neon

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
parameters:
2+
ignoreErrors:
3+
- '#, PHPUnit_Framework_MockObject_MockObject given#'

src/Rules/ContainerInterfacePrivateServiceRule.php

+2-6
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,11 @@ public function getNodeType(): string
3131
return MethodCall::class;
3232
}
3333

34-
/**
35-
* @param MethodCall $node
36-
* @param Scope $scope
37-
* @return array
38-
*/
3934
public function processNode(Node $node, Scope $scope): array
4035
{
4136
$services = $this->serviceMap->getServices();
42-
return $node->name === 'get'
37+
return $node instanceof MethodCall
38+
&& $node->name === 'get'
4339
&& $scope->getType($node->var)->getClass() === ContainerInterface::class
4440
&& isset($node->args[0])
4541
&& $node->args[0] instanceof Arg

src/Rules/ContainerInterfaceUnknownServiceRule.php

+2-6
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,11 @@ public function getNodeType(): string
3131
return MethodCall::class;
3232
}
3333

34-
/**
35-
* @param MethodCall $node
36-
* @param Scope $scope
37-
* @return array
38-
*/
3934
public function processNode(Node $node, Scope $scope): array
4035
{
4136
$services = $this->serviceMap->getServices();
42-
return $node->name === 'get'
37+
return $node instanceof MethodCall
38+
&& $node->name === 'get'
4339
&& $scope->getType($node->var)->getClass() === ContainerInterface::class
4440
&& isset($node->args[0])
4541
&& $node->args[0] instanceof Arg

tests/.gitkeep

Whitespace-only changes.

tests/ServiceMapTest.php

+59
Original file line numberDiff line numberDiff line change
@@ -1 +1,60 @@
11
<?php
2+
3+
declare(strict_types = 1);
4+
5+
namespace Lookyman\PHPStan\Symfony;
6+
7+
use PHPUnit\Framework\TestCase;
8+
9+
/**
10+
* @covers \Lookyman\PHPStan\Symfony\ServiceMap
11+
*/
12+
final class ServiceMapTest extends TestCase
13+
{
14+
15+
public function testGetServices()
16+
{
17+
$serviceMap = new ServiceMap(__DIR__ . '/container.xml');
18+
self::assertEquals(
19+
[
20+
'withoutClass' => [
21+
'class' => \null,
22+
'public' => \true,
23+
'synthetic' => \false,
24+
],
25+
'withClass' => [
26+
'class' => 'Foo',
27+
'public' => \true,
28+
'synthetic' => \false,
29+
],
30+
'withoutPublic' => [
31+
'class' => 'Foo',
32+
'public' => \true,
33+
'synthetic' => \false,
34+
],
35+
'publicNotFalse' => [
36+
'class' => 'Foo',
37+
'public' => \true,
38+
'synthetic' => \false,
39+
],
40+
'private' => [
41+
'class' => 'Foo',
42+
'public' => \false,
43+
'synthetic' => \false,
44+
],
45+
'synthetic' => [
46+
'class' => 'Foo',
47+
'public' => \true,
48+
'synthetic' => \true,
49+
],
50+
'alias' => [
51+
'class' => 'Foo',
52+
'public' => \true,
53+
'synthetic' => \false,
54+
],
55+
],
56+
$serviceMap->getServices()
57+
);
58+
}
59+
60+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
<?php
2+
3+
declare(strict_types = 1);
4+
5+
namespace Lookyman\PHPStan\Symfony\Type;
6+
7+
use Lookyman\PHPStan\Symfony\ServiceMap;
8+
use PHPStan\Analyser\Scope;
9+
use PHPStan\Reflection\MethodReflection;
10+
use PHPStan\Type\DynamicMethodReturnTypeExtension;
11+
use PHPStan\Type\ObjectType;
12+
use PHPStan\Type\Type;
13+
use PHPUnit\Framework\TestCase;
14+
use PhpParser\Node\Arg;
15+
use PhpParser\Node\Expr;
16+
use PhpParser\Node\Expr\MethodCall;
17+
use PhpParser\Node\Scalar\String_;
18+
use Symfony\Component\DependencyInjection\ContainerInterface;
19+
20+
/**
21+
* @covers \Lookyman\PHPStan\Symfony\Type\ContainerInterfaceDynamicReturnTypeExtension
22+
*/
23+
final class ContainerInterfaceDynamicReturnTypeExtensionTest extends TestCase
24+
{
25+
26+
public function testImplementsDynamicMethodReturnTypeExtension()
27+
{
28+
self::assertInstanceOf(
29+
DynamicMethodReturnTypeExtension::class,
30+
new ContainerInterfaceDynamicReturnTypeExtension(new ServiceMap(__DIR__ . '/../container.xml'))
31+
);
32+
}
33+
34+
public function testGetClass()
35+
{
36+
$extension = new ContainerInterfaceDynamicReturnTypeExtension(new ServiceMap(__DIR__ . '/../container.xml'));
37+
self::assertEquals(ContainerInterface::class, $extension::getClass());
38+
}
39+
40+
public function testIsMethodSupported()
41+
{
42+
$methodGet = $this->createMock(MethodReflection::class);
43+
$methodGet->expects(self::once())->method('getName')->willReturn('get');
44+
45+
$methodFoo = $this->createMock(MethodReflection::class);
46+
$methodFoo->expects(self::once())->method('getName')->willReturn('foo');
47+
48+
$extension = new ContainerInterfaceDynamicReturnTypeExtension(new ServiceMap(__DIR__ . '/../container.xml'));
49+
self::assertTrue($extension->isMethodSupported($methodGet));
50+
self::assertFalse($extension->isMethodSupported($methodFoo));
51+
}
52+
53+
/**
54+
* @dataProvider getTypeFromMethodCallProvider
55+
*/
56+
public function testGetTypeFromMethodCall(MethodReflection $methodReflection, MethodCall $methodCall, Type $expectedType)
57+
{
58+
$extension = new ContainerInterfaceDynamicReturnTypeExtension(new ServiceMap(__DIR__ . '/../container.xml'));
59+
$type = $extension->getTypeFromMethodCall(
60+
$methodReflection,
61+
$methodCall,
62+
$this->createMock(Scope::class)
63+
);
64+
self::assertEquals($expectedType, $type);
65+
}
66+
67+
public function getTypeFromMethodCallProvider(): array
68+
{
69+
$notFoundType = $this->createMock(Type::class);
70+
71+
$methodReflectionNotFound = $this->createMock(MethodReflection::class);
72+
$methodReflectionNotFound->expects(self::once())->method('getReturnType')->willReturn($notFoundType);
73+
74+
return [
75+
'found' => [
76+
$this->createMock(MethodReflection::class),
77+
new MethodCall($this->createMock(Expr::class), '', [new Arg(new String_('withClass'))]),
78+
new ObjectType('Foo'),
79+
],
80+
'notFound' => [
81+
$methodReflectionNotFound,
82+
new MethodCall($this->createMock(Expr::class), ''),
83+
$notFoundType,
84+
],
85+
];
86+
}
87+
88+
}

tests/container.xml

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<container xmlns="http://symfony.com/schema/dic/services" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
3+
<services>
4+
<service></service><!-- without id -->
5+
<service id="withoutClass"></service>
6+
<service id="withClass" class="Foo"></service>
7+
<service id="withoutPublic" class="Foo"></service>
8+
<service id="publicNotFalse" class="Foo" public="true"></service>
9+
<service id="private" class="Foo" public="false"></service>
10+
<service id="synthetic" class="Foo" synthetic="true"></service>
11+
<service id="alias" class="Bar" alias="withClass"></service>
12+
</services>
13+
</container>

0 commit comments

Comments
 (0)