Failed Conditions
Push — master ( 516359...a8b39a )
by Florent
04:00
created

SchemaContext   C

Complexity

Total Complexity 25

Size/Duplication

Total Lines 380
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 37

Importance

Changes 0
Metric Value
wmc 25
lcom 1
cbo 37
dl 0
loc 380
rs 5
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * The MIT License (MIT)
7
 *
8
 * Copyright (c) 2014-2017 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\Tests\Context;
15
16
use Assert\Assertion;
17
use Behat\Behat\Context\Context;
18
use Behat\Behat\Hook\Scope\BeforeScenarioScope;
19
use OAuth2Framework\Component\Server\Event\AccessToken\AccessTokenCreatedEvent;
20
use OAuth2Framework\Component\Server\Event\AccessToken\AccessTokenRevokedEvent;
21
use OAuth2Framework\Component\Server\Event\AuthCode\AuthCodeCreatedEvent;
22
use OAuth2Framework\Component\Server\Event\AuthCode\AuthCodeMarkedAsUsedEvent;
23
use OAuth2Framework\Component\Server\Event\AuthCode\AuthCodeRevokedEvent;
24
use OAuth2Framework\Component\Server\Event\Client\ClientCreatedEvent;
25
use OAuth2Framework\Component\Server\Event\Client\ClientDeletedEvent;
26
use OAuth2Framework\Component\Server\Event\Client\ClientOwnerChangedEvent;
27
use OAuth2Framework\Component\Server\Event\Client\ClientParametersUpdatedEvent;
28
use OAuth2Framework\Component\Server\Event\InitialAccessToken\InitialAccessTokenCreatedEvent;
29
use OAuth2Framework\Component\Server\Event\InitialAccessToken\InitialAccessTokenRevokedEvent;
30
use OAuth2Framework\Component\Server\Event\PreConfiguredAuthorization\PreConfiguredAuthorizationCreatedEvent;
31
use OAuth2Framework\Component\Server\Event\PreConfiguredAuthorization\PreConfiguredAuthorizationRevokedEvent;
32
use OAuth2Framework\Component\Server\Event\RefreshToken\AccessTokenAddedToRefreshTokenEvent;
33
use OAuth2Framework\Component\Server\Event\RefreshToken\RefreshTokenCreatedEvent;
34
use OAuth2Framework\Component\Server\Event\RefreshToken\RefreshTokenRevokedEvent;
35
use OAuth2Framework\Component\Server\Model\AccessToken\AccessToken;
36
use OAuth2Framework\Component\Server\Model\AccessToken\AccessTokenId;
37
use OAuth2Framework\Component\Server\Model\AuthCode\AuthCode;
38
use OAuth2Framework\Component\Server\Model\AuthCode\AuthCodeId;
39
use OAuth2Framework\Component\Server\Model\Client\Client;
40
use OAuth2Framework\Component\Server\Model\Client\ClientId;
41
use OAuth2Framework\Component\Server\Model\DataBag\DataBag;
42
use OAuth2Framework\Component\Server\Model\InitialAccessToken\InitialAccessToken;
43
use OAuth2Framework\Component\Server\Model\InitialAccessToken\InitialAccessTokenId;
44
use OAuth2Framework\Component\Server\Model\PreConfiguredAuthorization\PreConfiguredAuthorization;
45
use OAuth2Framework\Component\Server\Model\PreConfiguredAuthorization\PreConfiguredAuthorizationId;
46
use OAuth2Framework\Component\Server\Model\RefreshToken\RefreshToken;
47
use OAuth2Framework\Component\Server\Model\RefreshToken\RefreshTokenId;
48
use OAuth2Framework\Component\Server\Model\ResourceServer\ResourceServerId;
49
use OAuth2Framework\Component\Server\Model\UserAccount\UserAccountId;
50
use OAuth2Framework\Component\Server\Schema\DomainObjectInterface;
51
52
final class SchemaContext implements Context
53
{
54
    /**
55
     * @var null|DomainObjectInterface
56
     */
57
    private $domainObject = null;
58
59
    /**
60
     * @var null|string
61
     */
62
    private $jsonObject = null;
63
64
    /**
65
     * @var ApplicationContext
66
     */
67
    private $applicationContext;
68
69
    /**
70
     * @BeforeScenario
71
     *
72
     * @param BeforeScenarioScope $scope
73
     */
74
    public function gatherContexts(BeforeScenarioScope $scope)
75
    {
76
        $environment = $scope->getEnvironment();
77
78
        $this->applicationContext = $environment->getContext(ApplicationContext::class);
79
    }
80
81
    /**
82
     * @Given I have a valid Access Token Created Event object
83
     */
84
    public function iHaveAValidAccessTokenCreatedEventObject()
85
    {
86
        $this->domainObject = AccessTokenCreatedEvent::create(
87
            AccessTokenId::create('AccessTOKEN'),
88
            UserAccountId::create('UserACCOUNT'),
89
            ClientId::create('Client'),
90
            DataBag::createFromArray([]),
91
            DataBag::createFromArray([]),
92
            [],
93
            new \DateTimeImmutable('now +1 hour'),
94
            null,
95
            null
96
        );
97
    }
98
99
    /**
100
     * @Given I have a valid Access Token Revoked Event object
101
     */
102
    public function iHaveAValidAccessTokenRevokedEventObject()
103
    {
104
        $this->domainObject = AccessTokenRevokedEvent::create(
105
            AccessTokenId::create('AccessTOKEN')
106
        );
107
    }
108
109
    /**
110
     * @Given I have a valid Pre-Configured Authorization Created Event object
111
     */
112
    public function iHaveAValidPreConfiguredAuthorizationCreatedEventObject()
113
    {
114
        $this->domainObject = PreConfiguredAuthorizationCreatedEvent::create(
115
            PreConfiguredAuthorizationId::create('PreConfiguredAuthorization'),
116
            ClientId::create('Client'),
117
            UserAccountId::create('UserACCOUNT'),
118
            []
119
        );
120
    }
121
122
    /**
123
     * @Given I have a valid Pre-Configured Authorization Revoked Event object
124
     */
125
    public function iHaveAValidPreConfiguredAuthorizationRevokedEventObject()
126
    {
127
        $this->domainObject = PreConfiguredAuthorizationRevokedEvent::create(
128
            PreConfiguredAuthorizationId::create('PreConfiguredAuthorization')
129
        );
130
    }
131
132
    /**
133
     * @Given I have a valid Initial Access Token Created Event object
134
     */
135
    public function iHaveAValidInitialAccessTokenCreatedEventObject()
136
    {
137
        $this->domainObject = InitialAccessTokenCreatedEvent::create(
138
            InitialAccessTokenId::create('InitialAccessTOKEN'),
139
            UserAccountId::create('UserACCOUNT'),
140
            new \DateTimeImmutable('now +1 hour')
141
        );
142
    }
143
144
    /**
145
     * @Given I have a valid Initial Access Token Revoked Event object
146
     */
147
    public function iHaveAValidInitialAccessTokenRevokedEventObject()
148
    {
149
        $this->domainObject = InitialAccessTokenRevokedEvent::create(
150
            InitialAccessTokenId::create('InitialAccessTOKEN')
151
        );
152
    }
153
154
    /**
155
     * @Given I have a valid Refresh Token Created Event object
156
     */
157
    public function iHaveAValidRefreshTokenCreatedEventObject()
158
    {
159
        $this->domainObject = RefreshTokenCreatedEvent::create(
160
            RefreshTokenId::create('RefreshTOKEN'),
161
            UserAccountId::create('UserACCOUNT'),
162
            ClientId::create('Client'),
163
            DataBag::createFromArray([]),
164
            DataBag::createFromArray([]),
165
            new \DateTimeImmutable('now +1 hour'),
166
            [],
167
            null
168
        );
169
    }
170
171
    /**
172
     * @Given I have a valid Access Token Added To Refresh Token Event object
173
     */
174
    public function iHaveAValidAccessTokenAddedToRefreshTokenEventObject()
175
    {
176
        $this->domainObject = AccessTokenAddedToRefreshTokenEvent::create(
177
            RefreshTokenId::create('RefreshTOKEN'),
178
            AccessTokenId::create('AccessToken')
179
        );
180
    }
181
182
    /**
183
     * @Given I have a valid Refresh Token Revoked Event object
184
     */
185
    public function iHaveAValidRefreshTokenRevokedEventObject()
186
    {
187
        $this->domainObject = RefreshTokenRevokedEvent::create(
188
            RefreshTokenId::create('RefreshTOKEN')
189
        );
190
    }
191
192
    /**
193
     * @Given I have a valid Client Created Event object
194
     */
195
    public function iHaveAValidClientCreatedEventObject()
196
    {
197
        $this->domainObject = ClientCreatedEvent::create(
198
            ClientId::create('Client'),
199
            DataBag::createFromArray([]),
200
            UserAccountId::create('UserACCOUNT')
201
        );
202
    }
203
204
    /**
205
     * @Given I have a valid Client Owner Changed Event object
206
     */
207
    public function iHaveAValidClientOwnerChangedEventObject()
208
    {
209
        $this->domainObject = ClientOwnerChangedEvent::create(
210
            ClientId::create('Client'),
211
            UserAccountId::create('UserACCOUNT')
212
        );
213
    }
214
215
    /**
216
     * @Given I have a valid Client Parameters Updated Event object
217
     */
218
    public function iHaveAValidClientParametersUpdatedEventObject()
219
    {
220
        $this->domainObject = ClientParametersUpdatedEvent::create(
221
            ClientId::create('Client'),
222
            DataBag::createFromArray([])
223
        );
224
    }
225
226
    /**
227
     * @Given I have a valid Client Deleted Event object
228
     */
229
    public function iHaveAValidClientDeletedEventObject()
230
    {
231
        $this->domainObject = ClientDeletedEvent::create(
232
            ClientId::create('Client')
233
        );
234
    }
235
236
    /**
237
     * @Given I have a valid Authorization Code Created Event object
238
     */
239
    public function iHaveAValidAuthorizationCodeCreatedEventObject()
240
    {
241
        $this->domainObject = AuthCodeCreatedEvent::create(
242
            AuthCodeId::create('AccessTOKEN'),
243
            ClientId::create('Client'),
244
            UserAccountId::create('UserACCOUNT'),
245
            [],
246
            'redirect_uri',
247
            new \DateTimeImmutable('now +1 hour'),
248
            DataBag::createFromArray([]),
249
            DataBag::createFromArray([]),
250
            [],
251
            false,
252
            null
253
        );
254
    }
255
256
    /**
257
     * @Given I have a valid Authorization Code Marked As Used Event object
258
     */
259
    public function iHaveAValidAuthorizationCodeMarkedAsUsedEventObject()
260
    {
261
        $this->domainObject = AuthCodeMarkedAsUsedEvent::create(
262
            AuthCodeId::create('AccessTOKEN')
263
        );
264
    }
265
266
    /**
267
     * @Given I have a valid Authorization Code Revoked Event object
268
     */
269
    public function iHaveAValidAuthorizationCodeRevokedEventObject()
270
    {
271
        $this->domainObject = AuthCodeRevokedEvent::create(
272
            AuthCodeId::create('AccessTOKEN')
273
        );
274
    }
275
276
    /**
277
     * @Given I have an Access Token Object
278
     */
279
    public function iHaveAnAccessTokenObject()
280
    {
281
        $accessToken = AccessToken::createEmpty();
282
        $accessToken = $accessToken->create(
283
            AccessTokenId::create('AccessTokenId'),
284
            UserAccountId::create('UserAccountId'),
285
            ClientId::create('ClientId'),
286
            DataBag::createFromArray([
287
                'foo' => 'bar',
288
            ]),
289
            DataBag::createFromArray([
290
                'plic' => 'ploc',
291
            ]),
292
            ['openid'],
293
            new \DateTimeImmutable('now +1 hour'),
294
            RefreshTokenId::create('RefreshTokenId'),
295
            ResourceServerId::create('ResourceServerId')
296
        );
297
        $this->domainObject = $accessToken;
298
    }
299
300
    /**
301
     * @Given I have an Initial Access Token Object
302
     */
303
    public function iHaveAnInitialAccessTokenObject()
304
    {
305
        $initialAccessToken = InitialAccessToken::createEmpty();
306
        $initialAccessToken = $initialAccessToken->create(
307
            InitialAccessTokenId::create('InitialAccessTokenId'),
308
            UserAccountId::create('UserAccountId'),
309
            new \DateTimeImmutable('now +1 hour')
310
        );
311
        $initialAccessToken = $initialAccessToken->markAsRevoked();
312
        $this->domainObject = $initialAccessToken;
313
    }
314
315
    /**
316
     * @Given I have an Authorization Code Object
317
     */
318
    public function iHaveAnAuthorizationCodeObject()
319
    {
320
        $authCode = AuthCode::createEmpty();
321
        $authCode = $authCode->create(
322
            AuthCodeId::create('AuthCodeId'),
323
            ClientId::create('ClientId'),
324
            UserAccountId::create('UserAccountId'),
325
            [
326
                'foo' => 'bar',
327
            ],
328
            'redirect_uri',
329
            new \DateTimeImmutable('now +1 hour'),
330
            DataBag::createFromArray([
331
                'foo' => 'bar',
332
            ]),
333
            DataBag::createFromArray([
334
                'plic' => 'ploc',
335
            ]),
336
            ['openid'],
337
            true,
338
            ResourceServerId::create('ResourceServerId')
339
        );
340
        $authCode = $authCode->markAsUsed();
341
        $authCode = $authCode->markAsRevoked();
342
        $this->domainObject = $authCode;
343
    }
344
345
    /**
346
     * @Given I have an Client Object
347
     */
348
    public function iHaveAnClientObject()
349
    {
350
        $client = Client::createEmpty();
351
        $client = $client->create(
352
            ClientId::create('ClientId'),
353
            DataBag::createFromArray([
354
                'foo' => 'bar',
355
            ]),
356
            UserAccountId::create('UserAccountId')
357
        );
358
        $client = $client->withOwnerId(UserAccountId::create('UserAccountId'));
359
        $client = $client->withParameters(DataBag::createFromArray(['foo' => 'bar']));
360
        $client = $client->markAsDeleted();
361
        $this->domainObject = $client;
362
    }
363
364
    /**
365
     * @Given I have a Pre-Configured Authorization Object
366
     */
367
    public function iHaveAPreConfiguredAuthorizationObject()
368
    {
369
        $preConfiguredAuthorization = PreConfiguredAuthorization::createEmpty();
370
        $preConfiguredAuthorization = $preConfiguredAuthorization->create(
371
            PreConfiguredAuthorizationId::create('PreConfiguredAuthorizationId'),
372
            UserAccountId::create('UserAccountId'),
373
            ClientId::create('ClientId'),
374
            ['openid']
375
        );
376
        $preConfiguredAuthorization = $preConfiguredAuthorization->markAsRevoked();
377
        $this->domainObject = $preConfiguredAuthorization;
378
    }
379
380
    /**
381
     * @Given I have an Refresh Token Object
382
     */
383
    public function iHaveAnRefreshTokenObject()
384
    {
385
        $refreshToken = RefreshToken::createEmpty();
386
        $refreshToken = $refreshToken->create(
387
            RefreshTokenId::create('RefreshTokenId'),
388
            UserAccountId::create('UserAccountId'),
389
            ClientId::create('ClientId'),
390
            DataBag::createFromArray([
391
                'foo' => 'bar',
392
            ]),
393
            DataBag::createFromArray([
394
                'plic' => 'ploc',
395
            ]),
396
            ['openid'],
397
            new \DateTimeImmutable('now +1 hour'),
398
            ResourceServerId::create('ResourceServerId')
399
        );
400
        $refreshToken = $refreshToken->addAccessToken(
401
            AccessTokenId::create('AccessTokenId')
402
        );
403
        $refreshToken = $refreshToken->markAsRevoked();
404
        $this->domainObject = $refreshToken;
405
    }
406
407
    /**
408
     * @When I convert the Domain Object into a Json Object
409
     */
410
    public function iConvertTheDomainObjectIntoAJsonObject()
411
    {
412
        Assertion::notNull($this->domainObject, 'Domain object is not set.');
413
        $converter = $this->applicationContext->getApplication()->getDomainConverter();
414
        $jsonObject = $converter->toJson($this->domainObject);
415
        Assertion::string($jsonObject, 'Invalid JSON object.');
416
        $this->jsonObject = $jsonObject;
417
    }
418
419
    /**
420
     * @Then I can recover the event from the Json Object and its class is :class
421
     */
422
    public function iCanRecoverTheEventFromTheJsonObjectAndItsClassIs($class)
423
    {
424
        Assertion::string($this->jsonObject, 'Invalid JSON object.');
425
        $converter = $this->applicationContext->getApplication()->getDomainConverter();
426
        $domainObject = $converter->fromJson($this->jsonObject);
427
        Assertion::isInstanceOf($domainObject, DomainObjectInterface::class, 'Invalid domain object.');
428
        Assertion::isInstanceOf($domainObject, $class, sprintf('Invalid class. I got %s.', get_class($domainObject)));
429
        Assertion::eq($this->domainObject->jsonSerialize(), $domainObject->jsonSerialize());
430
    }
431
}
432