diff options
author | Daniel Weipert <code@drogueronin.de> | 2022-01-02 17:34:34 +0100 |
---|---|---|
committer | Daniel Weipert <code@drogueronin.de> | 2022-01-10 11:29:13 +0100 |
commit | 39c0f9b8346447b27d65da1d62f8e03697fe8907 (patch) | |
tree | a423090519e2fe299f989d0d7bb98f2e718ea197 /tests/Test.php | |
parent | 976df6c8f88cb86ab713528b1c6f2ce3b91f6000 (diff) |
Add tests
Diffstat (limited to 'tests/Test.php')
-rw-r--r-- | tests/Test.php | 180 |
1 files changed, 180 insertions, 0 deletions
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 @@ +<?php + +use GuzzleHttp\Client; +use GuzzleHttp\Psr7\Response as GuzzleHttpResponse; +use PHPUnit\Framework\TestCase; +use Symfony\Component\HttpFoundation\Response; + +class Test extends TestCase +{ + protected $client; + + private $apiKey = '1234'; + + protected function setUp(): void + { + $this->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', <<<EOF + [app] + title = "Flat-File Forms" + + [email] + host = "localhost" + user = "root" + password = "123456" + + [api] + keys = ["1234"] + EOF); + + # formname config + file_put_contents($contentRoot . '/customername/formname/config/config.toml', <<<EOF + [api] + keys = ["asdfghjklö0987654321", "123"] + + [email] + host = "gmx" + EOF); + + # formname fields + file_put_contents($contentRoot . '/customername/formname/fields/_fields.toml', <<<EOF + [field.name] + file = "name.toml" + + [field.email] + file = "email.toml" + required = true + + [field.date] + required = true + EOF); + file_put_contents($contentRoot . '/customername/formname/fields/email.toml', <<<EOF + type = "email" + name = "email" + placeholder = "E-Mail" + + [attributes] + data-email = "test@example.org" + EOF); + file_put_contents($contentRoot . '/customername/formname/fields/name.toml', <<<EOF + type = "text" + name = "name" + + [attributes] + data-value = 123 + EOF); + + # pagedform fields + file_put_contents($contentRoot . '/customername/pagedform/fields/_fields.toml', <<<EOF + [page.one.field.name] + file = "name.toml" + + [page.one.field.email] + file = "email.toml" + required = true + + [page.second.field.date] + required = true + EOF); + file_put_contents($contentRoot . '/customername/pagedform/fields/email.toml', <<<EOF + type = "email" + name = "email" + placeholder = "E-Mail" + + [attributes] + data-email = "test@example.org" + EOF); + file_put_contents($contentRoot . '/customername/pagedform/fields/name.toml', <<<EOF + type = "text" + name = "name" + + [attributes] + data-value = 123 + EOF); + } + + public function request($method, $path): GuzzleHttpResponse + { + $response = null; + try { + $response = $this->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); + } +} + |