Skip to content

Commit

Permalink
reajuste estrutura de mocks + suporte ao end-point de club
Browse files Browse the repository at this point in the history
  • Loading branch information
joao-pedro-alves committed Dec 19, 2023
1 parent c9dafb8 commit 188595a
Show file tree
Hide file tree
Showing 32 changed files with 514 additions and 1 deletion.
15 changes: 15 additions & 0 deletions src/Http/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Hotmart\Http\Endpoints\Authentication;
use Hotmart\Http\Endpoints\Subscriptions;
use Hotmart\Http\Endpoints\Transactions;
use Hotmart\Http\Endpoints\Club;

class Client
{
Expand Down Expand Up @@ -43,6 +44,11 @@ class Client
*/
private $transactions;

/**
* @var \Hotmart\Http\Endpoints\Club
*/
private $club;

/**
* @param string $clientId
* @param string $clientSecret
Expand All @@ -64,6 +70,7 @@ public function __construct($clientId, $clientSecret, $clientBasic, $extras = []
$this->authentication = new Authentication($this, $this->http);
$this->subscriptions = new Subscriptions($this, $this->http);
$this->transactions = new Transactions($this, $this->http);
$this->club = new Club($this, $this->http);
}

/**
Expand All @@ -82,6 +89,14 @@ public function transactions()
return $this->transactions;
}

/**
* @return \Hotmart\Http\Endpoints\Club
*/
public function club()
{
return $this->club;
}

/**
* @return \Hotmart\Http\Endpoints\Authentication
*/
Expand Down
81 changes: 81 additions & 0 deletions src/Http/Endpoints/Club.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?php

namespace Hotmart\Http\Endpoints;

use Hotmart\Http\Routes;

class Club extends Endpoint
{
/**
* @param string $subdomain
* @param array $params
* @return array
*/
public function modules(string $subdomain, array $params = [])
{
$params = array_merge(['subdomain' => $subdomain], $params);

return $this->http->request(
self::GET,
Routes::club()->modules(),
$this->bindQueryParams($params, $this->bindToken())
);
}

/**
* @param string $subdomain
* @param string $moduleId
* @param array $params
* @return array
*/
public function modulePages(string $subdomain, string $moduleId)
{
$params = [
'subdomain' => $subdomain,
'module_id' => $moduleId
];

return $this->http->request(
self::GET,
Routes::club()->modulePages($moduleId),
$this->bindQueryParams($params, $this->bindToken())
);
}

/**
* @param string $subdomain
* @param array $params
* @return array
*/
public function users(string $subdomain)
{
$params = [
'subdomain' => $subdomain,
];

return $this->http->request(
self::GET,
Routes::club()->users(),
$this->bindQueryParams($params, $this->bindToken())
);
}

/**
* @param string $subdomain
* @param array $params
* @param string $userId
* @return array
*/
public function userLessons(string $subdomain, string $userId)
{
$params = [
'subdomain' => $subdomain,
];

return $this->http->request(
self::GET,
Routes::club()->userLessons($userId),
$this->bindQueryParams($params, $this->bindToken())
);
}
}
2 changes: 1 addition & 1 deletion tests/unit/BaseTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ abstract class BaseTestCase extends TestCase
*/
protected static function jsonMock($mockName)
{
return file_get_contents(__DIR__ . "/Http/Endpoints/Mocks/Endpoints/$mockName.json");
return file_get_contents(__DIR__ . "/Mocks/Endpoints/$mockName.json");
}

/**
Expand Down
7 changes: 7 additions & 0 deletions tests/unit/Http/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Hotmart\Http\Endpoints\Transactions;
use Hotmart\Http\Endpoints\Subscriptions;
use Hotmart\Http\Endpoints\Authentication;
use Hotmart\Http\Endpoints\Club;

class ClientTest extends BaseTestCase
{
Expand Down Expand Up @@ -40,6 +41,12 @@ public function test_get_authentication_instance()
$this->assertInstanceOf(Authentication::class, $this->client->authentication());
}

public function test_get_Club_instance()
{
$this->assertTrue(method_exists($this->client, 'club'));
$this->assertInstanceOf(Club::class, $this->client->club());
}

public function test_get_credentials()
{
$this->assertTrue(method_exists($this->client, 'clientId'));
Expand Down
152 changes: 152 additions & 0 deletions tests/unit/Http/Endpoints/ClubTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
<?php

namespace Hotmart\Test\Http\Endpoints;

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

class ClubTest extends BaseTestCase
{
public function mockProvider()
{
return [[[
'modules' => new MockHandler([
new Response(200, [], self::jsonMock('Authentication')),
new Response(200, [], self::jsonMock('ClubModules')),
]),
'modulePages' => new MockHandler([
new Response(200, [], self::jsonMock('Authentication')),
new Response(200, [], self::jsonMock('ClubModulePages')),
]),
'users' => new MockHandler([
new Response(200, [], self::jsonMock('Authentication')),
new Response(200, [], self::jsonMock('ClubUsers')),
]),
'userLessons' => new MockHandler([
new Response(200, [], self::jsonMock('Authentication')),
new Response(200, [], self::jsonMock('ClubUserLessons')),
]),
]]];
}

/**
* @dataProvider mockProvider
*/
public function test_get_modules($mock)
{
$container = [];

$client = self::buildClient($container, $mock['modules']);
$response = $client->club()->modules('SUBDOMAIN', ['is_extra' => true]);

$this->assertEquals(
Club::GET,
self::getRequestMethod($container[1])
);

$this->assertEquals(
Routes::club()->modules(),
self::getRequestUri($container[1])
);

$this->assertEquals(
json_decode(self::jsonMock('ClubModules'), true),
$response
);

$query = self::getQueryString($container[1]);
$this->assertContains('subdomain=SUBDOMAIN', $query);
$this->assertContains('is_extra=1', $query);
}

/**
* @dataProvider mockProvider
*/
public function test_get_module_pages($mock)
{
$container = [];

$client = self::buildClient($container, $mock['modulePages']);
$response = $client->club()->modulePages('SUBDOMAIN', 'MODULE_ID');

$this->assertEquals(
Club::GET,
self::getRequestMethod($container[1])
);

$this->assertEquals(
Routes::club()->modulePages('MODULE_ID'),
self::getRequestUri($container[1])
);

$this->assertEquals(
json_decode(self::jsonMock('ClubModulePages'), true),
$response
);

$query = self::getQueryString($container[1]);
$this->assertContains('subdomain=SUBDOMAIN', $query);
$this->assertContains('module_id=MODULE_ID', $query);
}

/**
* @dataProvider mockProvider
*/
public function test_get_users($mock)
{
$container = [];

$client = self::buildClient($container, $mock['users']);
$response = $client->club()->users('SUBDOMAIN');

$this->assertEquals(
Club::GET,
self::getRequestMethod($container[1])
);

$this->assertEquals(
Routes::club()->users(),
self::getRequestUri($container[1])
);

$this->assertEquals(
json_decode(self::jsonMock('ClubUsers'), true),
$response
);

$query = self::getQueryString($container[1]);
$this->assertContains('subdomain=SUBDOMAIN', $query);
}

/**
* @dataProvider mockProvider
*/
public function test_get_user_lessons($mock)
{
$container = [];

$client = self::buildClient($container, $mock['userLessons']);
$response = $client->club()->userLessons('SUBDOMAIN', 'USER_ID');

$this->assertEquals(
Club::GET,
self::getRequestMethod($container[1])
);

$this->assertEquals(
Routes::club()->userLessons('USER_ID'),
self::getRequestUri($container[1])
);

$this->assertEquals(
json_decode(self::jsonMock('ClubUserLessons'), true),
$response
);

$query = self::getQueryString($container[1]);
$this->assertContains('subdomain=SUBDOMAIN', $query);
}
}
41 changes: 41 additions & 0 deletions tests/unit/Mocks/Endpoints/ClubModulePages.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
[
{
"liberation_days": 100,
"name": "Dripping 100 days",
"total_comments": 0,
"liberation_type": "BY_DAYS",
"page_order": 1,
"days_of_duration": 120,
"has_media": false,
"type": "CONTENT",
"has_duration": true
},
{
"name": "Dripping BY_DATE",
"liberation_date": 1585278000000,
"liberation_type": "BY_DATE",
"page_order": 2,
"type": "CONTENT",
"total_comments": 5,
"has_media": false,
"has_duration": false,
"rates": [
{
"rate": 3,
"total": 1
},
{
"rate": 5,
"total": 1
}
]
},
{
"page_order": 3,
"type": "ADVERTISEMENT",
"has_media": false,
"name": "Offer product",
"has_duration": false,
"total_comments": 0
}
]
Loading

0 comments on commit 188595a

Please sign in to comment.