1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* File containing the UserTest class. |
5
|
|
|
* |
6
|
|
|
* @copyright Copyright (C) eZ Systems AS. All rights reserved. |
7
|
|
|
* @license For full copyright and license information view LICENSE file distributed with this source code. |
8
|
|
|
*/ |
9
|
|
|
namespace eZ\Publish\Core\Repository\Tests\Values\User; |
10
|
|
|
|
11
|
|
|
use eZ\Publish\API\Repository\Tests\Values\ValueObjectTestTrait; |
12
|
|
|
use eZ\Publish\API\Repository\Values\Content\Content; |
13
|
|
|
use eZ\Publish\API\Repository\Values\Content\VersionInfo; |
14
|
|
|
use eZ\Publish\Core\Repository\Values\User\User; |
15
|
|
|
use Mockery; |
16
|
|
|
use PHPUnit_Framework_TestCase; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Test internal integrity of @see \eZ\Publish\Core\Repository\Values\User\User ValueObject. |
20
|
|
|
*/ |
21
|
|
|
class UserTest extends PHPUnit_Framework_TestCase |
22
|
|
|
{ |
23
|
|
|
use ValueObjectTestTrait; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Test a new class and default values on properties. |
27
|
|
|
* |
28
|
|
|
* @covers \eZ\Publish\Core\Repository\Values\User\User::__construct |
29
|
|
|
*/ |
30
|
|
|
public function testNewClass() |
31
|
|
|
{ |
32
|
|
|
$user = new User(); |
33
|
|
|
|
34
|
|
|
$this->assertPropertiesCorrect( |
35
|
|
|
[ |
36
|
|
|
'login' => null, |
37
|
|
|
'email' => null, |
38
|
|
|
'passwordHash' => null, |
39
|
|
|
'hashAlgorithm' => null, |
40
|
|
|
'maxLogin' => null, |
41
|
|
|
'enabled' => false, |
42
|
|
|
], |
43
|
|
|
$user |
44
|
|
|
); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Test getName method. |
49
|
|
|
* |
50
|
|
|
* @covers \eZ\Publish\Core\Repository\Values\User\User::getName |
51
|
|
|
*/ |
52
|
|
View Code Duplication |
public function testGetName() |
53
|
|
|
{ |
54
|
|
|
$name = 'Translated name'; |
55
|
|
|
$contentMock = Mockery::mock(Content::class); |
56
|
|
|
$versionInfoMock = Mockery::mock(VersionInfo::class); |
57
|
|
|
|
58
|
|
|
$contentMock->shouldReceive('getVersionInfo')->andReturn($versionInfoMock); |
59
|
|
|
$versionInfoMock->shouldReceive('getName')->andReturn($name); |
60
|
|
|
|
61
|
|
|
$object = new User(['content' => $contentMock]); |
62
|
|
|
|
63
|
|
|
$this->assertEquals($name, $object->getName()); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Test retrieving missing property. |
68
|
|
|
* |
69
|
|
|
* @covers \eZ\Publish\API\Repository\Values\User\User::__get |
70
|
|
|
* @expectedException \eZ\Publish\API\Repository\Exceptions\PropertyNotFoundException |
71
|
|
|
*/ |
72
|
|
|
public function testMissingProperty() |
73
|
|
|
{ |
74
|
|
|
$user = new User(); |
75
|
|
|
$value = $user->notDefined; |
|
|
|
|
76
|
|
|
self::fail('Succeeded getting non existing property'); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @covers \eZ\Publish\Core\Repository\Values\User\User::getProperties |
81
|
|
|
*/ |
82
|
|
View Code Duplication |
public function testObjectProperties() |
83
|
|
|
{ |
84
|
|
|
$object = new User(); |
85
|
|
|
$properties = $object->attributes(); |
|
|
|
|
86
|
|
|
self::assertNotContains('internalFields', $properties, 'Internal property found '); |
87
|
|
|
self::assertContains('id', $properties, 'Property not found '); |
88
|
|
|
self::assertContains('fields', $properties, 'Property not found '); |
89
|
|
|
self::assertContains('versionInfo', $properties, 'Property not found '); |
90
|
|
|
self::assertContains('contentInfo', $properties, 'Property not found '); |
91
|
|
|
|
92
|
|
|
// check for duplicates and double check existence of property |
93
|
|
|
$propertiesHash = []; |
94
|
|
|
foreach ($properties as $property) { |
95
|
|
|
if (isset($propertiesHash[$property])) { |
96
|
|
|
self::fail("Property '{$property}' exists several times in properties list"); |
97
|
|
|
} elseif (!isset($object->$property)) { |
98
|
|
|
self::fail("Property '{$property}' does not exist on object, even though it was hinted to be there"); |
99
|
|
|
} |
100
|
|
|
$propertiesHash[$property] = 1; |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Test setting read only property. |
106
|
|
|
* |
107
|
|
|
* @covers \eZ\Publish\API\Repository\Values\User\User::__set |
108
|
|
|
* @expectedException \eZ\Publish\API\Repository\Exceptions\PropertyReadOnlyException |
109
|
|
|
*/ |
110
|
|
|
public function testReadOnlyProperty() |
111
|
|
|
{ |
112
|
|
|
$user = new User(); |
113
|
|
|
$user->login = 'user'; |
|
|
|
|
114
|
|
|
self::fail('Succeeded setting read only property'); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Test if property exists. |
119
|
|
|
* |
120
|
|
|
* @covers \eZ\Publish\API\Repository\Values\User\User::__isset |
121
|
|
|
*/ |
122
|
|
|
public function testIsPropertySet() |
123
|
|
|
{ |
124
|
|
|
$user = new User(); |
125
|
|
|
$value = isset($user->notDefined); |
126
|
|
|
self::assertEquals(false, $value); |
127
|
|
|
|
128
|
|
|
$value = isset($user->login); |
|
|
|
|
129
|
|
|
self::assertEquals(true, $value); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Test unsetting a property. |
134
|
|
|
* |
135
|
|
|
* @covers \eZ\Publish\API\Repository\Values\User\User::__unset |
136
|
|
|
* @expectedException \eZ\Publish\API\Repository\Exceptions\PropertyReadOnlyException |
137
|
|
|
*/ |
138
|
|
|
public function testUnsetProperty() |
139
|
|
|
{ |
140
|
|
|
$user = new User(['login' => 'admin']); |
141
|
|
|
unset($user->login); |
142
|
|
|
self::fail('Unsetting read-only property succeeded'); |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
|
Since your code implements the magic setter
_set
, this function will be called for any write access on an undefined variable. You can add the@property
annotation to your class or interface to document the existence of this variable.Since the property has write access only, you can use the @property-write annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.