From 39c0f9b8346447b27d65da1d62f8e03697fe8907 Mon Sep 17 00:00:00 2001 From: Daniel Weipert Date: Sun, 2 Jan 2022 17:34:34 +0100 Subject: Add tests --- tests/Test.php | 180 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 180 insertions(+) create mode 100644 tests/Test.php (limited to 'tests/Test.php') diff --git a/tests/Test.php b/tests/Test.php new file mode 100644 index 0000000..2c81d46 --- /dev/null +++ b/tests/Test.php @@ -0,0 +1,180 @@ +client = new Client([ + 'base_uri' => 'http://localhost:8080', + ]); + + // add content + $contentRoot = dirname(__DIR__) . '/content'; + @mkdir($contentRoot . '/config'); + @mkdir($contentRoot . '/customername/formname/config'); + @mkdir($contentRoot . '/customername/formname/fields'); + @mkdir($contentRoot . '/customername/pagedform/fields'); + + # root config + file_put_contents($contentRoot . '/config/config.toml', <<client->request($method, $path, ['http_errors' => false]); + } catch (\Exception $e) {} + + return $response; + } + + public function testApiKey() + { + $formnameApiKey = '123'; + $rootApiKey = '1234'; + + // formname + $formnameResponse = $this->request('GET', 'customername/formname/fields'); + $this->assertEquals(Response::HTTP_BAD_REQUEST, $formnameResponse->getStatusCode()); + + $formnameResponse = $this->request('GET', 'customername/formname/fields?key=321'); + $this->assertEquals(Response::HTTP_UNAUTHORIZED, $formnameResponse->getStatusCode()); + + $formnameResponse = $this->request('GET', 'customername/formname/fields?key=' . $formnameApiKey); + $this->assertEquals(Response::HTTP_OK, $formnameResponse->getStatusCode()); + + $formnameResponse = $this->request('GET', 'customername/formname/fields?key=' . $rootApiKey); + $this->assertEquals(Response::HTTP_OK, $formnameResponse->getStatusCode()); + + // pagedform + $pagedformResponse = $this->request('GET', 'customername/pagedform/fields?key=' . $formnameApiKey); + $this->assertEquals(Response::HTTP_UNAUTHORIZED, $pagedformResponse->getStatusCode()); + + $pagedformResponse = $this->request('GET', 'customername/pagedform/fields?key=' . $rootApiKey); + $this->assertEquals(Response::HTTP_OK, $pagedformResponse->getStatusCode()); + } + + public function testFields() { + $formnameResponse = $this->request('GET', 'customername/formname/fields?key=' . $this->apiKey); + $formnameBody = json_decode((string)$formnameResponse->getBody(), true)['data']; + $this->assertArrayHasKey('name', $formnameBody); + $this->assertArrayHasKey('email', $formnameBody); + $this->assertArrayHasKey('date', $formnameBody); + + $this->assertArrayHasKey('name', $formnameBody['name']); + $this->assertArrayHasKey('name', $formnameBody['date']); + $this->assertArrayHasKey('required', $formnameBody['email']); + } + + public function testFieldsPaged() { + // all pages + $pagedformResponse = $this->request('GET', 'customername/pagedform/fields?key=' . $this->apiKey); + $pagedformBody = json_decode((string)$pagedformResponse->getBody(), true)['data']; + $this->assertArrayHasKey('one', $pagedformBody); + $this->assertArrayHasKey('second', $pagedformBody); + + $this->assertArrayHasKey('name', $pagedformBody['one']); + $this->assertArrayHasKey('email', $pagedformBody['one']); + $this->assertArrayHasKey('date', $pagedformBody['second']); + + // page=one + $pagedformResponse = $this->request('GET', 'customername/pagedform/fields?page=one&key=' . $this->apiKey); + $pagedformBody = json_decode((string)$pagedformResponse->getBody(), true)['data']; + $this->assertArrayNotHasKey('one', $pagedformBody); + $this->assertArrayHasKey('name', $pagedformBody); + $this->assertArrayHasKey('email', $pagedformBody); + + // page=second + $pagedformResponse = $this->request('GET', 'customername/pagedform/fields?page=second&key=' . $this->apiKey); + $pagedformBody = json_decode((string)$pagedformResponse->getBody(), true)['data']; + $this->assertArrayNotHasKey('second', $pagedformBody); + $this->assertArrayHasKey('date', $pagedformBody); + } +} + -- cgit v1.2.3