-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
273 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace DarthSoup\Tests\Whmcs\Auth; | ||
|
||
use DarthSoup\Whmcs\Auth\AuthFactory; | ||
use DarthSoup\Whmcs\Auth\Method\PasswordAuth; | ||
use DarthSoup\Whmcs\Auth\Method\TokenAuth; | ||
use InvalidArgumentException; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class AuthFactoryTest extends TestCase | ||
{ | ||
protected AuthFactory $factory; | ||
|
||
protected function setUp(): void | ||
{ | ||
parent::setUp(); | ||
|
||
$this->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'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace DarthSoup\Tests\Whmcs\Auth\Method; | ||
|
||
use DarthSoup\Whmcs\Auth\Method\PasswordAuth; | ||
use DarthSoup\WhmcsApi\Client; | ||
use InvalidArgumentException; | ||
use Mockery; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class PasswordAuthTest extends TestCase | ||
{ | ||
protected PasswordAuth $auth; | ||
|
||
protected function setUp(): void | ||
{ | ||
parent::setUp(); | ||
|
||
$this->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', | ||
]); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace DarthSoup\Tests\Whmcs\Auth\Method; | ||
|
||
use DarthSoup\Whmcs\Auth\Method\TokenAuth; | ||
use DarthSoup\WhmcsApi\Client; | ||
use InvalidArgumentException; | ||
use Mockery; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class TokenAuthTest extends TestCase | ||
{ | ||
protected TokenAuth $auth; | ||
|
||
protected function setUp(): void | ||
{ | ||
parent::setUp(); | ||
|
||
$this->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', | ||
]); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace DarthSoup\Tests\Whmcs; | ||
|
||
use DarthSoup\Whmcs\Auth\AuthFactory; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace DarthSoup\Tests\Whmcs; | ||
|
||
use DarthSoup\Whmcs\Auth\AuthFactory; | ||
use DarthSoup\Whmcs\WhmcsFactory; | ||
use DarthSoup\WhmcsApi\Client; | ||
use Http\Client\Common\HttpMethodsClientInterface; | ||
use InvalidArgumentException; | ||
|
||
class WhmcsFactoryTest extends AbstractTestCase | ||
{ | ||
protected WhmcsFactory $factory; | ||
|
||
protected function setUp(): void | ||
{ | ||
parent::setUp(); | ||
|
||
$this->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([]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace DarthSoup\Tests\Whmcs; | ||
|
||
use DarthSoup\Whmcs\WhmcsFactory; | ||
use DarthSoup\Whmcs\WhmcsManager; | ||
use DarthSoup\WhmcsApi\Client; | ||
use Illuminate\Contracts\Config\Repository; | ||
use Mockery; | ||
|
||
class WhmcsManagerTest extends AbstractTestCase | ||
{ | ||
public function testCreateConnection() | ||
{ | ||
$configRepository = Mockery::mock(Repository::class); | ||
$factory = Mockery::mock(WhmcsFactory::class); | ||
$manager = new WhmcsManager($configRepository, $factory); | ||
|
||
$config = ['token' => '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()); | ||
} | ||
} |