Completed
Push — develop ( c09155...432462 )
by
unknown
16:18
created

testCreateServiceWithNoAssociation()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 10
rs 9.4286
cc 1
eloc 5
nc 1
nop 0
1
<?php
2
/**
3
 * YAWIK
4
 *
5
 * @filesource
6
 * @license MIT
7
 * @copyright  2013 - 2015 Cross Solution <http://cross-solution.de>
8
 */
9
  
10
/** */
11
namespace JobsTest\Factory\Form;
12
13
use Auth\Entity\User;
14
use Core\Entity\Collection\ArrayCollection;
15
use Jobs\Factory\Form\HiringOrganizationSelectFactory;
16
use Organizations\Entity\Organization;
17
use Organizations\Entity\OrganizationContact;
18
use Organizations\Entity\OrganizationName;
19
use Zend\Form\FormElementManager;
20
21
/**
22
 * Tests for the HiringOrganizationSelect factory
23
 *
24
 * @covers \Jobs\Factory\Form\HiringOrganizationSelectFactory
25
 * @author Mathias Gelhausen <[email protected]>
26
 * @group Jobs
27
 * @group Jobs.Factory
28
 * @group Jobs.Factory.Form
29
 */
30
class HiringOrganizationSelectFactoryTest extends \PHPUnit_Framework_TestCase
31
{
32
    /**
33
     * The "Class under Test"
34
     *
35
     * @var HiringOrganizationSelectFactory
36
     */
37
    private $target;
38
39
    /**
40
     * The user entity fixture
41
     *
42
     * @var User
43
     */
44
    private $user;
45
46
    /**
47
     * The form element manager mock
48
     *
49
     * @var FormElementManager
50
     */
51
    private $formElements;
52
53
    public function setUp()
54
    {
55
56
        $this->target = new HiringOrganizationSelectFactory();
57
58
        if ("testImplementsFactoryInterface" == $this->getName(/*withDataSet */ false)) { return; }
59
60
        $userOrg = $this->getMockBuilder('\Organizations\Entity\OrganizationReference')
61
                        ->disableOriginalConstructor()
62
                        ->getMock();
63
64
65
        $user = new User();
66
        $user->setId('testUser');
67
        $user->setOrganization($userOrg);
68
69
        $this->user = $user;
70
71
        $auth = $this->getMockBuilder('Auth\AuthenticationService')
72
                     ->disableOriginalConstructor()
73
                     ->getMock();
74
75
        $auth->expects($this->once())
76
             ->method('getUser')
77
             ->willReturn($user);
78
79
        $services = $this->getMockBuilder('\Zend\ServiceManager\ServiceManager')
80
                         ->disableOriginalConstructor()
81
                         ->getMock();
82
83
        $services->expects($this->once())
84
                 ->method('get')
85
                 ->with('AuthenticationService')
86
                 ->willReturn($auth);
87
88
        $formElements = $this->getMockBuilder('\Zend\Form\FormElementManager')
89
                             ->disableOriginalConstructor()
90
                             ->getMock();
91
92
        $formElements->expects($this->once())->method('getServiceLocator')->willReturn($services);
93
        $this->formElements = $formElements;
94
    }
95
96
    /**
97
     * @testdox Implements \Zend\ServiceManager\FactoryInterface
98
     */
99
    public function testImplementsFactoryInterface()
100
    {
101
        $this->assertInstanceOf('\Zend\ServiceManager\FactoryInterface', $this->target);
102
    }
103
104
    /**
105
     * @testdox createService() returns select form element.
106
     */
107
    public function testCreateServiceReturnsSelectFormElement()
108
    {
109
        /* @var $org \PHPUnit_Framework_MockObject_MockObject */
110
        $org = $this->user->getOrganization();
111
        $org->expects($this->once())->method('hasAssociation')->willReturn(false);
112
113
        $select = $this->target->createService($this->formElements);
114
115
        $this->assertInstanceOf('\Jobs\Form\HiringOrganizationSelect', $select);
116
117
    }
118
    /**
119
     * @testdox createService() returns select element with no value options if no organization is associated to the user.
120
     */
121
    public function testCreateServiceWithNoAssociation()
122
    {
123
        /* @var $org \PHPUnit_Framework_MockObject_MockObject */
124
        $org = $this->user->getOrganization();
125
        $org->expects($this->once())->method('hasAssociation')->willReturn(false);
126
127
        $select = $this->target->createService($this->formElements);
128
129
        $this->assertEquals(array(), $select->getValueOptions());
130
    }
131
132
    /**
133
     * @testdox createService() returns select element with value options if organization is associated to the user.
134
     */
135
    public function testCreateServiceWithAssociation()
136
    {
137
        /* @var $org \PHPUnit_Framework_MockObject_MockObject */
138
        $org = $this->user->getOrganization();
139
        $org->expects($this->once())->method('hasAssociation')->willReturn(true);
140
141
        $orgs = new ArrayCollection();
142
143
        $org0 = $this->generateOrganizationEntity(
144
                    'testOrg0',
145
                    'testOrg0.name',
146
                    'org0.testCity',
147
                    'org0.testStreet',
148
                    'org0.1234'
149
        );
150
151
        $org1 = $this->generateOrganizationEntity('testOrg1', 'testOrg1.name', 'org1.city', 'org1.street', 'org1.number');
152
        $orgs->add($org1);
153
154
        $org->expects($this->once())->method('getHiringOrganizations')->willReturn($orgs);
155
156
        $org->expects($this->once())->method('getOrganization')->willReturn($org0);
157
158
        $expect = array(
159
            'testOrg0' => 'testOrg0.name|org0.testCity|org0.testStreet|org0.1234|',
160
            'testOrg1' => 'testOrg1.name|org1.city|org1.street|org1.number|'
161
        );
162
163
        $select = $this->target->createService($this->formElements);
164
165
        $actual = $select->getValueOptions();
166
167
        $this->assertEquals($expect, $actual);
168
    }
169
170
    private function generateOrganizationEntity($id, $name, $city, $street, $number)
171
    {
172
        $org = new Organization();
173
        $name = new OrganizationName($name);
174
        $org->setOrganizationName($name);
175
        $org->setId($id);
176
        $orgContact = new OrganizationContact();
177
        $orgContact->setCity($city)
178
                    ->setStreet($street)
179
                    ->setHouseNumber($number);
180
        $org->setContact($orgContact);
181
182
        return $org;
183
184
    }
185
}