|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Tests\Unit; |
| 6 | + |
| 7 | +use CodeMash\Auth; |
| 8 | +use CodeMash\Exceptions\RequestValidationException; |
| 9 | +use Generator; |
| 10 | + |
| 11 | +final class AuthTest extends CodeMashTestCase |
| 12 | +{ |
| 13 | + /** @var Auth */ |
| 14 | + private $auth; |
| 15 | + |
| 16 | + protected function setUp(): void |
| 17 | + { |
| 18 | + parent::setUp(); |
| 19 | + |
| 20 | + $this->auth = new Auth($this->client); |
| 21 | + } |
| 22 | + |
| 23 | + /** |
| 24 | + * @dataProvider provideTestAuthenticateFail |
| 25 | + */ |
| 26 | + public function testAuthenticateFail( |
| 27 | + array $params, |
| 28 | + string $expectedException, |
| 29 | + string $expectedExceptionMessage |
| 30 | + ): void { |
| 31 | + $this->expectException($expectedException); |
| 32 | + $this->expectExceptionMessage($expectedExceptionMessage); |
| 33 | + |
| 34 | + $this->auth->authenticate($params); |
| 35 | + } |
| 36 | + |
| 37 | + public static function provideTestAuthenticateFail(): Generator |
| 38 | + { |
| 39 | + yield 'Empty params' => [ |
| 40 | + '$params' => [], |
| 41 | + '$expectedException' => RequestValidationException::class, |
| 42 | + '$expectedExceptionMessage' => '"password" is required!', |
| 43 | + ]; |
| 44 | + |
| 45 | + yield 'Missing userName' => [ |
| 46 | + '$params' => ['password' => 'fooBar'], |
| 47 | + '$expectedException' => RequestValidationException::class, |
| 48 | + '$expectedExceptionMessage' => '"userName" is required!', |
| 49 | + ]; |
| 50 | + } |
| 51 | + |
| 52 | + public function testAuthenticate(): void |
| 53 | + { |
| 54 | + $params = [ |
| 55 | + 'password' => 'testPassword', |
| 56 | + 'userName' => 'testUserName', |
| 57 | + ]; |
| 58 | + |
| 59 | + $expectedResult = ['foo' => 'bar']; |
| 60 | + |
| 61 | + $this->client |
| 62 | + ->expects($this->once()) |
| 63 | + ->method('request') |
| 64 | + ->with( |
| 65 | + 'POST', |
| 66 | + 'v2/auth/credentials', |
| 67 | + [ |
| 68 | + 'headers' => [ |
| 69 | + 'Accept' => 'application/json', |
| 70 | + 'Content-Type' => 'application/json', |
| 71 | + ], |
| 72 | + 'body' => '{"password":"testPassword","userName":"testUserName"}', |
| 73 | + ] |
| 74 | + )->willReturn(['result' => $expectedResult]); |
| 75 | + |
| 76 | + $actualResult = $this->auth->authenticate($params); |
| 77 | + |
| 78 | + $this->assertSame($expectedResult, $actualResult); |
| 79 | + } |
| 80 | + |
| 81 | + public function testCheckAuth(): void |
| 82 | + { |
| 83 | + $expectedResult = ['foo' => 'bar']; |
| 84 | + |
| 85 | + $this->client |
| 86 | + ->expects($this->once()) |
| 87 | + ->method('request') |
| 88 | + ->with( |
| 89 | + 'POST', |
| 90 | + 'v2/auth/', |
| 91 | + [ |
| 92 | + 'headers' => [ |
| 93 | + 'Accept' => 'application/json', |
| 94 | + 'Content-Type' => 'application/json', |
| 95 | + ], |
| 96 | + ] |
| 97 | + )->willReturn(['result' => $expectedResult]); |
| 98 | + |
| 99 | + $actualResult = $this->auth->checkAuth(); |
| 100 | + |
| 101 | + $this->assertSame($expectedResult, $actualResult); |
| 102 | + } |
| 103 | + |
| 104 | + /** |
| 105 | + * @dataProvider provideTestLogoutFail |
| 106 | + */ |
| 107 | + public function testLogoutFail( |
| 108 | + array $params, |
| 109 | + string $expectedException, |
| 110 | + string $expectedExceptionMessage |
| 111 | + ): void { |
| 112 | + $this->expectException($expectedException); |
| 113 | + $this->expectExceptionMessage($expectedExceptionMessage); |
| 114 | + |
| 115 | + $this->auth->logout($params); |
| 116 | + } |
| 117 | + |
| 118 | + public static function provideTestLogoutFail(): Generator |
| 119 | + { |
| 120 | + yield 'Empty params' => [ |
| 121 | + '$params' => [], |
| 122 | + '$expectedException' => RequestValidationException::class, |
| 123 | + '$expectedExceptionMessage' => '"bearerToken" is required!', |
| 124 | + ]; |
| 125 | + } |
| 126 | + |
| 127 | + public function testLogout(): void |
| 128 | + { |
| 129 | + $params = [ |
| 130 | + 'bearerToken' => 'testBearerToken', |
| 131 | + ]; |
| 132 | + |
| 133 | + $expectedResult = [ |
| 134 | + 'result' => [ |
| 135 | + 'foo' => 'bar' |
| 136 | + ], |
| 137 | + ]; |
| 138 | + |
| 139 | + $this->client |
| 140 | + ->expects($this->once()) |
| 141 | + ->method('request') |
| 142 | + ->with( |
| 143 | + 'POST', |
| 144 | + 'auth/logout', |
| 145 | + [ |
| 146 | + 'headers' => [ |
| 147 | + 'Accept' => 'application/json', |
| 148 | + 'Content-Type' => 'application/json', |
| 149 | + 'Authorization' => 'Bearer testBearerToken', |
| 150 | + ], |
| 151 | + ] |
| 152 | + )->willReturn($expectedResult); |
| 153 | + |
| 154 | + $actualResult = $this->auth->logout($params); |
| 155 | + |
| 156 | + $this->assertSame($expectedResult, $actualResult); |
| 157 | + } |
| 158 | +} |
0 commit comments