From e530d87b8d73641f7c3d14077df3de801d18ace9 Mon Sep 17 00:00:00 2001 From: Bailey Herbert Date: Fri, 15 Mar 2024 10:27:14 -0700 Subject: [PATCH] Use mock responses for unit tests --- tests/EndpointTest.php | 39 ++++++++++++++++++++++++++++++++++++--- tests/OAuthTest.php | 19 ++++++++++++++++--- tests/bootstrap.php | 24 ++++++++++-------------- 3 files changed, 62 insertions(+), 20 deletions(-) diff --git a/tests/EndpointTest.php b/tests/EndpointTest.php index 92bb158..c51fb83 100644 --- a/tests/EndpointTest.php +++ b/tests/EndpointTest.php @@ -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; @@ -25,7 +28,15 @@ 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 @@ -33,7 +44,15 @@ public function testPerformEndpoint() { } 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' ]); @@ -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(); diff --git a/tests/OAuthTest.php b/tests/OAuthTest.php index 08aa227..55b595e 100644 --- a/tests/OAuthTest.php +++ b/tests/OAuthTest.php @@ -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 @@ -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, @@ -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; @@ -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.'); } diff --git a/tests/bootstrap.php b/tests/bootstrap.php index 3e84a8f..3273d3c 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -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);