Skip to content

Commit

Permalink
reestruturacao dos diretorios de teste dos end-points + adicionado te…
Browse files Browse the repository at this point in the history
…ste da classe de client http
  • Loading branch information
joao-pedro-alves committed Dec 19, 2023
1 parent 34abb69 commit c9dafb8
Show file tree
Hide file tree
Showing 33 changed files with 180 additions and 91 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -221,4 +221,4 @@ $hotmart = new Hotmart\Client('CLIENT_ID', 'CLIENT_SECRET', 'CLIENT_BASIC');
$hotmart->transactions()->refund($transactionCode);
```

*Referência documentação:* https://developers.hotmart.com/docs/pt-BR/v1/sales/sales-refund/
*Referência documentação:* https://developers.hotmart.com/docs/pt-BR/v1/sales/sales-refund/
26 changes: 26 additions & 0 deletions src/Http/Routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,4 +95,30 @@ public static function transactions()

return $std;
}

/**
* @return \Hotmart\Anonymous
*/
public static function club()
{
$std = new Anonymous();

$std->modules = static function () {
return '/club/api/v1/modules';
};

$std->modulePages = static function ($moduleId) {
return '/club/api/v1/modules/'.$moduleId.'/pages';
};

$std->users = static function () {
return '/club/api/v1/users';
};

$std->userLessons = static function ($userId) {
return '/club/api/v1/users/' . $userId . '/lessons';
};

return $std;
}
}
72 changes: 68 additions & 4 deletions tests/unit/BaseTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,78 @@
namespace Hotmart\Test;

use PHPUnit\Framework\TestCase;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Middleware;
use Hotmart\Http\Client;
use Hotmart\Http\Http;

abstract class BaseTestCase extends TestCase
{
static protected function loadJsonMock(string $path)
/**
* @param string $mockName
*
* @return string
*/
protected static function jsonMock($mockName)
{
$rawJson = file_get_contents(__DIR__ . "/Mocks/$path.json");
return json_decode($rawJson, true);
return file_get_contents(__DIR__ . "/Http/Endpoints/Mocks/Endpoints/$mockName.json");
}

/**
* @param array $container
* @param \GuzzleHttp\Handler\MockHandler $mock
*/
protected static function buildClient(&$container, $mock)
{
$history = Middleware::history($container);

$handler = HandlerStack::create($mock);
$handler->push($history);

$http = new Http(['handler' => $handler]);
return new Client('', '', '', ['http' => $http]);
}

/**
* @param array $container
*
* @return string
*/
protected static function getRequestUri($container)
{
return $container['request']->getUri()->getPath();
}
}

/**
* @param array $container
*
* @return string
*/
protected static function getRequestMethod($container)
{
return $container['request']->getMethod();
}

/**
* @param array $container
*
* @return string
*/
protected static function getQueryString($container)
{
return $container['request']->getUri()->getQuery();
}

/**
* @param array $container
*
* @return string
*/
protected static function getBody($container)
{
$requestBody = $container['request']->getBody();
$bodySize = $requestBody->getSize();

return $requestBody->read($bodySize);
}
}
80 changes: 0 additions & 80 deletions tests/unit/Endpoints/BaseTestCase.php

This file was deleted.

53 changes: 53 additions & 0 deletions tests/unit/Http/ClientTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

namespace Hotmart\Test\Http;

use Hotmart\Http\Client;
use Hotmart\Test\BaseTestCase;
use Hotmart\Http\Endpoints\Transactions;
use Hotmart\Http\Endpoints\Subscriptions;
use Hotmart\Http\Endpoints\Authentication;

class ClientTest extends BaseTestCase
{
/**
* @var \Hotmart\Http\Client
*/
private $client;

public function setUp()
{
parent::__construct();

$this->client = new Client('CLIENT_ID', 'CLIENT_SECRET', 'CLIENT_BASIC');
}

public function test_get_subscriptions_instance()
{
$this->assertTrue(method_exists($this->client, 'subscriptions'));
$this->assertInstanceOf(Subscriptions::class, $this->client->subscriptions());
}

public function test_get_transactions_instance()
{
$this->assertTrue(method_exists($this->client, 'transactions'));
$this->assertInstanceOf(Transactions::class, $this->client->transactions());
}

public function test_get_authentication_instance()
{
$this->assertTrue(method_exists($this->client, 'authentication'));
$this->assertInstanceOf(Authentication::class, $this->client->authentication());
}

public function test_get_credentials()
{
$this->assertTrue(method_exists($this->client, 'clientId'));
$this->assertTrue(method_exists($this->client, 'clientSecret'));
$this->assertTrue(method_exists($this->client, 'clientBasic'));

$this->assertEquals('CLIENT_ID', $this->client->clientId());
$this->assertEquals('CLIENT_SECRET', $this->client->clientSecret());
$this->assertEquals('CLIENT_BASIC', $this->client->clientBasic());
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
<?php

namespace Hotmart\Test\Endpoints;
namespace Hotmart\Test\Http\Endpoints;

use GuzzleHttp\Psr7\Response;
use Hotmart\Test\BaseTestCase;
use GuzzleHttp\Handler\MockHandler;
use Hotmart\Http\Endpoints\Authentication;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
<?php
namespace Hotmart\Test\Endpoints;
namespace Hotmart\Test\Http\Endpoints;

use GuzzleHttp\Psr7\Request;
use GuzzleHttp\Psr7\Response;
use Hotmart\Http\ResponseHandler;
use Hotmart\Exceptions\HotmartException;
use GuzzleHttp\Exception\ClientException;
use Hotmart\Test\BaseTestCase;

class ResponseHandlerTest extends BaseTestCase
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
<?php

namespace Hotmart\Test\Endpoints;
namespace Hotmart\Test\Http\Endpoints;

use GuzzleHttp\Psr7\Response;
use GuzzleHttp\Handler\MockHandler;
use Hotmart\Exceptions\HotmartException;
use Hotmart\Http\Endpoints\Subscriptions;
use Hotmart\Test\BaseTestCase;

class SubscriptionsTest extends BaseTestCase
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
<?php

namespace Hotmart\Test\Endpoints;
namespace Hotmart\Test\Http\Endpoints;

use Hotmart\Http\Routes;
use GuzzleHttp\Psr7\Response;
use GuzzleHttp\Handler\MockHandler;
use Hotmart\Http\Endpoints\Transactions;
use Hotmart\Http\Routes;
use Hotmart\Test\BaseTestCase;

class TransactionsTest extends BaseTestCase
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
<?php
namespace Hotmart\Test\Endpoints;
namespace Hotmart\Test\Http;

use Hotmart\Http\Routes;
use Hotmart\Test\BaseTestCase;

class RoutesTest extends BaseTestCase
{
Expand Down Expand Up @@ -66,4 +67,25 @@ public function test_transaction_routes()
$this->assertIsCallable($routes->refund);
$this->assertEquals('/payments/api/v1/sales/abc/refund', $routes->refund('abc'));
}

public function test_club_routes()
{
$routes = Routes::club();

$this->assertObjectHasAttribute('modules', $routes);
$this->assertIsCallable($routes->modules);
$this->assertEquals('/club/api/v1/modules', $routes->modules());

$this->assertObjectHasAttribute('modulePages', $routes);
$this->assertIsCallable($routes->modulePages);
$this->assertEquals('/club/api/v1/modules/MODULE_ID/pages', $routes->modulePages('MODULE_ID'));

$this->assertObjectHasAttribute('users', $routes);
$this->assertIsCallable($routes->users);
$this->assertEquals('/club/api/v1/users', $routes->users());

$this->assertObjectHasAttribute('userLessons', $routes);
$this->assertIsCallable($routes->users);
$this->assertEquals('/club/api/v1/users/USER_ID/lessons', $routes->userLessons('USER_ID'));
}
}

0 comments on commit c9dafb8

Please sign in to comment.