Skip to content

Commit c8fe20f

Browse files
committed
dot annotated services unit tests
1 parent 3f65990 commit c8fe20f

7 files changed

Lines changed: 97 additions & 46 deletions

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,5 @@ composer.phar
3636

3737
# Commit your application's lock file http://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file
3838
# You may choose to ignore a library lock file http://getcomposer.org/doc/02-libraries.md#lock-file
39-
composer.lock
39+
composer.lock
40+
.phpunit.result.cache

src/Factory/AbstractAnnotatedFactory.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@
1414
use Doctrine\Common\Annotations\CachedReader;
1515
use Doctrine\Common\Annotations\Reader;
1616
use Doctrine\Common\Cache\Cache;
17+
use Psr\Container\ContainerExceptionInterface;
1718
use Psr\Container\ContainerInterface;
19+
use Psr\Container\NotFoundExceptionInterface;
1820

1921
/**
2022
* Class AbstractAnnotatedFactory
@@ -37,7 +39,9 @@ public function setAnnotationReader(Reader $annotationReader)
3739

3840
/**
3941
* @param ContainerInterface $container
40-
* @return AnnotationReader|CachedReader|Reader
42+
* @return Reader
43+
* @throws ContainerExceptionInterface
44+
* @throws NotFoundExceptionInterface
4145
*/
4246
protected function createAnnotationReader(ContainerInterface $container): Reader
4347
{

src/Factory/AnnotatedServiceFactory.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,9 @@ public function createObject(ContainerInterface $container, $requestedName)
5555
$service = null;
5656

5757
$annotationReader = $this->createAnnotationReader($container);
58-
$refClass = new ReflectionClass($requestedName);
58+
$refClass = $this->getReflectionClass($requestedName);
5959
$constructor = $refClass->getConstructor();
60+
6061
if ($constructor === null) {
6162
$service = new $requestedName();
6263
} else {
@@ -144,4 +145,14 @@ protected function readKeysFromArray(array $keys, $array)
144145
}
145146
return $value;
146147
}
148+
149+
/**
150+
* @param string $requestedName
151+
* @return ReflectionClass
152+
* @throws ReflectionException
153+
*/
154+
protected function getReflectionClass(string $requestedName): ReflectionClass
155+
{
156+
return new ReflectionClass($requestedName);
157+
}
147158
}

tests/AnnotatedRepositoryFactoryTest.php

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
use Doctrine\ORM\EntityRepository;
88
use Dot\AnnotatedServices\Annotation\Entity;
99
use Dot\AnnotatedServices\Exception\RuntimeException;
10-
use DotTest\AnnotatedServices\Stubs\TestRepository;
1110
use PHPUnit\Framework\TestCase;
1211
use Psr\Container\ContainerInterface;
1312
use Dot\AnnotatedServices\Factory\AnnotatedRepositoryFactory as Subject;
@@ -55,30 +54,26 @@ public function testThrowsExceptionClassNotExtendsEntityRepository()
5554

5655
public function testCreateObjectThrowsExceptionAnnotationNotFound()
5756
{
58-
$requestedName = TestRepository::class;
59-
57+
$repository = $this->createMock(EntityRepository::class);
6058
$this->annotationReader->method('getClassAnnotation')->willReturn(null);
6159

62-
$this->subject
63-
->method('createAnnotationReader')
64-
->willReturn($this->annotationReader);
60+
$this->subject->method('createAnnotationReader')->willReturn($this->annotationReader);
6561

6662
$this->expectException(RuntimeException::class);
6763
$this->expectExceptionMessage(RuntimeException::annotationNotFound(
6864
Entity::class,
69-
$requestedName,
65+
get_class($repository),
7066
get_class($this->subject)
7167
)->getMessage());
7268

73-
$this->subject->__invoke($this->container, $requestedName);
69+
$this->subject->__invoke($this->container, get_class($repository));
7470
}
7571

7672
public function testCreateObjectReturnsEntityRepository()
7773
{
78-
$requestedName = TestRepository::class;
74+
$repository = $this->createMock(EntityRepository::class);
7975
$annotation = new Entity('test');
8076
$entityManager = $this->createMock(EntityManagerInterface::class);
81-
$repository = $this->createMock(TestRepository::class);
8277

8378
$entityManager->method('getRepository')->willReturn($repository);
8479

@@ -92,7 +87,7 @@ public function testCreateObjectReturnsEntityRepository()
9287
->method('createAnnotationReader')
9388
->willReturn($this->annotationReader);
9489

95-
$object = $this->subject->__invoke($this->container, $requestedName);
90+
$object = $this->subject->__invoke($this->container, get_class($repository));
9691

9792
$this->assertInstanceOf(EntityRepository::class, $object);
9893
}

tests/AnnotatedServiceFactoryTest.php

Lines changed: 72 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
<?php
22

3-
43
namespace DotTest\AnnotatedServices;
54

6-
75
use Doctrine\Common\Annotations\Reader;
6+
use Dot\AnnotatedServices\Annotation\Inject;
87
use Dot\AnnotatedServices\Exception\RuntimeException;
98
use Dot\AnnotatedServices\Factory\AnnotatedServiceFactory as Subject;
10-
use DotTest\AnnotatedServices\Stubs\TestService;
119
use PHPUnit\Framework\TestCase;
1210
use Psr\Container\ContainerInterface;
11+
use ReflectionMethod;
12+
use ReflectionClass;
1313

1414
/**
1515
* Class AnnotatedServiceFactoryTest
@@ -29,7 +29,10 @@ public function setUp(): void
2929

3030
$this->container = $this->createMock(ContainerInterface::class);
3131
$this->annotationReader = $this->createMock(Reader::class);
32-
$this->subject = $this->createPartialMock(Subject::class, ['createAnnotationReader']);
32+
$this->subject = $this->createPartialMock(Subject::class, [
33+
'createAnnotationReader',
34+
'getReflectionClass'
35+
]);
3336
}
3437

3538
public function testThrowsExceptionClassNotFound()
@@ -42,11 +45,74 @@ public function testThrowsExceptionClassNotFound()
4245
$this->subject->__invoke($this->container, $requestedName);
4346
}
4447

45-
public function testReturnsSimpleService()
48+
public function testReturnServiceWithNoDependencies()
4649
{
47-
$requestedName = TestService::class;
50+
$requestedName = 'TestService';
51+
$this->getMockBuilder($requestedName)->allowMockingUnknownTypes()->getMock();
52+
$refClass = $this->createMock(ReflectionClass::class);
53+
54+
$refClass->method('getConstructor')->willReturn(null);
55+
$refClass->method('getMethods')->willReturn([]);
56+
57+
$this->annotationReader->method('getMethodAnnotation')->willReturn(null);
58+
$this->subject
59+
->method('createAnnotationReader')
60+
->willReturn($this->annotationReader);
61+
$this->subject->method('getReflectionClass')->willReturn($refClass);
62+
4863
$object = $this->subject->__invoke($this->container, $requestedName);
4964

5065
$this->assertInstanceOf($requestedName, $object);
5166
}
67+
68+
public function testThrowsExceptionAnnotationNotFound()
69+
{
70+
$requestedName = 'TestService';
71+
$this->getMockBuilder($requestedName)->allowMockingUnknownTypes()->getMock();
72+
$refClass = $this->createMock(ReflectionClass::class);
73+
$refConstructor = $this->createMock(ReflectionMethod::class);
74+
75+
$refClass->method('getConstructor')->willReturn($refConstructor);
76+
$refConstructor->method('getNumberOfRequiredParameters')->willReturn(100);
77+
78+
$this->annotationReader->method('getMethodAnnotation')->willReturn(null);
79+
$this->subject
80+
->method('createAnnotationReader')
81+
->willReturn($this->annotationReader);
82+
$this->subject->method('getReflectionClass')->willReturn($refClass);
83+
84+
$this->expectException(RuntimeException::class);
85+
$this->expectExceptionMessage(RuntimeException::annotationNotFound(
86+
Inject::class,
87+
$requestedName,
88+
get_class($this->subject),
89+
)->getMessage());
90+
91+
$this->subject->__invoke($this->container, $requestedName);
92+
}
93+
94+
public function testReturnService()
95+
{
96+
$requestedName = 'TestService';
97+
$this->getMockBuilder($requestedName)->allowMockingUnknownTypes()->getMock();
98+
$refClass = $this->createMock(ReflectionClass::class);
99+
$refConstructor = $this->createMock(ReflectionMethod::class);
100+
101+
$refClass->method('getConstructor')->willReturn($refConstructor);
102+
$refClass->method('getMethods')->willReturn([]);
103+
$refConstructor->method('getNumberOfRequiredParameters')->willReturn(1);
104+
105+
$inject = new Inject(['test']);
106+
$this->annotationReader->method('getMethodAnnotation')->willReturn($inject);
107+
108+
$this->subject
109+
->method('createAnnotationReader')
110+
->willReturn($this->annotationReader);
111+
$this->subject->method('getReflectionClass')->willReturn($refClass);
112+
113+
$service = $this->subject->__invoke($this->container, $requestedName);
114+
115+
$this->assertInstanceOf($requestedName, $service);
116+
}
52117
}
118+

tests/Stubs/TestRepository.php

Lines changed: 0 additions & 16 deletions
This file was deleted.

tests/Stubs/TestService.php

Lines changed: 0 additions & 10 deletions
This file was deleted.

0 commit comments

Comments
 (0)