From b2e8d5992967359bf111503b88afc7f1b0f814b4 Mon Sep 17 00:00:00 2001 From: ronaldoshukulli Date: Wed, 24 Sep 2025 14:01:21 +0100 Subject: [PATCH 1/2] Fix the way urls are constructed --- src/Endpoints/BootstrapEndpoint.php | 9 ++ src/Endpoints/Schools.php | 28 ++++-- tests/SchoolsTest.php | 139 ++++++++++++++++++++++++++++ 3 files changed, 168 insertions(+), 8 deletions(-) create mode 100644 tests/SchoolsTest.php diff --git a/src/Endpoints/BootstrapEndpoint.php b/src/Endpoints/BootstrapEndpoint.php index 0f741ea..cabf712 100644 --- a/src/Endpoints/BootstrapEndpoint.php +++ b/src/Endpoints/BootstrapEndpoint.php @@ -88,6 +88,15 @@ private function throwError(ClientException $exception) } } + protected function constructUri($baseUri, $id = null, $endpoint = '') + { + if (isset($id)) { + return $baseUri . $id . '/' . $endpoint; + } + + return $baseUri . $endpoint; + } + /** * Get all of resource * diff --git a/src/Endpoints/Schools.php b/src/Endpoints/Schools.php index 610ba3d..5e8a23d 100644 --- a/src/Endpoints/Schools.php +++ b/src/Endpoints/Schools.php @@ -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 @@ -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(self::ENDPOINT_URL, $id); } $this->achievements = new Achievements($token, $this->uri, $this->logPath); @@ -249,7 +256,8 @@ public function updateDomain($domain) */ public function pending($includes = [], $parameters = []) { - $this->uri = $this->uri . 'pending/'; + $this->uri = $this->constructUri(self::ENDPOINT_URL, $this->id, 'pending/'); +// $this->uri = $this->uri . 'pending/'; return $this->all($includes, $parameters); } @@ -262,7 +270,8 @@ public function pending($includes = [], $parameters = []) */ public function audited($includes = [], $parameters = []) { - $this->uri = $this->uri . 'audited/'; + $this->uri = $this->constructUri(self::ENDPOINT_URL, $this->id, 'audited/'); +// $this->uri = $this->uri . 'audited/'; return $this->all($includes, $parameters); } @@ -275,7 +284,8 @@ public function audited($includes = [], $parameters = []) */ public function declined($includes = [], $parameters = []) { - $this->uri = $this->uri . 'declined/'; + $this->uri = $this->constructUri(self::ENDPOINT_URL, $this->id, 'declined/'); +// $this->uri = $this->uri . 'declined/'; return $this->all($includes, $parameters); } @@ -288,7 +298,8 @@ public function declined($includes = [], $parameters = []) */ public function revoked($includes = [], $parameters = []) { - $this->uri = $this->uri . 'revoked/'; + $this->uri = $this->constructUri(self::ENDPOINT_URL, $this->id, 'revoked/'); +// $this->uri = $this->uri . 'revoked/'; return $this->all($includes, $parameters); } @@ -301,7 +312,8 @@ public function revoked($includes = [], $parameters = []) */ public function search($includes = [], $parameters = []) { - $this->uri = $this->uri . 'all/'; + $this->uri = $this->constructUri(self::ENDPOINT_URL, $this->id, 'all/'); +// $this->uri = $this->uri . 'all/'; return $this->all($includes, $parameters); } @@ -315,7 +327,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); } diff --git a/tests/SchoolsTest.php b/tests/SchoolsTest.php new file mode 100644 index 0000000..0a93fa9 --- /dev/null +++ b/tests/SchoolsTest.php @@ -0,0 +1,139 @@ +token = file_get_contents(__DIR__ . '/../.token'); + $client = new Client($this->token); + $this->schoolsApi = $client->schools; + } + +// public function testSearchSchoolEndpointIsNotCorrectAfterMultipleCalls() +// { +// $this->schoolsApi->search([], ['postcode' => 'EC3A']); +// $this->assertEquals('schools/all/', $this->schoolsApi->uri); +// +// $this->expectException(ClientException::class); +// $this->schoolsApi->search([], ['postcode' => 'EC3B']); +// $this->assertEquals('schools/all/all/', $this->schoolsApi->uri); +// +// $this->expectException(ClientException::class); +// $this->schoolsApi->search([], ['postcode' => 'EC3B']); +// $this->assertEquals('schools/all/all/all/', $this->schoolsApi->uri); +// } + +// public function testSchoolsPendingEndpointIsNotCorrectAfterMultipleCalls() +// { +// $this->schoolsApi->pending(); +// $this->assertEquals('schools/pending/', $this->schoolsApi->uri); +// +// $this->expectException(ClientException::class); +// $this->schoolsApi->pending(); +// $this->assertEquals('schools/pending/pending/', $this->schoolsApi->uri); +// } + +// public function testSchoolAuditedEndpointIsNotCorrectAfterMultipleCalls() +// { +// $this->schoolsApi->audited(); +// $this->assertEquals('schools/audited/', $this->schoolsApi->uri); +// +// $this->expectException(ClientException::class); +// $this->schoolsApi->audited(); +// $this->assertEquals('schools/audited/audited/', $this->schoolsApi->uri); +// } + +// public function testSchoolDeclinedEndpointIsNotCorrectAfterMultipleCalls() +// { +// $this->schoolsApi->declined(); +// $this->assertEquals('schools/declined/', $this->schoolsApi->uri); +// +// $this->expectException(ClientException::class); +// $this->schoolsApi->declined(); +// $this->assertEquals('schools/declined/declined/', $this->schoolsApi->uri); +// } + +// public function testSchoolRevokedEndpointIsNotCorrectAfterMultipleCalls() +// { +// $this->schoolsApi->revoked(); +// $this->assertEquals('schools/revoked/', $this->schoolsApi->uri); +// +// $this->expectException(ClientException::class); +// $this->schoolsApi->revoked(); +// $this->assertEquals('schools/revoked/revoked/', $this->schoolsApi->uri); +// } + + + public function testSchoolsPendingEndpointIsCorrectAfterMultipleCalls() + { + $this->schoolsApi->pending(); + $this->assertEquals('schools/pending/', $this->schoolsApi->uri); + + $this->schoolsApi->pending(); + $this->assertEquals('schools/pending/', $this->schoolsApi->uri); + } + + public function testSchoolAuditedEndpointIsCorrectAfterMultipleCalls() + { + $this->schoolsApi->audited(); + $this->assertEquals('schools/audited/', $this->schoolsApi->uri); + + $this->schoolsApi->audited(); + $this->assertEquals('schools/audited/', $this->schoolsApi->uri); + } + + public function testSchoolDeclinedEndpointIsCorrectAfterMultipleCalls() + { + $this->schoolsApi->declined(); + $this->assertEquals('schools/declined/', $this->schoolsApi->uri); + + $this->schoolsApi->declined(); + $this->assertEquals('schools/declined/', $this->schoolsApi->uri); + } + + public function testSchoolRevokedEndpointIsCorrectAfterMultipleCalls() + { + $this->schoolsApi->revoked(); + $this->assertEquals('schools/revoked/', $this->schoolsApi->uri); + + $this->schoolsApi->revoked(); + $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']); + $this->assertEquals('schools/all/', $this->schoolsApi->uri); + + $this->schoolsApi->search([], ['postcode' => 'SW1A']); + $this->assertEquals('schools/all/', $this->schoolsApi->uri); + } + +// public static function uriParams() +// { +// $fullUrl = 'https://wonde.com/' . self::BASE_URL; +// return [ +// [$fullUrl, 1, 'all/'], +// [$fullUrl, null, 'all/'], +// [$fullUrl, null, ''], +// [$fullUrl, null, null], +// ]; +// } +// +// /** +// *@dataProvider uriParams +// */ +// public function testConstructUriMethod($url, $id, $endpoint) +// { +// var_dump($this->schoolsApi->constructUri($url, $id, $endpoint)); +// } +} \ No newline at end of file From e311d2339922c83f8633a42277295f0905151573 Mon Sep 17 00:00:00 2001 From: ronaldoshukulli Date: Wed, 24 Sep 2025 15:20:18 +0100 Subject: [PATCH 2/2] Fix the way urls are constructed --- src/Endpoints/BootstrapEndpoint.php | 9 -- src/Endpoints/Schools.php | 26 +++--- tests/SchoolsTest.php | 131 ++++++++++++---------------- 3 files changed, 71 insertions(+), 95 deletions(-) diff --git a/src/Endpoints/BootstrapEndpoint.php b/src/Endpoints/BootstrapEndpoint.php index cabf712..0f741ea 100644 --- a/src/Endpoints/BootstrapEndpoint.php +++ b/src/Endpoints/BootstrapEndpoint.php @@ -88,15 +88,6 @@ private function throwError(ClientException $exception) } } - protected function constructUri($baseUri, $id = null, $endpoint = '') - { - if (isset($id)) { - return $baseUri . $id . '/' . $endpoint; - } - - return $baseUri . $endpoint; - } - /** * Get all of resource * diff --git a/src/Endpoints/Schools.php b/src/Endpoints/Schools.php index 5e8a23d..941fca3 100644 --- a/src/Endpoints/Schools.php +++ b/src/Endpoints/Schools.php @@ -176,7 +176,7 @@ public function __construct($token, $id = false, $logPath = '') if ($id) { $this->id = $id; - $this->uri = $this->constructUri(self::ENDPOINT_URL, $id); + $this->uri = $this->constructUri(); } $this->achievements = new Achievements($token, $this->uri, $this->logPath); @@ -211,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; @@ -256,8 +265,7 @@ public function updateDomain($domain) */ public function pending($includes = [], $parameters = []) { - $this->uri = $this->constructUri(self::ENDPOINT_URL, $this->id, 'pending/'); -// $this->uri = $this->uri . 'pending/'; + $this->uri = $this->constructUri('pending/'); return $this->all($includes, $parameters); } @@ -270,8 +278,7 @@ public function pending($includes = [], $parameters = []) */ public function audited($includes = [], $parameters = []) { - $this->uri = $this->constructUri(self::ENDPOINT_URL, $this->id, 'audited/'); -// $this->uri = $this->uri . 'audited/'; + $this->uri = $this->constructUri('audited/'); return $this->all($includes, $parameters); } @@ -284,8 +291,7 @@ public function audited($includes = [], $parameters = []) */ public function declined($includes = [], $parameters = []) { - $this->uri = $this->constructUri(self::ENDPOINT_URL, $this->id, 'declined/'); -// $this->uri = $this->uri . 'declined/'; + $this->uri = $this->constructUri('declined/'); return $this->all($includes, $parameters); } @@ -298,8 +304,7 @@ public function declined($includes = [], $parameters = []) */ public function revoked($includes = [], $parameters = []) { - $this->uri = $this->constructUri(self::ENDPOINT_URL, $this->id, 'revoked/'); -// $this->uri = $this->uri . 'revoked/'; + $this->uri = $this->constructUri('revoked/'); return $this->all($includes, $parameters); } @@ -312,8 +317,7 @@ public function revoked($includes = [], $parameters = []) */ public function search($includes = [], $parameters = []) { - $this->uri = $this->constructUri(self::ENDPOINT_URL, $this->id, 'all/'); -// $this->uri = $this->uri . 'all/'; + $this->uri = $this->constructUri('all/'); return $this->all($includes, $parameters); } diff --git a/tests/SchoolsTest.php b/tests/SchoolsTest.php index 0a93fa9..5e09fb5 100644 --- a/tests/SchoolsTest.php +++ b/tests/SchoolsTest.php @@ -7,75 +7,24 @@ class SchoolsTest extends TestCase { private const BASE_URL = 'schools/'; + private $client; private $schoolsApi; protected function setUp(): void { $this->token = file_get_contents(__DIR__ . '/../.token'); - $client = new Client($this->token); - $this->schoolsApi = $client->schools; + $this->client = new Client($this->token); + $this->schoolsApi = $this->client->schools; } -// public function testSearchSchoolEndpointIsNotCorrectAfterMultipleCalls() -// { -// $this->schoolsApi->search([], ['postcode' => 'EC3A']); -// $this->assertEquals('schools/all/', $this->schoolsApi->uri); -// -// $this->expectException(ClientException::class); -// $this->schoolsApi->search([], ['postcode' => 'EC3B']); -// $this->assertEquals('schools/all/all/', $this->schoolsApi->uri); -// -// $this->expectException(ClientException::class); -// $this->schoolsApi->search([], ['postcode' => 'EC3B']); -// $this->assertEquals('schools/all/all/all/', $this->schoolsApi->uri); -// } - -// public function testSchoolsPendingEndpointIsNotCorrectAfterMultipleCalls() -// { -// $this->schoolsApi->pending(); -// $this->assertEquals('schools/pending/', $this->schoolsApi->uri); -// -// $this->expectException(ClientException::class); -// $this->schoolsApi->pending(); -// $this->assertEquals('schools/pending/pending/', $this->schoolsApi->uri); -// } - -// public function testSchoolAuditedEndpointIsNotCorrectAfterMultipleCalls() -// { -// $this->schoolsApi->audited(); -// $this->assertEquals('schools/audited/', $this->schoolsApi->uri); -// -// $this->expectException(ClientException::class); -// $this->schoolsApi->audited(); -// $this->assertEquals('schools/audited/audited/', $this->schoolsApi->uri); -// } - -// public function testSchoolDeclinedEndpointIsNotCorrectAfterMultipleCalls() -// { -// $this->schoolsApi->declined(); -// $this->assertEquals('schools/declined/', $this->schoolsApi->uri); -// -// $this->expectException(ClientException::class); -// $this->schoolsApi->declined(); -// $this->assertEquals('schools/declined/declined/', $this->schoolsApi->uri); -// } - -// public function testSchoolRevokedEndpointIsNotCorrectAfterMultipleCalls() -// { -// $this->schoolsApi->revoked(); -// $this->assertEquals('schools/revoked/', $this->schoolsApi->uri); -// -// $this->expectException(ClientException::class); -// $this->schoolsApi->revoked(); -// $this->assertEquals('schools/revoked/revoked/', $this->schoolsApi->uri); -// } - - 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); } @@ -85,6 +34,9 @@ public function testSchoolAuditedEndpointIsCorrectAfterMultipleCalls() $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); } @@ -94,6 +46,9 @@ public function testSchoolDeclinedEndpointIsCorrectAfterMultipleCalls() $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); } @@ -103,6 +58,9 @@ public function testSchoolRevokedEndpointIsCorrectAfterMultipleCalls() $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); } @@ -112,28 +70,51 @@ public function testSearchSchoolEndpointIsCorrectAfterMultipleCalls() $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 uriParams() -// { -// $fullUrl = 'https://wonde.com/' . self::BASE_URL; -// return [ -// [$fullUrl, 1, 'all/'], -// [$fullUrl, null, 'all/'], -// [$fullUrl, null, ''], -// [$fullUrl, null, null], -// ]; -// } -// -// /** -// *@dataProvider uriParams -// */ -// public function testConstructUriMethod($url, $id, $endpoint) -// { -// var_dump($this->schoolsApi->constructUri($url, $id, $endpoint)); -// } + 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)); + } } \ No newline at end of file