Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 24 additions & 8 deletions src/Endpoints/Schools.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,16 @@

class Schools extends BootstrapEndpoint
{
private const ENDPOINT_URL = 'schools/';
/**
* @var string
*/
public $uri = 'schools/';
public $uri = self::ENDPOINT_URL;

/**
* @var string
*/
public $id = null;

/**
* @var Achievements
Expand Down Expand Up @@ -169,7 +175,8 @@ public function __construct($token, $id = false, $logPath = '')
$this->logPath = $logPath;

if ($id) {
$this->uri = $this->uri . $id . '/';
$this->id = $id;
$this->uri = $this->constructUri();
}

$this->achievements = new Achievements($token, $this->uri, $this->logPath);
Expand Down Expand Up @@ -204,6 +211,15 @@ public function __construct($token, $id = false, $logPath = '')
$this->subjects = new Subjects($token, $this->uri, $this->logPath);
}

public function constructUri($endpoint = '')
{
if (isset($this->id)) {
return self::ENDPOINT_URL . $this->id . '/' . $endpoint;
}

return self::ENDPOINT_URL . $endpoint;
}

public function updateDomain($domain)
{
$this->achievements->domain = $domain;
Expand Down Expand Up @@ -249,7 +265,7 @@ public function updateDomain($domain)
*/
public function pending($includes = [], $parameters = [])
{
$this->uri = $this->uri . 'pending/';
$this->uri = $this->constructUri('pending/');
return $this->all($includes, $parameters);
}

Expand All @@ -262,7 +278,7 @@ public function pending($includes = [], $parameters = [])
*/
public function audited($includes = [], $parameters = [])
{
$this->uri = $this->uri . 'audited/';
$this->uri = $this->constructUri('audited/');
return $this->all($includes, $parameters);
}

Expand All @@ -275,7 +291,7 @@ public function audited($includes = [], $parameters = [])
*/
public function declined($includes = [], $parameters = [])
{
$this->uri = $this->uri . 'declined/';
$this->uri = $this->constructUri('declined/');
return $this->all($includes, $parameters);
}

Expand All @@ -288,7 +304,7 @@ public function declined($includes = [], $parameters = [])
*/
public function revoked($includes = [], $parameters = [])
{
$this->uri = $this->uri . 'revoked/';
$this->uri = $this->constructUri('revoked/');
return $this->all($includes, $parameters);
}

Expand All @@ -301,7 +317,7 @@ public function revoked($includes = [], $parameters = [])
*/
public function search($includes = [], $parameters = [])
{
$this->uri = $this->uri . 'all/';
$this->uri = $this->constructUri('all/');
return $this->all($includes, $parameters);
}

Expand All @@ -315,7 +331,7 @@ public function search($includes = [], $parameters = [])
*/
public function get($id, $includes = [], $parameters = [])
{
$this->uri = 'schools/';
$this->uri = self::ENDPOINT_URL;
return parent::get($id, $includes, $parameters);
}

Expand Down
120 changes: 120 additions & 0 deletions tests/SchoolsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
<?php

use GuzzleHttp\Exception\ClientException;
use PHPUnit\Framework\TestCase;
use \Wonde\Client;

class SchoolsTest extends TestCase
{
private const BASE_URL = 'schools/';
private $client;
private $schoolsApi;
protected function setUp(): void
{
$this->token = file_get_contents(__DIR__ . '/../.token');
$this->client = new Client($this->token);
$this->schoolsApi = $this->client->schools;
}

public function testSchoolsPendingEndpointIsCorrectAfterMultipleCalls()
{
$this->schoolsApi->pending();
$this->assertEquals('schools/pending/', $this->schoolsApi->uri);

$this->schoolsApi->pending();
// Before fix issue
$this->assertNotEquals('schools/pending/pending', $this->schoolsApi->uri);

$this->assertEquals('schools/pending/', $this->schoolsApi->uri);
}

public function testSchoolAuditedEndpointIsCorrectAfterMultipleCalls()
{
$this->schoolsApi->audited();
$this->assertEquals('schools/audited/', $this->schoolsApi->uri);

$this->schoolsApi->audited();
// Before fix issue
$this->assertNotEquals('schools/audited/audited', $this->schoolsApi->uri);

$this->assertEquals('schools/audited/', $this->schoolsApi->uri);
}

public function testSchoolDeclinedEndpointIsCorrectAfterMultipleCalls()
{
$this->schoolsApi->declined();
$this->assertEquals('schools/declined/', $this->schoolsApi->uri);

$this->schoolsApi->declined();
// Before fix issue
$this->assertNotEquals('schools/declined/declined', $this->schoolsApi->uri);

$this->assertEquals('schools/declined/', $this->schoolsApi->uri);
}

public function testSchoolRevokedEndpointIsCorrectAfterMultipleCalls()
{
$this->schoolsApi->revoked();
$this->assertEquals('schools/revoked/', $this->schoolsApi->uri);

$this->schoolsApi->revoked();
// Before fix issue
$this->assertNotEquals('schools/revoked/revoked/', $this->schoolsApi->uri);

$this->assertEquals('schools/revoked/', $this->schoolsApi->uri);
}

public function testSearchSchoolEndpointIsCorrectAfterMultipleCalls()
{
$this->schoolsApi->search([], ['postcode' => 'SW1A']);
$this->assertEquals('schools/all/', $this->schoolsApi->uri);

$this->schoolsApi->search([], ['postcode' => 'SW1A']);
// Before fix issue
$this->assertNotEquals('schools/all/all/', $this->schoolsApi->uri);

$this->assertEquals('schools/all/', $this->schoolsApi->uri);

$this->schoolsApi->search([], ['postcode' => 'SW1A']);
// Before fix issue
$this->assertNotEquals('schools/all/all/all/', $this->schoolsApi->uri);

$this->assertEquals('schools/all/', $this->schoolsApi->uri);
}

public static function allSchoolsUriParams()
{
return [
['all/', self::BASE_URL . 'all/'],
['all/', self::BASE_URL . 'all/'],
['', self::BASE_URL],
[null, self::BASE_URL],
];
}

public static function specificSchoolUriParams()
{
return [
[1, 'all/', self::BASE_URL . '1/all/'],
[null, 'all/', self::BASE_URL . 'all/'],
[null, '', self::BASE_URL],
[null, null, self::BASE_URL],
];
}

/**
*@dataProvider allSchoolsUriParams
*/
public function testConstructUriMethod($endpoint, $expected)
{
$this->assertSame($expected, $this->schoolsApi->constructUri($endpoint));
}

/**
*@dataProvider specificSchoolUriParams
*/
public function testConstructUriMethodForSpecificSchool($id, $endpoint, $expected)
{
$this->assertSame($expected, $this->client->school($id)->constructUri($endpoint));
}
}