Skip to content

Commit

Permalink
feat: add unittests
Browse files Browse the repository at this point in the history
  • Loading branch information
darthsoup committed Jan 25, 2022
1 parent 5f31ef2 commit 7ffa380
Show file tree
Hide file tree
Showing 7 changed files with 273 additions and 1 deletion.
4 changes: 3 additions & 1 deletion tests/AbstractTestCase.php
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\Facades\Whmcs;
Expand All @@ -21,7 +23,7 @@ protected function getPackageAliases($app)
/**
* Define environment setup.
*
* @param \Illuminate\Foundation\Application $app
* @param \Illuminate\Foundation\Application $app
* @return void
*/
protected function getEnvironmentSetUp($app)
Expand Down
45 changes: 45 additions & 0 deletions tests/Auth/AuthFactoryTest.php
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');
}
}
71 changes: 71 additions & 0 deletions tests/Auth/Method/PasswordAuthTest.php
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',
]);
}

}
71 changes: 71 additions & 0 deletions tests/Auth/Method/TokenAuthTest.php
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',
]);
}

}
2 changes: 2 additions & 0 deletions tests/ServiceProviderTest.php
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;
Expand Down
44 changes: 44 additions & 0 deletions tests/WhmcsFactoryTest.php
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([]);
}
}
37 changes: 37 additions & 0 deletions tests/WhmcsManagerTest.php
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());
}
}

0 comments on commit 7ffa380

Please sign in to comment.