Passed
Pull Request — master (#11)
by Managlea
03:10
created

ResourceHandlerTest::postSingle()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 26
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 26
rs 8.8571
cc 1
eloc 19
nc 1
nop 0
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
/**
13
 * Remove duplicate code
14
 */
15
class ResourceHandlerTest extends \PHPUnit_Framework_TestCase
16
{
17
    /**
18
     * @test
19
     */
20
    public function initialize()
21
    {
22
        $resourceMapper = new ResourceMapper;
23
        $resourceHandler = ResourceHandler::initialize(new EntityManagerFactory(), $resourceMapper);
24
        $this->assertTrue($resourceHandler instanceof ResourceHandlerInterface);
25
    }
26
27
    /**
28
     * @test
29
     * @expectedException \Exception
30
     */
31
    public function getSingleWrongEntityManager()
32
    {
33
        $entityManagerFactory = $this->getMockBuilder('Managlea\Component\EntityManagerFactory')
34
            ->disableOriginalConstructor()
35
            ->getMock();
36
        $entityManagerFactory->method('create')
37
            ->willReturn('foo');
38
39
        $resourceMapper = new ResourceMapper;
40
        $resourceHandler = ResourceHandler::initialize($entityManagerFactory, $resourceMapper);
41
        $resourceHandler->getSingle('product', 1);
42
    }
43
44
    /**
45
     * @test
46
     */
47
    public function getSingle()
48
    {
49
        $entityManager = $this->getMockBuilder('Managlea\Component\EntityManager\DoctrineEntityManager')
50
            ->disableOriginalConstructor()
51
            ->getMock();
52
        $entityManager->method('get')
53
            ->willReturn('bar');
54
55
        $entityManagerFactory = $this->getMockBuilder('Managlea\Component\EntityManagerFactory')
56
            ->disableOriginalConstructor()
57
            ->getMock();
58
        $entityManagerFactory->method('create')
59
            ->willReturn($entityManager);
60
61
        $resourceMapper = new ResourceMapper;
62
        $resourceHandler = ResourceHandler::initialize($entityManagerFactory, $resourceMapper);
63
        $this->assertTrue($resourceHandler instanceof ResourceHandlerInterface);
64
65
        $single = $resourceHandler->getSingle('product', 1);
66
        $this->assertEquals('bar', $single);
67
    }
68
69
    /**
70
     * @test
71
     */
72
    public function getCollection()
73
    {
74
        $entityManager = $this->getMockBuilder('Managlea\Component\EntityManager\DoctrineEntityManager')
75
            ->disableOriginalConstructor()
76
            ->getMock();
77
        $entityManager->method('getCollection')
78
            ->willReturn(array('foo', 'bar'));
79
80
        $entityManagerFactory = $this->getMockBuilder('Managlea\Component\EntityManagerFactory')
81
            ->disableOriginalConstructor()
82
            ->getMock();
83
        $entityManagerFactory->method('create')
84
            ->willReturn($entityManager);
85
86
        $resourceMapper = new ResourceMapper;
87
        $resourceHandler = ResourceHandler::initialize($entityManagerFactory, $resourceMapper);
88
        $this->assertTrue($resourceHandler instanceof ResourceHandlerInterface);
89
90
        $collection = $resourceHandler->getCollection('product');
91
        $this->assertTrue(count($collection) == 2 && is_array($collection));
92
    }
93
94
    /**
95
     * @test
96
     */
97
    public function postSingle()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
98
    {
99
        $data = array('name' => 'Random Joe', 'dateOfBirth' => '1970-01-01');
100
101
        $product = new Product();
102
        $product->setName($data['name']);
103
        $product->setDateOfBirth($data['dateOfBirth']);
104
105
        $entityManager = $this->getMockBuilder('Managlea\Component\EntityManager\DoctrineEntityManager')
106
            ->disableOriginalConstructor()
107
            ->getMock();
108
        $entityManager->method('create')
109
            ->willReturn($product);
110
111
        $entityManagerFactory = $this->getMockBuilder('Managlea\Component\EntityManagerFactory')
112
            ->disableOriginalConstructor()
113
            ->getMock();
114
        $entityManagerFactory->method('create')
115
            ->willReturn($entityManager);
116
        $resourceMapper = new ResourceMapper;
117
118
        $resourceHandler = ResourceHandler::initialize($entityManagerFactory, $resourceMapper);
119
120
        $resource = $resourceHandler->postSingle('product', $data);
121
        $this->assertEquals($data['name'], $resource->getName());
122
    }
123
124
    /**
125
     * @test
126
     */
127
    public function putSingle()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
128
    {
129
        $data = array('name' => 'Random Joe', 'dateOfBirth' => '1970-01-02');
130
131
        $product = new Product();
132
        $product->setName($data['name']);
133
        $product->setDateOfBirth($data['dateOfBirth']);
134
135
        $entityManager = $this->getMockBuilder('Managlea\Component\EntityManager\DoctrineEntityManager')
136
            ->disableOriginalConstructor()
137
            ->getMock();
138
        $entityManager->method('update')
139
            ->willReturn($product);
140
141
        $entityManagerFactory = $this->getMockBuilder('Managlea\Component\EntityManagerFactory')
142
            ->disableOriginalConstructor()
143
            ->getMock();
144
        $entityManagerFactory->method('create')
145
            ->willReturn($entityManager);
146
        $resourceMapper = new ResourceMapper;
147
148
        $resourceHandler = ResourceHandler::initialize($entityManagerFactory, $resourceMapper);
149
150
        $resource = $resourceHandler->putSingle('product', 1, $data);
151
        $this->assertEquals($data['name'], $resource->getName());
152
    }
153
154
    /**
155
     * @test
156
     */
157
    public function deleteSingle()
158
    {
159
        $entityManager = $this->getMockBuilder('Managlea\Component\EntityManager\DoctrineEntityManager')
160
            ->disableOriginalConstructor()
161
            ->getMock();
162
        $entityManager->method('delete')
163
            ->willReturn(true);
164
165
        $entityManagerFactory = $this->getMockBuilder('Managlea\Component\EntityManagerFactory')
166
            ->disableOriginalConstructor()
167
            ->getMock();
168
        $entityManagerFactory->method('create')
169
            ->willReturn($entityManager);
170
        $resourceMapper = new ResourceMapper;
171
172
        $resourceHandler = ResourceHandler::initialize($entityManagerFactory, $resourceMapper);
173
174
        $resource = $resourceHandler->deleteSingle('product', 1);
175
        $this->assertEquals(true, $resource);
176
    }
177
}
178