-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathAnnotationProviderTest.php
More file actions
97 lines (81 loc) · 3.29 KB
/
AnnotationProviderTest.php
File metadata and controls
97 lines (81 loc) · 3.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
<?php
namespace Pgs\HashIdBundle\Tests\AnnotationProvider;
use Doctrine\Common\Annotations\Reader;
use Pgs\HashIdBundle\AnnotationProvider\AnnotationProvider;
use Pgs\HashIdBundle\AnnotationProvider\AnnotationProviderInterface;
use Pgs\HashIdBundle\Controller\DemoController;
use Pgs\HashIdBundle\Exception\InvalidControllerException;
use Pgs\HashIdBundle\Reflection\ReflectionProvider;
use PHPUnit\Framework\TestCase;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
class AnnotationProviderTest extends TestCase
{
/**
* @var AnnotationProviderInterface
*/
protected $controllerAnnotationProvider;
protected function setUp(): void
{
$this->controllerAnnotationProvider = new AnnotationProvider(
$this->getReaderMock(),
$this->getReflectionProviderMock()
);
}
public function testCreate(): void
{
$this->assertInstanceOf(AnnotationProvider::class, $this->controllerAnnotationProvider);
}
public function testInvalidControllerString(): void
{
$this->expectException(InvalidControllerException::class);
$this->controllerAnnotationProvider->getFromString('dummy_controler_string', 'annotationClassName');
}
public function testReturnObject(): void
{
$result = $this->controllerAnnotationProvider->getFromString(
'Pgs\HashIdBundle\Controller\DemoController::demo',
'annotationClassName'
);
$this->assertTrue(\is_object($result));
$controller = $this->getControllerMock();
$result = $this->controllerAnnotationProvider->getFromObject($controller, 'demo', 'annotationClassName');
$this->assertTrue(\is_object($result));
}
public function testInvalidControllerObject(): void
{
$this->expectException(InvalidControllerException::class);
$controller = 'dummy_controller_string';
$this->controllerAnnotationProvider->getFromObject($controller, 'demo', 'annotationClassName');
}
protected function getReaderMock()
{
$readerMock = $this->getMockBuilder(Reader::class)
->setMethods(['getMethodAnnotation'])
->getMockForAbstractClass();
$readerMock
->method('getMethodAnnotation')
->with($this->createMock(\ReflectionMethod::class))->willReturn(new \stdClass());
return $readerMock;
}
protected function getReflectionProviderMock()
{
$reflectionProviderMock = $this->getMockBuilder(ReflectionProvider::class)
->setMethods([
'getMethodReflectionFromClassString',
'getMethodReflectionFromObject',
])
->getMock();
$reflectionProviderMock
->method('getMethodReflectionFromClassString')
->with(DemoController::class, 'demo')->willReturn($this->createMock(\ReflectionMethod::class));
$reflectionProviderMock
->method('getMethodReflectionFromObject')
->with($this->getControllerMock(), 'demo')->willReturn($this->createMock(\ReflectionMethod::class));
return $reflectionProviderMock;
}
protected function getControllerMock()
{
$mock = $this->getMockBuilder(AbstractController::class)->setMethods(['demo'])->getMockForAbstractClass();
return $mock;
}
}