Completed
Push — develop ( e2b887...c09155 )
by
unknown
12:13
created

provideOrganizationInterfaceFunctionValues()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 39
Code Lines 34

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 39
rs 8.8571
cc 1
eloc 34
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 OrganizationsTest\Entity;
12
13
use Auth\Entity\User;
14
use Core\Entity\Collection\ArrayCollection;
15
use Core\Entity\Hydrator\EntityHydrator;
16
use Core\Entity\Permissions;
17
use Core\Entity\PermissionsInterface;
18
use Organizations\Entity\Organization;
19
use Organizations\Entity\OrganizationContact;
20
use Organizations\Entity\OrganizationName;
21
use Organizations\Entity\OrganizationReference;
22
23
/**
24
 * Test the OrganizationReference entity.
25
 *
26
 * @covers \Organizations\Entity\OrganizationReference
27
 * @author Mathias Gelhausen <[email protected]>
28
 * @group Organizations
29
 * @group Organizations.Entity
30
 */
31
class OrganizationReferenceTest extends \PHPUnit_Framework_TestCase
32
{
33
34
    /**
35
     * Are the correct interfaces implemented?
36
     *
37
     */
38
    public function testOrganizationReferenceImplementsInterfaces()
39
    {
40
        $target = $this->getMockBuilder('\Organizations\Entity\OrganizationReference')
41
                       ->disableOriginalConstructor()
42
                       ->getMock();
43
44
        $this->assertInstanceOf('\Organizations\Entity\OrganizationReferenceInterface', $target);
45
        $this->assertInstanceOf('\Organizations\Entity\OrganizationInterface', $target);
46
    }
47
48
    /**
49
     * Does loading reference work as expected?
50
     * Does all interface methods work as expected?
51
     */
52
    public function testLoadingOrganizationAndInterfaceImplementation()
53
    {
54
        foreach (array('owner', 'employee', 'none') as $type) {
55
56
            $organization = new Organization();
57
58
            $repository = $this->getMockBuilder('\Organizations\Repository\Organization')
59
                               ->disableOriginalConstructor()->getMock();
60
61
            $repository->expects($this->once())
62
                       ->method('findByUser')
63
                       ->with('1234')
64
                       ->willReturn('owner' == $type ? $organization : null);
65
66
            if ('owner' == $type) {
67
                $repository->expects($this->never())
68
                           ->method('findByEmployee');
69
            } else {
70
                $repository->expects($this->once())
71
                           ->method('findByEmployee')
72
                           ->with('1234')
73
                           ->willReturn('employee' == $type ? $organization : null);
74
            }
75
76
            $target = new OrganizationReference('1234', $repository);
77
78
            if ('none' == $type) {
79
                $this->assertNull($target->getOrganization());
80
                $this->assertFalse($target->hasAssociation());
81
                $this->assertFalse($target->isOwner());
82
                $this->assertFalse($target->isEmployee());
83
            } else {
84
                $this->assertSame($organization, $target->getOrganization());
85
                $this->assertTrue($target->hasAssociation());
86
                if ('owner' == $type) {
87
                    $this->assertTrue($target->isOwner());
88
                    $this->assertFalse($target->isEmployee());
89
                } else {
90
                    $this->assertFalse($target->isOwner());
91
                    $this->assertTrue($target->isEmployee());
92
                }
93
            }
94
        }
95
    }
96
97
    /**
98
     *
99
     * @dataProvider provideOrganizationInterfaceFunctionValues
100
     *
101
     */
102
    public function testOrganizationInterfaceMethodsReturnsSelfIfNoAssociation($func, $args)
103
    {
104
        $rep = $this->getMockBuilder('\Organizations\Repository\Organization')
105
                    ->setMethods(array('findByUser', 'findByEmployee'))
106
                    ->disableOriginalConstructor()->getMock();
107
108
        $target = new OrganizationReference('1234', $rep);
109
        if (!is_array($func)) {
110
            $func = array($func);
111
            $args = array($args);
112
        }
113
114
        for ($i=0,$c=count($func); $i<$c; $i+=1) {
115
            $f = $func[$i];
116
            $a = $args[$i];
117
118
            $this->assertSame($target, call_user_func_array(array($target, $f), $a));
119
        }
120
    }
121
122
    /**
123
     * @dataProvider provideOrganizationInterfaceFunctionValues
124
     *
125
     */
126
    public function testOrganizationInterfaceMethodsReturnsExpectedValues($func, $args, $expected, $assertionType = 'same')
127
    {
128
        $organization = new Organization();
129
        $rep = $this->getMockBuilder('\Organizations\Repository\Organization')
130
                    ->disableOriginalConstructor()->getMock();
131
        $rep->method('findByUser')->willReturn($organization);
132
133
        $target = new OrganizationReference('1234', $rep);
134
135
        if (!is_array($func)) {
136
            $func = array($func); $args = array($args); $expected = array($expected);
137
        }
138
139
        for ($i=0, $c=count($func); $i<$c; $i+=1) {
140
            $f = $func[$i];
141
            $a = $args[$i];
142
            $e = $expected[$i];
143
144
            if ('__self__' === $e) { $e = $target; }
145
146
            $actual = call_user_func_array(array($target, $f), $a);
147
148
            if ('instance' == $assertionType) {
149
                $this->assertInstanceOf($e, $actual);
150
            } else {
151
                $this->assertSame($e, $actual);
152
            }
153
        }
154
    }
155
156
    public function provideOrganizationInterfaceFunctionValues()
157
    {
158
        $hydrator = new EntityHydrator();
159
        $date = new \DateTime();
160
        $parent = new Organization();
161
        $name = new OrganizationName('noname');
162
        $emps = new ArrayCollection();
163
        $user = new User();
164
        $user->setId('543');
165
        $perms = new Permissions();
166
        $contact = new OrganizationContact();
167
168
        return array(
169
            array(array('__set', '__get', '__isset'), array(array('id', '4321'), array('id'), array('id')), array('__self__', '4321', true)),
170
            array('setHydrator', array($hydrator), '__self__'),
171
            array('getHydrator', array(), '\Zend\Stdlib\Hydrator\HydratorInterface', 'instance'),
172
            array(array('setId', 'getId'), array(array('4321'), array()), array('__self__', '4321')),
173
            array(array('setDateCreated', 'getDateCreated'), array(array($date), array()), array('__self__', $date)),
174
            array(array('setDateModified', 'getDateModified'), array(array($date), array()), array('__self__', $date)),
175
            array(array('setParent', 'getParent'), array(array($parent), array()), array('__self__', $parent)),
176
            array(array('setContact', 'getContact'), array(array($contact), array()), array('__self__', $contact)),
177
            array('isHiringOrganization', array(), false),
178
            array('getHiringOrganizations', array(), null),
179
            array(array('setOrganizationName', 'getOrganizationName'), array(array($name), array()), array('__self__', $name)),
180
            array(array('setDescription', 'getDescription'), array(array('nodesc'), array()), array('__self__', 'nodesc')),
181
            array(array('setEmployees', 'getEmployees'), array(array($emps), array()), array('__self__', $emps)),
182
            array('getEmployee', array('4321'), null),
183
            array(array('setUser', 'getUser', 'getPermissionsUserIds'),
184
                  array(array($user), array(), array()),
185
                  array('__self__', $user, array(PermissionsInterface::PERMISSION_ALL => array($user->getId())))
186
            ),
187
            array('getJobs', array(), null),
188
            array(array('setPermissions', 'getPermissions'), array(array($perms), array()), array('__self__', $perms)),
189
            array('getPermissionsResourceId', array(), 'organization:'),
190
            array('getSearchableProperties', array(), array()),
191
            array(array('setKeywords', 'getKeywords'), array(array(array('no', 'keywords')), array()), array(null, null)),
192
            array('clearKeywords', array(), null),
193
        );
194
    }
195
}