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
|
|
|
$resourceHandler = $this->getResourceHandler(); |
20
|
|
|
$this->assertTrue($resourceHandler instanceof ResourceHandlerInterface); |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @test |
25
|
|
|
*/ |
26
|
|
|
public function getSingle() |
27
|
|
|
{ |
28
|
|
|
$resourceHandler = $this->getResourceHandler('get', 'bar'); |
29
|
|
|
$this->assertTrue($resourceHandler instanceof ResourceHandlerInterface); |
30
|
|
|
|
31
|
|
|
$single = $resourceHandler->getSingle('product', 1); |
32
|
|
|
$this->assertEquals('bar', $single); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @test |
37
|
|
|
*/ |
38
|
|
|
public function getCollection() |
39
|
|
|
{ |
40
|
|
|
$resourceHandler = $this->getResourceHandler('getCollection', array('foo', 'bar')); |
41
|
|
|
$this->assertTrue($resourceHandler instanceof ResourceHandlerInterface); |
42
|
|
|
|
43
|
|
|
$collection = $resourceHandler->getCollection('product'); |
44
|
|
|
$this->assertTrue(count($collection) == 2 && is_array($collection)); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @test |
49
|
|
|
* @dataProvider dataProvider |
50
|
|
|
*/ |
51
|
|
|
public function postSingle($data) |
52
|
|
|
{ |
53
|
|
|
$resourceHandler = $this->provideResourceHandler('create'); |
54
|
|
|
$resource = $resourceHandler->postSingle('product', $data); |
55
|
|
|
$this->assertEquals($data['name'], $resource->getName()); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @test |
60
|
|
|
* @dataProvider dataProvider |
61
|
|
|
*/ |
62
|
|
|
public function putSingle($data) |
63
|
|
|
{ |
64
|
|
|
$resourceHandler = $this->provideResourceHandler('update'); |
65
|
|
|
$resource = $resourceHandler->putSingle('product', 1, $data); |
66
|
|
|
$this->assertEquals($data['name'], $resource->getName()); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @test |
71
|
|
|
*/ |
72
|
|
|
public function deleteSingle() |
73
|
|
|
{ |
74
|
|
|
$entityManager = $this->getMockBuilder('Managlea\Component\EntityManager\DoctrineEntityManager') |
75
|
|
|
->disableOriginalConstructor() |
76
|
|
|
->getMock(); |
77
|
|
|
$entityManager->method('delete') |
78
|
|
|
->willReturn(true); |
79
|
|
|
|
80
|
|
|
$entityManagerFactory = $this->getMockBuilder('Managlea\Component\EntityManagerFactory') |
81
|
|
|
->disableOriginalConstructor() |
82
|
|
|
->getMock(); |
83
|
|
|
$entityManagerFactory->method('create') |
84
|
|
|
->willReturn($entityManager); |
85
|
|
|
$resourceMapper = new ResourceMapper; |
86
|
|
|
|
87
|
|
|
$resourceHandler = ResourceHandler::initialize($entityManagerFactory, $resourceMapper); |
88
|
|
|
|
89
|
|
|
$resource = $resourceHandler->deleteSingle('product', 1); |
90
|
|
|
$this->assertEquals(true, $resource); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
private function provideResourceHandler($action) |
94
|
|
|
{ |
95
|
|
|
$data = current(current($this->dataProvider())); |
96
|
|
|
|
97
|
|
|
$product = new Product(); |
98
|
|
|
$product->setName($data['name']); |
99
|
|
|
$product->setDateOfBirth($data['dateOfBirth']); |
100
|
|
|
|
101
|
|
|
$entityManager = $this->getMockBuilder('Managlea\Component\EntityManager\DoctrineEntityManager') |
102
|
|
|
->disableOriginalConstructor() |
103
|
|
|
->getMock(); |
104
|
|
|
|
105
|
|
|
$entityManager->method($action) |
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
|
|
|
return $resourceHandler; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @param $method |
122
|
|
|
* @param $returnData |
123
|
|
|
* @return ResourceHandlerInterface |
124
|
|
|
*/ |
125
|
|
|
public function getResourceHandler($method = null, $returnData = null) |
126
|
|
|
{ |
127
|
|
|
$entityManager = $this->getMockBuilder('Managlea\Component\EntityManager\DoctrineEntityManager') |
128
|
|
|
->disableOriginalConstructor() |
129
|
|
|
->getMock(); |
130
|
|
|
|
131
|
|
|
if (!is_null($method)) { |
132
|
|
|
$entityManager->method($method) |
133
|
|
|
->willReturn($returnData); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
$entityManagerFactory = $this->getMockBuilder('Managlea\Component\EntityManagerFactory') |
137
|
|
|
->disableOriginalConstructor() |
138
|
|
|
->getMock(); |
139
|
|
|
$entityManagerFactory->method('create') |
140
|
|
|
->willReturn($entityManager); |
141
|
|
|
|
142
|
|
|
$resourceMapper = new ResourceMapper; |
143
|
|
|
$resourceHandler = ResourceHandler::initialize($entityManagerFactory, $resourceMapper); |
144
|
|
|
|
145
|
|
|
return $resourceHandler; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
public function dataProvider() |
149
|
|
|
{ |
150
|
|
|
return array( |
151
|
|
|
array(array('name' => 'Random Joe', 'dateOfBirth' => '1970-01-01')) |
152
|
|
|
); |
153
|
|
|
} |
154
|
|
|
} |
155
|
|
|
|