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
|
|
|
$resourceHandler = $this->getResourceHandler('get', 'bar'); |
47
|
|
|
$this->assertTrue($resourceHandler instanceof ResourceHandlerInterface); |
48
|
|
|
|
49
|
|
|
$single = $resourceHandler->getSingle('product', 1); |
50
|
|
|
$this->assertEquals('bar', $single); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @test |
55
|
|
|
*/ |
56
|
|
|
public function getCollection() |
57
|
|
|
{ |
58
|
|
|
$resourceHandler = $this->getResourceHandler('getCollection', array('foo', 'bar')); |
59
|
|
|
$this->assertTrue($resourceHandler instanceof ResourceHandlerInterface); |
60
|
|
|
|
61
|
|
|
$collection = $resourceHandler->getCollection('product'); |
62
|
|
|
$this->assertTrue(count($collection) == 2 && is_array($collection)); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @test |
67
|
|
|
* @dataProvider dataProvider |
68
|
|
|
*/ |
69
|
|
|
public function postSingle($data) |
70
|
|
|
{ |
71
|
|
|
$resourceHandler = $this->provideResourceHandler('create'); |
72
|
|
|
$resource = $resourceHandler->postSingle('product', $data); |
73
|
|
|
$this->assertEquals($data['name'], $resource->getName()); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @test |
78
|
|
|
* @dataProvider dataProvider |
79
|
|
|
*/ |
80
|
|
|
public function putSingle($data) |
81
|
|
|
{ |
82
|
|
|
$resourceHandler = $this->provideResourceHandler('update'); |
83
|
|
|
$resource = $resourceHandler->putSingle('product', 1, $data); |
84
|
|
|
$this->assertEquals($data['name'], $resource->getName()); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @test |
89
|
|
|
*/ |
90
|
|
|
public function deleteSingle() |
91
|
|
|
{ |
92
|
|
|
$entityManager = $this->getMockBuilder('Managlea\Component\EntityManager\DoctrineEntityManager') |
93
|
|
|
->disableOriginalConstructor() |
94
|
|
|
->getMock(); |
95
|
|
|
$entityManager->method('delete') |
96
|
|
|
->willReturn(true); |
97
|
|
|
|
98
|
|
|
$entityManagerFactory = $this->getMockBuilder('Managlea\Component\EntityManagerFactory') |
99
|
|
|
->disableOriginalConstructor() |
100
|
|
|
->getMock(); |
101
|
|
|
$entityManagerFactory->method('create') |
102
|
|
|
->willReturn($entityManager); |
103
|
|
|
$resourceMapper = new ResourceMapper; |
104
|
|
|
|
105
|
|
|
$resourceHandler = ResourceHandler::initialize($entityManagerFactory, $resourceMapper); |
106
|
|
|
|
107
|
|
|
$resource = $resourceHandler->deleteSingle('product', 1); |
108
|
|
|
$this->assertEquals(true, $resource); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
private function provideResourceHandler($action) |
112
|
|
|
{ |
113
|
|
|
$data = current(current($this->dataProvider())); |
114
|
|
|
|
115
|
|
|
$product = new Product(); |
116
|
|
|
$product->setName($data['name']); |
117
|
|
|
$product->setDateOfBirth($data['dateOfBirth']); |
118
|
|
|
|
119
|
|
|
$entityManager = $this->getMockBuilder('Managlea\Component\EntityManager\DoctrineEntityManager') |
120
|
|
|
->disableOriginalConstructor() |
121
|
|
|
->getMock(); |
122
|
|
|
|
123
|
|
|
$entityManager->method($action) |
124
|
|
|
->willReturn($product); |
125
|
|
|
|
126
|
|
|
$entityManagerFactory = $this->getMockBuilder('Managlea\Component\EntityManagerFactory') |
127
|
|
|
->disableOriginalConstructor() |
128
|
|
|
->getMock(); |
129
|
|
|
$entityManagerFactory->method('create') |
130
|
|
|
->willReturn($entityManager); |
131
|
|
|
$resourceMapper = new ResourceMapper; |
132
|
|
|
|
133
|
|
|
$resourceHandler = ResourceHandler::initialize($entityManagerFactory, $resourceMapper); |
134
|
|
|
|
135
|
|
|
return $resourceHandler; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* @param $method |
140
|
|
|
* @param $returnData |
141
|
|
|
* @return ResourceHandlerInterface |
142
|
|
|
*/ |
143
|
|
|
public function getResourceHandler($method, $returnData) |
144
|
|
|
{ |
145
|
|
|
$entityManager = $this->getMockBuilder('Managlea\Component\EntityManager\DoctrineEntityManager') |
146
|
|
|
->disableOriginalConstructor() |
147
|
|
|
->getMock(); |
148
|
|
|
|
149
|
|
|
$entityManager->method($method) |
150
|
|
|
->willReturn($returnData); |
151
|
|
|
|
152
|
|
|
$entityManagerFactory = $this->getMockBuilder('Managlea\Component\EntityManagerFactory') |
153
|
|
|
->disableOriginalConstructor() |
154
|
|
|
->getMock(); |
155
|
|
|
$entityManagerFactory->method('create') |
156
|
|
|
->willReturn($entityManager); |
157
|
|
|
|
158
|
|
|
$resourceMapper = new ResourceMapper; |
159
|
|
|
$resourceHandler = ResourceHandler::initialize($entityManagerFactory, $resourceMapper); |
160
|
|
|
|
161
|
|
|
return $resourceHandler; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
public function dataProvider() |
165
|
|
|
{ |
166
|
|
|
return array( |
167
|
|
|
array(array('name' => 'Random Joe', 'dateOfBirth' => '1970-01-01')) |
168
|
|
|
); |
169
|
|
|
} |
170
|
|
|
} |
171
|
|
|
|