Issues (3641)

Client/Authentication/AuthenticationClientTest.php (1 issue)

1
<?php
2
3
/**
4
 * Copyright © 2016-present Spryker Systems GmbH. All rights reserved.
5
 * Use of this software requires acceptance of the Evaluation License Agreement. See LICENSE file.
6
 */
7
8
namespace SprykerTest\Client\Authentication;
9
10
use Codeception\Test\Unit;
11
use Generated\Shared\Transfer\GlueAuthenticationResponseTransfer;
12
use Generated\Shared\Transfer\OauthResponseTransfer;
13
use Spryker\Client\Authentication\AuthenticationDependencyProvider;
14
use Spryker\Client\Authentication\Exception\MissingServerPluginException;
15
use Spryker\Shared\AuthenticationExtension\Dependency\Plugin\AuthenticationServerPluginInterface;
16
17
/**
18
 * Auto-generated group annotations
19
 *
20
 * @group SprykerTest
21
 * @group Client
22
 * @group Authentication
23
 * @group AuthenticationClientTest
24
 * Add your own group annotations below this line
25
 */
26
class AuthenticationClientTest extends Unit
27
{
28
    /**
29
     * @var \SprykerTest\Client\Authentication\AuthenticationClientTester
30
     */
31
    protected $tester;
32
33
    /**
34
     * @var string
35
     */
36
    protected const FAKE_ACCESS_TOKEN = 'FAKE_ACCESS_TOKEN';
37
38
    /**
39
     * @return void
40
     */
41
    public function testAuthenticateIsSuccessful(): void
42
    {
43
        // Arrange
44
        $this->setAuthenticationServer();
45
        $authenticateClient = $this->tester->getLocator()->authentication()->client();
46
        $glueAuthenticationRequestTransfer = $this->tester->haveGlueAuthenticationRequestTransfer();
47
48
        //Act
49
        $glueAuthenticationResponseTransfer = $authenticateClient->authenticate($glueAuthenticationRequestTransfer);
50
51
        //Assert
52
        $this->assertTrue($glueAuthenticationResponseTransfer->getOauthResponse()->getIsValid());
53
        $this->assertNotEmpty($glueAuthenticationResponseTransfer->getOauthResponse()->getAccessToken());
54
    }
55
56
    /**
57
     * @return void
58
     */
59
    public function testAuthenticateThrowsMissingServerPluginException(): void
60
    {
61
        //Arrange
62
        $authenticateClient = $this->tester->getLocator()->authentication()->client();
63
        $glueAuthenticationRequestTransfer = $this->tester->haveGlueAuthenticationRequestTransfer();
64
65
        //Assert
66
        $this->expectException(MissingServerPluginException::class);
67
        $this->expectExceptionMessage('Missing instance of `Spryker\Shared\AuthenticationExtension\Dependency\Plugin\AuthenticationServerPluginInterface`! Authentication server needs to be configured.');
68
69
        //Act
70
        $glueAuthenticationResponseTransfer = $authenticateClient->authenticate($glueAuthenticationRequestTransfer);
0 ignored issues
show
The assignment to $glueAuthenticationResponseTransfer is dead and can be removed.
Loading history...
71
    }
72
73
    /**
74
     * @return void
75
     */
76
    protected function setAuthenticationServer(): void
77
    {
78
        $this->tester->setDependency(
79
            AuthenticationDependencyProvider::PLUGINS_AUTHENTICATION_SERVER,
80
            [$this->createAuthenticationServerPluginMockMock()],
81
        );
82
    }
83
84
    /**
85
     * @return \PHPUnit\Framework\MockObject\MockObject|\Spryker\Shared\AuthenticationExtension\Dependency\Plugin\AuthenticationServerPluginInterface
86
     */
87
    protected function createAuthenticationServerPluginMockMock(): AuthenticationServerPluginInterface
88
    {
89
        $oauthResponseTransfer = (new OauthResponseTransfer())
90
            ->setIsValid(true)
91
            ->setAccessToken(static::FAKE_ACCESS_TOKEN);
92
        $glueAuthenticationResponseTransfer = (new GlueAuthenticationResponseTransfer())
93
            ->setOauthResponse($oauthResponseTransfer);
94
95
        $authenticationServerPluginMock = $this->createMock(AuthenticationServerPluginInterface::class);
96
        $authenticationServerPluginMock->expects($this->once())
97
            ->method('authenticate')
98
            ->willReturn($glueAuthenticationResponseTransfer);
99
100
        return $authenticationServerPluginMock;
101
    }
102
}
103