1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace FOS\ElasticaBundle\Tests\Manager; |
4
|
|
|
|
5
|
|
|
use FOS\ElasticaBundle\Manager\RepositoryManager; |
6
|
|
|
|
7
|
|
|
class CustomRepository |
8
|
|
|
{ |
9
|
|
|
} |
10
|
|
|
|
11
|
|
|
class Entity |
12
|
|
|
{ |
13
|
|
|
} |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @author Richard Miller <[email protected]> |
17
|
|
|
*/ |
18
|
|
|
class RepositoryManagerTest extends \PHPUnit_Framework_TestCase |
19
|
|
|
{ |
20
|
|
View Code Duplication |
public function testThatGetRepositoryReturnsDefaultRepository() |
|
|
|
|
21
|
|
|
{ |
22
|
|
|
/** @var $finderMock \PHPUnit_Framework_MockObject_MockObject|\FOS\ElasticaBundle\Finder\TransformedFinder */ |
23
|
|
|
$finderMock = $this->getMockBuilder('FOS\ElasticaBundle\Finder\TransformedFinder') |
24
|
|
|
->disableOriginalConstructor() |
25
|
|
|
->getMock(); |
26
|
|
|
|
27
|
|
|
/** @var $readerMock \PHPUnit_Framework_MockObject_MockObject|\Doctrine\Common\Annotations\Reader */ |
28
|
|
|
$readerMock = $this->getMockBuilder('Doctrine\Common\Annotations\Reader') |
29
|
|
|
->disableOriginalConstructor() |
30
|
|
|
->getMock(); |
31
|
|
|
|
32
|
|
|
$typeName = 'index/type'; |
33
|
|
|
|
34
|
|
|
$manager = new RepositoryManager($readerMock); |
|
|
|
|
35
|
|
|
$manager->addType($typeName, $finderMock); |
36
|
|
|
$repository = $manager->getRepository($typeName); |
37
|
|
|
$this->assertInstanceOf('FOS\ElasticaBundle\Repository', $repository); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
View Code Duplication |
public function testThatGetRepositoryReturnsCustomRepository() |
|
|
|
|
41
|
|
|
{ |
42
|
|
|
/** @var $finderMock \PHPUnit_Framework_MockObject_MockObject|\FOS\ElasticaBundle\Finder\TransformedFinder */ |
43
|
|
|
$finderMock = $this->getMockBuilder('FOS\ElasticaBundle\Finder\TransformedFinder') |
44
|
|
|
->disableOriginalConstructor() |
45
|
|
|
->getMock(); |
46
|
|
|
|
47
|
|
|
/** @var $readerMock \PHPUnit_Framework_MockObject_MockObject|\Doctrine\Common\Annotations\Reader */ |
48
|
|
|
$readerMock = $this->getMockBuilder('Doctrine\Common\Annotations\Reader') |
49
|
|
|
->disableOriginalConstructor() |
50
|
|
|
->getMock(); |
51
|
|
|
|
52
|
|
|
$typeName = 'index/type'; |
53
|
|
|
|
54
|
|
|
$manager = new RepositoryManager($readerMock); |
|
|
|
|
55
|
|
|
$manager->addType($typeName, $finderMock, 'FOS\ElasticaBundle\Tests\Manager\CustomRepository'); |
56
|
|
|
$repository = $manager->getRepository($typeName); |
57
|
|
|
$this->assertInstanceOf('FOS\ElasticaBundle\Tests\Manager\CustomRepository', $repository); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @expectedException \RuntimeException |
62
|
|
|
*/ |
63
|
|
View Code Duplication |
public function testThatGetRepositoryThrowsExceptionIfEntityNotConfigured() |
|
|
|
|
64
|
|
|
{ |
65
|
|
|
/** @var $finderMock \PHPUnit_Framework_MockObject_MockObject|\FOS\ElasticaBundle\Finder\TransformedFinder */ |
66
|
|
|
$finderMock = $this->getMockBuilder('FOS\ElasticaBundle\Finder\TransformedFinder') |
67
|
|
|
->disableOriginalConstructor() |
68
|
|
|
->getMock(); |
69
|
|
|
|
70
|
|
|
/** @var $readerMock \PHPUnit_Framework_MockObject_MockObject|\Doctrine\Common\Annotations\Reader */ |
71
|
|
|
$readerMock = $this->getMockBuilder('Doctrine\Common\Annotations\Reader') |
72
|
|
|
->disableOriginalConstructor() |
73
|
|
|
->getMock(); |
74
|
|
|
|
75
|
|
|
$typeName = 'index/type'; |
76
|
|
|
|
77
|
|
|
$manager = new RepositoryManager($readerMock); |
|
|
|
|
78
|
|
|
$manager->addType($typeName, $finderMock); |
79
|
|
|
$manager->getRepository('Missing type'); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @expectedException \RuntimeException |
84
|
|
|
*/ |
85
|
|
View Code Duplication |
public function testThatGetRepositoryThrowsExceptionIfCustomRepositoryNotFound() |
|
|
|
|
86
|
|
|
{ |
87
|
|
|
/** @var $finderMock \PHPUnit_Framework_MockObject_MockObject|\FOS\ElasticaBundle\Finder\TransformedFinder */ |
88
|
|
|
$finderMock = $this->getMockBuilder('FOS\ElasticaBundle\Finder\TransformedFinder') |
89
|
|
|
->disableOriginalConstructor() |
90
|
|
|
->getMock(); |
91
|
|
|
|
92
|
|
|
/** @var $readerMock \PHPUnit_Framework_MockObject_MockObject|\Doctrine\Common\Annotations\Reader */ |
93
|
|
|
$readerMock = $this->getMockBuilder('Doctrine\Common\Annotations\Reader') |
94
|
|
|
->disableOriginalConstructor() |
95
|
|
|
->getMock(); |
96
|
|
|
|
97
|
|
|
$typeName = 'index/type'; |
98
|
|
|
|
99
|
|
|
$manager = new RepositoryManager($readerMock); |
|
|
|
|
100
|
|
|
$manager->addType($typeName, $finderMock, 'FOS\ElasticaBundle\Tests\MissingRepository'); |
101
|
|
|
$manager->getRepository($typeName); |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.