-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathIdentityDecoratorTest.php
More file actions
146 lines (125 loc) · 4.73 KB
/
IdentityDecoratorTest.php
File metadata and controls
146 lines (125 loc) · 4.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
<?php
declare(strict_types=1);
namespace Authorization\Test\TestCase;
use ArrayObject;
use Authorization\AuthorizationServiceInterface;
use Authorization\IdentityDecorator;
use BadMethodCallException;
use Cake\TestSuite\TestCase;
use PHPUnit\Framework\Attributes\DataProvider;
use stdClass;
use TestApp\Model\Entity\Article;
/**
* Test case for IdentityDecorator.
*/
class IdentityDecoratorTest extends TestCase
{
public static function constructorDataProvider()
{
return [
'array' => [
['id' => 1],
],
'ArrayAccess' => [
new ArrayObject(['id' => 1]),
],
'Entity' => [
new Article(['id' => 1]),
],
];
}
#[DataProvider('constructorDataProvider')]
public function testConstructorAccepted($data)
{
$auth = $this->createStub(AuthorizationServiceInterface::class);
$identity = new IdentityDecorator($auth, $data);
$this->assertSame($data['id'], $identity['id']);
}
public function testCanDelegation()
{
$resource = new stdClass();
$auth = $this->createMock(AuthorizationServiceInterface::class);
$identity = new IdentityDecorator($auth, ['id' => 1]);
$auth->expects($this->once())
->method('can')
->with($identity, 'update', $resource)
->willReturn(true);
$this->assertTrue($identity->can('update', $resource));
}
public function testApplyScopeDelegation()
{
$resource = new stdClass();
$auth = $this->createMock(AuthorizationServiceInterface::class);
$identity = new IdentityDecorator($auth, ['id' => 1]);
$auth->expects($this->once())
->method('applyScope')
->with($identity, 'update', $resource)
->willReturn(true);
$this->assertTrue($identity->applyScope('update', $resource));
}
public function testApplyScopeAdditionalParams()
{
$resource = new stdClass();
$auth = $this->createMock(AuthorizationServiceInterface::class);
$identity = new IdentityDecorator($auth, ['id' => 1]);
$auth->expects($this->once())
->method('applyScope')
->with($identity, 'update', $resource, 'first argument', false)
->willReturn(true);
$this->assertTrue($identity->applyScope('update', $resource, 'first argument', false));
}
public function testCall()
{
$data = new Article(['id' => 1]);
$auth = $this->createStub(AuthorizationServiceInterface::class);
$identity = new IdentityDecorator($auth, $data);
$this->assertFalse(method_exists($identity, 'isDirty'), 'method not defined on decorator');
$this->assertTrue($identity->isDirty('id'), 'method is callable though.');
}
public function testCallArray()
{
$this->expectException(BadMethodCallException::class);
$data = ['id' => 1];
$auth = $this->createStub(AuthorizationServiceInterface::class);
$identity = new IdentityDecorator($auth, $data);
$identity->boom();
}
public function testArrayAccessImplementation()
{
$data = new ArrayObject(['id' => 1]);
$auth = $this->createStub(AuthorizationServiceInterface::class);
$identity = new IdentityDecorator($auth, $data);
$this->assertTrue(isset($identity['id']));
$this->assertFalse(isset($identity['nope']));
$this->assertSame(1, $identity['id']);
unset($identity['id']);
$this->assertFalse(isset($identity['id']), 'Properties can be unset');
$identity['id'] = 99;
$this->assertSame(99, $identity['id'], 'Properties can be set.');
}
public function testGetOriginalData()
{
$data = ['id' => 2];
$auth = $this->createStub(AuthorizationServiceInterface::class);
$identity = new IdentityDecorator($auth, $data);
$this->assertSame($data, $identity->getOriginalData());
}
public function testGetOriginalDataWrappedObjectHasOriginalData()
{
$data = ['id' => 2];
$auth = $this->createStub(AuthorizationServiceInterface::class);
$inner = new IdentityDecorator($auth, $data);
$identity = new IdentityDecorator($auth, $inner);
$this->assertSame($data, $identity->getOriginalData());
}
public function testGetProperty()
{
$data = new Article(['id' => 2]);
$auth = $this->createStub(AuthorizationServiceInterface::class);
$identity = new IdentityDecorator($auth, $data);
$this->assertTrue(isset($identity->id));
$this->assertSame($data->id, $identity->id);
$this->assertFalse(isset($identity->unknown));
$this->assertNull($identity->unknown);
}
}