diff --git a/src/Endpoints/Schools.php b/src/Endpoints/Schools.php index 610ba3d..941fca3 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(); } $this->achievements = new Achievements($token, $this->uri, $this->logPath); @@ -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; @@ -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); } @@ -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); } @@ -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); } @@ -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); } @@ -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); } @@ -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); } diff --git a/tests/SchoolsTest.php b/tests/SchoolsTest.php new file mode 100644 index 0000000..5e09fb5 --- /dev/null +++ b/tests/SchoolsTest.php @@ -0,0 +1,120 @@ +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)); + } +} \ No newline at end of file