1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace Managlea\Tests; |
5
|
|
|
|
6
|
|
|
use Managlea\Component\EntityManagerFactory; |
7
|
|
|
use Managlea\Component\ResourceHandler; |
8
|
|
|
use Managlea\Component\ResourceHandlerInterface; |
9
|
|
|
use Managlea\Component\ResourceMapper; |
10
|
|
|
use Managlea\Tests\Model\Product; |
11
|
|
|
|
12
|
|
|
class ResourceHandlerTest extends \PHPUnit_Framework_TestCase |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @test |
16
|
|
|
*/ |
17
|
|
|
public function initialize() |
18
|
|
|
{ |
19
|
|
|
$resourceMapper = new ResourceMapper; |
20
|
|
|
$resourceHandler = ResourceHandler::initialize(new EntityManagerFactory(), $resourceMapper); |
21
|
|
|
$this->assertTrue($resourceHandler instanceof ResourceHandlerInterface); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @test |
26
|
|
|
* @expectedException \Exception |
27
|
|
|
*/ |
28
|
|
|
public function getSingleWrongEntityManager() |
29
|
|
|
{ |
30
|
|
|
$entityManagerFactory = $this->getMockBuilder('Managlea\Component\EntityManagerFactory') |
31
|
|
|
->disableOriginalConstructor() |
32
|
|
|
->getMock(); |
33
|
|
|
$entityManagerFactory->method('create') |
34
|
|
|
->willReturn('foo'); |
35
|
|
|
|
36
|
|
|
$resourceMapper = new ResourceMapper; |
37
|
|
|
$resourceHandler = ResourceHandler::initialize($entityManagerFactory, $resourceMapper); |
38
|
|
|
$resourceHandler->getSingle('product', 1); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @test |
43
|
|
|
*/ |
44
|
|
|
public function getSingle() |
45
|
|
|
{ |
46
|
|
|
$entityManager = $this->getMockBuilder('Managlea\Component\EntityManager\DoctrineEntityManager') |
47
|
|
|
->disableOriginalConstructor() |
48
|
|
|
->getMock(); |
49
|
|
|
$entityManager->method('get') |
50
|
|
|
->willReturn('bar'); |
51
|
|
|
|
52
|
|
|
$entityManagerFactory = $this->getMockBuilder('Managlea\Component\EntityManagerFactory') |
53
|
|
|
->disableOriginalConstructor() |
54
|
|
|
->getMock(); |
55
|
|
|
$entityManagerFactory->method('create') |
56
|
|
|
->willReturn($entityManager); |
57
|
|
|
|
58
|
|
|
$resourceMapper = new ResourceMapper; |
59
|
|
|
$resourceHandler = ResourceHandler::initialize($entityManagerFactory, $resourceMapper); |
60
|
|
|
$this->assertTrue($resourceHandler instanceof ResourceHandlerInterface); |
61
|
|
|
|
62
|
|
|
$single = $resourceHandler->getSingle('product', 1); |
63
|
|
|
$this->assertEquals('bar', $single); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @test |
68
|
|
|
*/ |
69
|
|
|
public function getCollection() |
70
|
|
|
{ |
71
|
|
|
$entityManager = $this->getMockBuilder('Managlea\Component\EntityManager\DoctrineEntityManager') |
72
|
|
|
->disableOriginalConstructor() |
73
|
|
|
->getMock(); |
74
|
|
|
$entityManager->method('getCollection') |
75
|
|
|
->willReturn(array('foo', 'bar')); |
76
|
|
|
|
77
|
|
|
$entityManagerFactory = $this->getMockBuilder('Managlea\Component\EntityManagerFactory') |
78
|
|
|
->disableOriginalConstructor() |
79
|
|
|
->getMock(); |
80
|
|
|
$entityManagerFactory->method('create') |
81
|
|
|
->willReturn($entityManager); |
82
|
|
|
|
83
|
|
|
$resourceMapper = new ResourceMapper; |
84
|
|
|
$resourceHandler = ResourceHandler::initialize($entityManagerFactory, $resourceMapper); |
85
|
|
|
$this->assertTrue($resourceHandler instanceof ResourceHandlerInterface); |
86
|
|
|
|
87
|
|
|
$collection = $resourceHandler->getCollection('product'); |
88
|
|
|
$this->assertTrue(count($collection) == 2 && is_array($collection)); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @test |
93
|
|
|
*/ |
94
|
|
|
public function postSingle() |
|
|
|
|
95
|
|
|
{ |
96
|
|
|
$data = array('name' => 'Random Joe', 'dateOfBirth' => '1970-01-01'); |
97
|
|
|
|
98
|
|
|
$product = new Product(); |
99
|
|
|
$product->setName($data['name']); |
100
|
|
|
$product->setDateOfBirth($data['dateOfBirth']); |
101
|
|
|
|
102
|
|
|
$entityManager = $this->getMockBuilder('Managlea\Component\EntityManager\DoctrineEntityManager') |
103
|
|
|
->disableOriginalConstructor() |
104
|
|
|
->getMock(); |
105
|
|
|
$entityManager->method('create') |
106
|
|
|
->willReturn($product); |
107
|
|
|
|
108
|
|
|
$entityManagerFactory = $this->getMockBuilder('Managlea\Component\EntityManagerFactory') |
109
|
|
|
->disableOriginalConstructor() |
110
|
|
|
->getMock(); |
111
|
|
|
$entityManagerFactory->method('create') |
112
|
|
|
->willReturn($entityManager); |
113
|
|
|
$resourceMapper = new ResourceMapper; |
114
|
|
|
|
115
|
|
|
$resourceHandler = ResourceHandler::initialize($entityManagerFactory, $resourceMapper); |
116
|
|
|
|
117
|
|
|
$resource = $resourceHandler->postSingle('product', $data); |
118
|
|
|
$this->assertEquals($data['name'], $resource->getName()); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @test |
123
|
|
|
*/ |
124
|
|
|
public function putSingle() |
|
|
|
|
125
|
|
|
{ |
126
|
|
|
$data = array('name' => 'Random Joe', 'dateOfBirth' => '1970-01-02'); |
127
|
|
|
|
128
|
|
|
$product = new Product(); |
129
|
|
|
$product->setName($data['name']); |
130
|
|
|
$product->setDateOfBirth($data['dateOfBirth']); |
131
|
|
|
|
132
|
|
|
$entityManager = $this->getMockBuilder('Managlea\Component\EntityManager\DoctrineEntityManager') |
133
|
|
|
->disableOriginalConstructor() |
134
|
|
|
->getMock(); |
135
|
|
|
$entityManager->method('update') |
136
|
|
|
->willReturn($product); |
137
|
|
|
|
138
|
|
|
$entityManagerFactory = $this->getMockBuilder('Managlea\Component\EntityManagerFactory') |
139
|
|
|
->disableOriginalConstructor() |
140
|
|
|
->getMock(); |
141
|
|
|
$entityManagerFactory->method('create') |
142
|
|
|
->willReturn($entityManager); |
143
|
|
|
$resourceMapper = new ResourceMapper; |
144
|
|
|
|
145
|
|
|
$resourceHandler = ResourceHandler::initialize($entityManagerFactory, $resourceMapper); |
146
|
|
|
|
147
|
|
|
$resource = $resourceHandler->putSingle('product', 1, $data); |
148
|
|
|
$this->assertEquals($data['name'], $resource->getName()); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* @test |
153
|
|
|
*/ |
154
|
|
|
public function deleteSingle() |
155
|
|
|
{ |
156
|
|
|
$entityManager = $this->getMockBuilder('Managlea\Component\EntityManager\DoctrineEntityManager') |
157
|
|
|
->disableOriginalConstructor() |
158
|
|
|
->getMock(); |
159
|
|
|
$entityManager->method('delete') |
160
|
|
|
->willReturn(true); |
161
|
|
|
|
162
|
|
|
$entityManagerFactory = $this->getMockBuilder('Managlea\Component\EntityManagerFactory') |
163
|
|
|
->disableOriginalConstructor() |
164
|
|
|
->getMock(); |
165
|
|
|
$entityManagerFactory->method('create') |
166
|
|
|
->willReturn($entityManager); |
167
|
|
|
$resourceMapper = new ResourceMapper; |
168
|
|
|
|
169
|
|
|
$resourceHandler = ResourceHandler::initialize($entityManagerFactory, $resourceMapper); |
170
|
|
|
|
171
|
|
|
$resource = $resourceHandler->deleteSingle('product', 1); |
172
|
|
|
$this->assertEquals(true, $resource); |
173
|
|
|
} |
174
|
|
|
} |
175
|
|
|
|
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.