Skip to content

Commit

Permalink
Use mock responses for unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
baileyherbert committed Mar 15, 2024
1 parent 93a267b commit e530d87
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 20 deletions.
39 changes: 36 additions & 3 deletions tests/EndpointTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

namespace Herbert\Tests\Envato;

use GuzzleHttp\Handler\MockHandler;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Psr7\Response;
use Herbert\Envato\Auth\Token;
use Herbert\Envato\Endpoint;
use Herbert\Envato\ResultSet;
Expand All @@ -25,15 +28,31 @@ public function testRetrieveEndpoint() {
}

public function testPerformEndpoint() {
$client = new EnvatoClient(new Token(TEST_PERSONAL_TOKEN));
$body = json_encode(array('matches' => array()));
$response = new Response(200, [], $body);
$mock = new MockHandler([ $response ]);
$handler = HandlerStack::create($mock);

$client = new EnvatoClient(new Token(TEST_PERSONAL_TOKEN), [
'handler' => $handler
]);

$items = $client->market->items();

// Must be a ResultSet
$this->assertInstanceOf(ResultSet::class, $items, 'Endpoint performed but did not return a ResultSet.');
}

public function testPerformEndpointWithVariables() {
$client = new EnvatoClient(new Token(TEST_PERSONAL_TOKEN));
$body = json_encode(array('matches' => array()));
$response = new Response(200, [], $body);
$mock = new MockHandler([ $response ]);
$handler = HandlerStack::create($mock);

$client = new EnvatoClient(new Token(TEST_PERSONAL_TOKEN), [
'handler' => $handler
]);

$items = $client->market->site([
'site' => 'themeforest'
]);
Expand All @@ -43,7 +62,21 @@ public function testPerformEndpointWithVariables() {
}

public function testRetrieveIdentity() {
$client = new EnvatoClient(new Token(TEST_PERSONAL_TOKEN));
$body = json_encode(array(
'clientId' => null,
'userId' => TEST_USER_ID,
'scopes' => array(),
'ttl' => 315360000
));

$response = new Response(200, [], $body);
$mock = new MockHandler([ $response, $response ]);
$handler = HandlerStack::create($mock);

$client = new EnvatoClient(new Token(TEST_PERSONAL_TOKEN), [
'handler' => $handler
]);

$identity = $client->getIdentity();
$userId = $client->getUserId();

Expand Down
19 changes: 16 additions & 3 deletions tests/OAuthTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

namespace Herbert\Tests\Envato;

use GuzzleHttp\Handler\MockHandler;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Psr7\Response;
use Herbert\Envato\Auth\OAuth;
use Herbert\Envato\Auth\Token;
use PHPUnit\Framework\TestCase;

class OAuthTest extends TestCase
Expand Down Expand Up @@ -35,6 +37,17 @@ public function testOAuthTokenGeneration() {

// Run the OAuth request
try {
$body = json_encode(array(
'refresh_token' => 'PZzGKkWLo9w7QVBLwQbOpWLksVjzjyDY',
'token_type' => 'bearer',
'access_token' => 'kh2UUTkaxqbV4zS93xMT2knjGdBbtbVq',
'expires_in' => 3600
));

$response = new Response(200, [], $body);
$mock = new MockHandler([ $response ]);
$handler = HandlerStack::create($mock);

$oauth = new OAuth([
'redirect_uri' => TEST_OAUTH_REDIRECT,
'client_id' => TEST_OAUTH_CLIENT,
Expand All @@ -43,7 +56,7 @@ public function testOAuthTokenGeneration() {
$this->assertJson($session, 'The session object provided to the storage callback was not valid JSON.');
$this->assertNotEmpty($session, 'The session object provided to the storage callback was empty.');
}
]);
], null, ['handler' => $handler]);

$token = $oauth->token;
$auth = $oauth->auth;
Expand All @@ -54,7 +67,7 @@ public function testOAuthTokenGeneration() {
}

$this->assertNotNull($auth, 'Failed to generate a token with the current test code. Go here and get a new code: ' . $uri);
$this->assertInstanceOf(Token::class, $token, 'Did not return an authenticated Token object.');
$this->assertTrue(is_string($token), 'Did not return an authenticated Token object.');
$this->assertNotNull($auth->expires, 'Authenticated Token object does not have an expiration time as expected.');
$this->assertNotNull($auth->token, 'Authenticated Token object does not have a token as expected.');
}
Expand Down
24 changes: 10 additions & 14 deletions tests/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,16 @@

require dirname(__DIR__) . '/vendor/autoload.php';

if (!isset($_ENV['ENVATO_PERSONAL_TOKEN'])) {
echo 'ENVATO_PERSONAL_TOKEN environment variable required';
exit(1);
}
// Parameters for testing OAuth
define('TEST_OAUTH_SECRET', 'O3Ynzq75h61UpbXSJuOPIbK2MCFsQciA');
define('TEST_OAUTH_REDIRECT', 'http://localhost:8080/');
define('TEST_OAUTH_CLIENT', 'temp-3ano9xnt');

// Configure an app for testing
// Register one here: https://build.envato.com/my-apps/
define('TEST_OAUTH_SECRET', '');
define('TEST_OAUTH_REDIRECT', '');
define('TEST_OAUTH_CLIENT', '');
// Code for testing OAuth token resolution
define('TEST_OAUTH_CODE', 'f4be726f30ffa3daabe9a6b6e6d6c361fe2486e4f04901ecf3e19c9b3834bce1');

// Create a token with the OAuth app above and paste it here
define('TEST_OAUTH_CODE', '');
// Personal token for testing direct access
define('TEST_PERSONAL_TOKEN', 'cFAKETOKENabcREPLACEMExyzcdefghj');

// Configure a personal token for testing
// Register one here: https://build.envato.com/my-apps/
define('TEST_PERSONAL_TOKEN', $_ENV['ENVATO_PERSONAL_TOKEN']);
// User ID for testing
define('TEST_USER_ID', 1234567);

0 comments on commit e530d87

Please sign in to comment.