|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/* |
|
6
|
|
|
* The MIT License (MIT) |
|
7
|
|
|
* |
|
8
|
|
|
* Copyright (c) 2014-2018 Spomky-Labs |
|
9
|
|
|
* |
|
10
|
|
|
* This software may be modified and distributed under the terms |
|
11
|
|
|
* of the MIT license. See the LICENSE file for details. |
|
12
|
|
|
*/ |
|
13
|
|
|
|
|
14
|
|
|
namespace OAuth2Framework\Component\Server\Core\Tests\Client; |
|
15
|
|
|
|
|
16
|
|
|
use OAuth2Framework\Component\Server\Core\Client\Client; |
|
17
|
|
|
use OAuth2Framework\Component\Server\Core\Client\ClientId; |
|
18
|
|
|
use OAuth2Framework\Component\Server\Core\Client\ClientRepository; |
|
19
|
|
|
use OAuth2Framework\Component\Server\Core\Client\Command\ChangeOwnerCommand; |
|
20
|
|
|
use OAuth2Framework\Component\Server\Core\Client\Command\ChangeOwnerCommandHandler; |
|
21
|
|
|
use OAuth2Framework\Component\Server\Core\Client\Command\CreateClientCommand; |
|
22
|
|
|
use OAuth2Framework\Component\Server\Core\Client\Command\CreateClientCommandHandler; |
|
23
|
|
|
use OAuth2Framework\Component\Server\Core\Client\Command\DeleteClientCommand; |
|
24
|
|
|
use OAuth2Framework\Component\Server\Core\Client\Command\DeleteClientCommandHandler; |
|
25
|
|
|
use OAuth2Framework\Component\Server\Core\Client\Command\UpdateClientCommand; |
|
26
|
|
|
use OAuth2Framework\Component\Server\Core\Client\Command\UpdateClientCommandHandler; |
|
27
|
|
|
use OAuth2Framework\Component\Server\Core\DataBag\DataBag; |
|
28
|
|
|
use OAuth2Framework\Component\Server\Core\UserAccount\UserAccountId; |
|
29
|
|
|
use PHPUnit\Framework\TestCase; |
|
30
|
|
|
use Prophecy\Argument; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @group ClientCommand |
|
34
|
|
|
*/ |
|
35
|
|
|
final class ClientCommandTest extends TestCase |
|
36
|
|
|
{ |
|
37
|
|
|
/** |
|
38
|
|
|
* @test |
|
39
|
|
|
*/ |
|
40
|
|
|
public function anClientCanBeCreatedUsingTheCommand() |
|
41
|
|
|
{ |
|
42
|
|
|
$clientId = ClientId::create('CLIENT_ID'); |
|
43
|
|
|
$command = CreateClientCommand::create( |
|
44
|
|
|
$clientId, |
|
45
|
|
|
UserAccountId::create('USER_ACCOUNT_ID'), |
|
46
|
|
|
DataBag::create([]) |
|
47
|
|
|
); |
|
48
|
|
|
|
|
49
|
|
|
$repository = $this->prophesize(ClientRepository::class); |
|
50
|
|
|
$repository->find($clientId)->willReturn(null)->shouldBeCalled(); |
|
51
|
|
|
$repository->save(Argument::type(Client::class))->shouldBeCalled(); |
|
52
|
|
|
|
|
53
|
|
|
$handler = new CreateClientCommandHandler( |
|
54
|
|
|
$repository->reveal() |
|
55
|
|
|
); |
|
56
|
|
|
$handler->handle($command); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @test |
|
61
|
|
|
*/ |
|
62
|
|
|
public function anClientCanBeUpdatedUsingTheCommand() |
|
63
|
|
|
{ |
|
64
|
|
|
$clientId = ClientId::create('CLIENT_ID'); |
|
65
|
|
|
$client = Client::createEmpty(); |
|
66
|
|
|
$client = $client->create( |
|
67
|
|
|
$clientId, |
|
68
|
|
|
DataBag::create([]), |
|
69
|
|
|
UserAccountId::create('USER_ACCOUNT_ID') |
|
70
|
|
|
); |
|
71
|
|
|
$command = UpdateClientCommand::create( |
|
72
|
|
|
$clientId, |
|
73
|
|
|
DataBag::create([]) |
|
74
|
|
|
); |
|
75
|
|
|
|
|
76
|
|
|
$repository = $this->prophesize(ClientRepository::class); |
|
77
|
|
|
$repository->find($clientId)->willReturn($client)->shouldBeCalled(); |
|
78
|
|
|
$repository->save(Argument::type(Client::class))->shouldBeCalled(); |
|
79
|
|
|
|
|
80
|
|
|
$handler = new UpdateClientCommandHandler( |
|
81
|
|
|
$repository->reveal() |
|
82
|
|
|
); |
|
83
|
|
|
$handler->handle($command); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* @test |
|
88
|
|
|
*/ |
|
89
|
|
|
public function anClientOwnerCanBeChangedUsingTheCommand() |
|
90
|
|
|
{ |
|
91
|
|
|
$clientId = ClientId::create('CLIENT_ID'); |
|
92
|
|
|
$client = Client::createEmpty(); |
|
93
|
|
|
$client = $client->create( |
|
94
|
|
|
$clientId, |
|
95
|
|
|
DataBag::create([]), |
|
96
|
|
|
UserAccountId::create('USER_ACCOUNT_ID') |
|
97
|
|
|
); |
|
98
|
|
|
$command = ChangeOwnerCommand::create( |
|
99
|
|
|
$clientId, |
|
100
|
|
|
UserAccountId::create('NEW_USER_ACCOUNT') |
|
101
|
|
|
); |
|
102
|
|
|
|
|
103
|
|
|
$repository = $this->prophesize(ClientRepository::class); |
|
104
|
|
|
$repository->find($clientId)->willReturn($client)->shouldBeCalled(); |
|
105
|
|
|
$repository->save(Argument::type(Client::class))->shouldBeCalled(); |
|
106
|
|
|
|
|
107
|
|
|
$handler = new ChangeOwnerCommandHandler( |
|
108
|
|
|
$repository->reveal() |
|
109
|
|
|
); |
|
110
|
|
|
$handler->handle($command); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* @test |
|
115
|
|
|
*/ |
|
116
|
|
|
public function anClientCanBeDeletedUsingTheCommand() |
|
117
|
|
|
{ |
|
118
|
|
|
$clientId = ClientId::create('CLIENT_ID'); |
|
119
|
|
|
$client = Client::createEmpty(); |
|
120
|
|
|
$client = $client->create( |
|
121
|
|
|
$clientId, |
|
122
|
|
|
DataBag::create([]), |
|
123
|
|
|
UserAccountId::create('USER_ACCOUNT_ID') |
|
124
|
|
|
); |
|
125
|
|
|
$command = DeleteClientCommand::create( |
|
126
|
|
|
$clientId |
|
127
|
|
|
); |
|
128
|
|
|
|
|
129
|
|
|
$repository = $this->prophesize(ClientRepository::class); |
|
130
|
|
|
$repository->find($clientId)->willReturn($client)->shouldBeCalled(); |
|
131
|
|
|
$repository->save(Argument::type(Client::class))->shouldBeCalled(); |
|
132
|
|
|
|
|
133
|
|
|
$handler = new DeleteClientCommandHandler( |
|
134
|
|
|
$repository->reveal() |
|
135
|
|
|
); |
|
136
|
|
|
$handler->handle($command); |
|
137
|
|
|
} |
|
138
|
|
|
} |
|
139
|
|
|
|