');
- $this->response->setJSON('', true);
-
- // this should be "" - json_encode('');
- $this->assertEquals('""', $this->feature->getJSON());
- }
-
- public function testFalseJSON()
- {
- $this->getFeatureResponse('
Hello World
');
- $this->response->setJSON(false, true);
-
- // this should be FALSE - json_encode(false)
- $this->assertEquals('false', $this->feature->getJSON());
- }
-
- public function testTrueJSON()
- {
- $this->getFeatureResponse('
Hello World
');
- $this->response->setJSON(true, true);
-
- // this should be TRUE - json_encode(true)
- $this->assertEquals('true', $this->feature->getJSON());
- }
-
- public function testInvalidJSON()
- {
- $tmp = ' test " case ';
- $this->getFeatureResponse('
Hello World
');
- $this->response->setBody($tmp);
-
- // this should be FALSE - invalid JSON - will see if this is working that way ;-)
- $this->assertFalse($this->response->getBody() === $this->feature->getJSON());
- }
-
- public function testGetXML()
- {
- $this->getFeatureResponse(['foo' => 'bar']);
- $formatter = Services::format()->getFormatter('application/xml');
-
- $this->assertEquals($formatter->format(['foo' => 'bar']), $this->feature->getXML());
- }
-
- public function testJsonFragment()
- {
- $this->getFeatureResponse([
- 'config' => [
- 'key-a',
- 'key-b',
- ],
- ]);
-
- $this->feature->assertJSONFragment(['config' => ['key-a']]);
- $this->feature->assertJSONFragment(['config' => ['key-a']], true);
- }
-
- public function testAssertJSONFragmentFollowingAssertArraySubset()
- {
- $this->getFeatureResponse([
- 'config' => '124',
- ]);
-
- $this->feature->assertJSONFragment(['config' => 124]); // must fail on strict
- $this->feature->assertJSONFragment(['config' => '124'], true);
- }
-
- public function testJsonExact()
- {
- $data = [
- 'config' => [
- 'key-a',
- 'key-b',
- ],
- ];
-
- $this->getFeatureResponse($data);
-
- $this->feature->assertJSONExact($data);
- }
-
- public function testJsonExactString()
- {
- $data = [
- 'config' => [
- 'key-a',
- 'key-b',
- ],
- ];
-
- $this->getFeatureResponse($data);
- $formatter = Services::format()->getFormatter('application/json');
-
- $this->feature->assertJSONExact($formatter->format($data));
- }
-
- protected function getFeatureResponse($body = null, array $responseOptions = [], array $headers = [])
- {
- $this->response = new Response(new App());
- $this->response->setBody($body);
-
- foreach ($responseOptions as $key => $value)
- {
- $method = 'set' . ucfirst($key);
-
- if (method_exists($this->response, $method))
- {
- $this->response = $this->response->$method($value);
- }
- }
-
- foreach ($headers as $key => $value)
- {
- $this->response = $this->response->setHeader($key, $value);
- }
-
- $this->feature = new FeatureResponse($this->response);
- }
-}
diff --git a/tests/system/Test/TestResponseTest.php b/tests/system/Test/TestResponseTest.php
new file mode 100644
index 000000000000..24ef181ebd80
--- /dev/null
+++ b/tests/system/Test/TestResponseTest.php
@@ -0,0 +1,435 @@
+getTestResponse('Hello World');
+ $this->response->setStatusCode($code);
+
+ $this->assertEquals($isOk, $this->testResponse->isOK());
+ }
+
+ /**
+ * Provides status codes and their expected "OK"
+ */
+ public function statusCodeProvider(): array
+ {
+ return [
+ [
+ 100,
+ false,
+ ],
+ [
+ 200,
+ true,
+ ],
+ [
+ 201,
+ true,
+ ],
+ [
+ 300,
+ true,
+ ], // Redirects are acceptable if the body is empty
+ [
+ 301,
+ true,
+ ],
+ [
+ 400,
+ false,
+ ],
+ [
+ 401,
+ false,
+ ],
+ [
+ 599,
+ false,
+ ],
+ ];
+ }
+
+ public function testIsOKEmpty()
+ {
+ $this->getTestResponse('Hi there');
+ $this->response->setStatusCode(200);
+ $this->response->setBody('');
+
+ $this->assertFalse($this->testResponse->isOK());
+ }
+
+ public function testAssertSee()
+ {
+ $this->getTestResponse('
Hello World
');
+
+ $this->testResponse->assertSee('Hello');
+ $this->testResponse->assertSee('World', 'h1');
+ }
+
+ public function testAssertDontSee()
+ {
+ $this->getTestResponse('
Hello World
');
+
+ $this->testResponse->assertDontSee('Worlds');
+ $this->testResponse->assertDontSee('World', 'h2');
+ }
+
+ public function testAssertSeeElement()
+ {
+ $this->getTestResponse('
Hello World
');
+
+ $this->testResponse->assertSeeElement('h1');
+ $this->testResponse->assertSeeElement('span');
+ $this->testResponse->assertSeeElement('h1.header');
+ }
+
+ public function testAssertDontSeeElement()
+ {
+ $this->getTestResponse('
Hello World
');
+
+ $this->testResponse->assertDontSeeElement('h2');
+ $this->testResponse->assertDontSeeElement('.span');
+ $this->testResponse->assertDontSeeElement('h1.para');
+ }
+
+ public function testAssertSeeLink()
+ {
+ $this->getTestResponse('
');
+ $this->response->setJSON('', true);
+
+ // this should be "" - json_encode('');
+ $this->assertEquals('""', $this->testResponse->getJSON());
+ }
+
+ public function testFalseJSON()
+ {
+ $this->getTestResponse('
Hello World
');
+ $this->response->setJSON(false, true);
+
+ // this should be FALSE - json_encode(false)
+ $this->assertEquals('false', $this->testResponse->getJSON());
+ }
+
+ public function testTrueJSON()
+ {
+ $this->getTestResponse('
Hello World
');
+ $this->response->setJSON(true, true);
+
+ // this should be TRUE - json_encode(true)
+ $this->assertEquals('true', $this->testResponse->getJSON());
+ }
+
+ public function testInvalidJSON()
+ {
+ $tmp = ' test " case ';
+ $this->getTestResponse('
Hello World
');
+ $this->response->setBody($tmp);
+
+ // this should be FALSE - invalid JSON - will see if this is working that way ;-)
+ $this->assertFalse($this->response->getBody() === $this->testResponse->getJSON());
+ }
+
+ public function testGetXML()
+ {
+ $this->getTestResponse(['foo' => 'bar']);
+ $formatter = Services::format()->getFormatter('application/xml');
+
+ $this->assertEquals($formatter->format(['foo' => 'bar']), $this->testResponse->getXML());
+ }
+
+ public function testJsonFragment()
+ {
+ $this->getTestResponse([
+ 'config' => [
+ 'key-a',
+ 'key-b',
+ ],
+ ]);
+
+ $this->testResponse->assertJSONFragment(['config' => ['key-a']]);
+ $this->testResponse->assertJSONFragment(['config' => ['key-a']], true);
+ }
+
+ public function testAssertJSONFragmentFollowingAssertArraySubset()
+ {
+ $this->getTestResponse([
+ 'config' => '124',
+ ]);
+
+ $this->testResponse->assertJSONFragment(['config' => 124]); // must fail on strict
+ $this->testResponse->assertJSONFragment(['config' => '124'], true);
+ }
+
+ public function testAssertJSONFragmentFailsGracefullyWhenNotGivenJson()
+ {
+ $this->getTestResponse('