From 7ffa3803b88f2c8593f2f5b6b6f0b61d05d83bd5 Mon Sep 17 00:00:00 2001 From: Kevin Krummnacker Date: Tue, 25 Jan 2022 01:18:19 +0100 Subject: [PATCH] feat: add unittests --- tests/AbstractTestCase.php | 4 +- tests/Auth/AuthFactoryTest.php | 45 ++++++++++++++++ tests/Auth/Method/PasswordAuthTest.php | 71 ++++++++++++++++++++++++++ tests/Auth/Method/TokenAuthTest.php | 71 ++++++++++++++++++++++++++ tests/ServiceProviderTest.php | 2 + tests/WhmcsFactoryTest.php | 44 ++++++++++++++++ tests/WhmcsManagerTest.php | 37 ++++++++++++++ 7 files changed, 273 insertions(+), 1 deletion(-) create mode 100644 tests/Auth/AuthFactoryTest.php create mode 100644 tests/Auth/Method/PasswordAuthTest.php create mode 100644 tests/Auth/Method/TokenAuthTest.php create mode 100644 tests/WhmcsFactoryTest.php create mode 100644 tests/WhmcsManagerTest.php diff --git a/tests/AbstractTestCase.php b/tests/AbstractTestCase.php index da068f7..8c2ddd3 100644 --- a/tests/AbstractTestCase.php +++ b/tests/AbstractTestCase.php @@ -1,5 +1,7 @@ factory = new AuthFactory(); + } + + public function testMakePasswordAuth() + { + $method = $this->factory->make('password'); + + $this->assertInstanceOf(PasswordAuth::class, $method); + } + + public function testMakeTokenAuth() + { + $method = $this->factory->make('token'); + + $this->assertInstanceOf(TokenAuth::class, $method); + } + + public function testMakeInvalidAuth() + { + $this->expectException(InvalidArgumentException::class); + $this->expectExceptionMessage('Unsupported authentication method [fail].'); + + $this->factory->make('fail'); + } +} diff --git a/tests/Auth/Method/PasswordAuthTest.php b/tests/Auth/Method/PasswordAuthTest.php new file mode 100644 index 0000000..f494804 --- /dev/null +++ b/tests/Auth/Method/PasswordAuthTest.php @@ -0,0 +1,71 @@ +auth = new PasswordAuth(); + } + + public function testMakeWithMethod() + { + $client = Mockery::mock(Client::class); + $client->shouldReceive('authenticate') + ->once()->with('identifier', 'secret', Client::AUTH_LOGIN_CREDENTIALS); + + $auth = $this->auth + ->with($client) + ->authenticate([ + 'username' => 'identifier', + 'password' => 'secret', + ]); + + $this->assertInstanceOf(Client::class, $auth); + } + + public function testWithoutUsername() + { + $client = Mockery::mock(Client::class); + + $this->expectException(InvalidArgumentException::class); + $this->expectExceptionMessage('The password authenticator requires a username.'); + + $this->auth->with($client)->authenticate([]); + } + + public function testWithoutPassword() + { + $client = Mockery::mock(Client::class); + + $this->expectException(InvalidArgumentException::class); + $this->expectExceptionMessage('The password authenticator requires a password.'); + + $this->auth->with($client)->authenticate(['username' => 'foo']); + } + + public function testMakeWithoutSettingClient() + { + $this->expectException(InvalidArgumentException::class); + $this->expectExceptionMessage('The client instance was not given to the auth process.'); + + $this->auth->authenticate([ + 'username' => 'foo', + 'password' => 'bar', + ]); + } + +} diff --git a/tests/Auth/Method/TokenAuthTest.php b/tests/Auth/Method/TokenAuthTest.php new file mode 100644 index 0000000..6dbe1f9 --- /dev/null +++ b/tests/Auth/Method/TokenAuthTest.php @@ -0,0 +1,71 @@ +auth = new TokenAuth(); + } + + public function testMakeWithMethod() + { + $client = Mockery::mock(Client::class); + $client->shouldReceive('authenticate') + ->once()->with('identifier', 'secret', Client::AUTH_API_CREDENTIALS); + + $auth = $this->auth + ->with($client) + ->authenticate([ + 'identifier' => 'identifier', + 'secret' => 'secret', + ]); + + $this->assertInstanceOf(Client::class, $auth); + } + + public function testWithoutUsername() + { + $client = Mockery::mock(Client::class); + + $this->expectException(InvalidArgumentException::class); + $this->expectExceptionMessage('The token authenticator requires a identifier.'); + + $this->auth->with($client)->authenticate([]); + } + + public function testWithoutPassword() + { + $client = Mockery::mock(Client::class); + + $this->expectException(InvalidArgumentException::class); + $this->expectExceptionMessage('The token authenticator requires a secret.'); + + $this->auth->with($client)->authenticate(['identifier' => 'foo']); + } + + public function testMakeWithoutSettingClient() + { + $this->expectException(InvalidArgumentException::class); + $this->expectExceptionMessage('The client instance was not given to the auth process.'); + + $this->auth->authenticate([ + 'identifier' => 'foo', + 'secret' => 'bar', + ]); + } + +} diff --git a/tests/ServiceProviderTest.php b/tests/ServiceProviderTest.php index 75cd472..9210fb6 100644 --- a/tests/ServiceProviderTest.php +++ b/tests/ServiceProviderTest.php @@ -1,5 +1,7 @@ factory = new WhmcsFactory(new AuthFactory()); + } + + public function testMakeConnectionWithMethod() + { + $client = $this->factory->make([ + 'method' => 'token', + 'identifier' => 'foo', + 'secret' => 'bar', + 'url' => 'https://whmcs.example.com/includes/api.php' + ]); + + $this->assertInstanceOf(Client::class, $client); + $this->assertInstanceOf(HttpMethodsClientInterface::class, $client->getHttpClient()); + } + + public function testWithoutMethod() + { + $this->expectException(InvalidArgumentException::class); + $this->expectExceptionMessage('The whmcs factory requires an auth method'); + + $this->factory->make([]); + } +} diff --git a/tests/WhmcsManagerTest.php b/tests/WhmcsManagerTest.php new file mode 100644 index 0000000..ea60b62 --- /dev/null +++ b/tests/WhmcsManagerTest.php @@ -0,0 +1,37 @@ + 'your-token']; + $config['name'] = 'primary'; + + $manager->getConfig()->shouldReceive('get')->once() + ->with('whmcs.connections')->andReturn(['primary' => $config]); + $manager->getFactory()->shouldReceive('make')->once() + ->with($config)->andReturn(Mockery::mock(Client::class)); + $manager->getConfig()->shouldReceive('get')->once() + ->with('whmcs.default')->andReturn('primary'); + + $this->assertSame([], $manager->getConnections()); + + $return = $manager->connection(); + $this->assertInstanceOf(Client::class, $return); + $this->assertArrayHasKey('primary', $manager->getConnections()); + } +}