From b2b002dfae824128c8442484cb8cfc7abcb211a2 Mon Sep 17 00:00:00 2001 From: Lonnie Ezell Date: Tue, 15 May 2018 22:30:09 -0500 Subject: [PATCH] Updating tests to work with new system. Partially complete. --- system/API/ResponseTrait.php | 13 +++--- system/Config/AutoloadConfig.php | 2 +- system/Config/Services.php | 3 +- system/HTTP/Response.php | 2 +- system/Test/FeatureResponse.php | 46 ++++++++++++++++++- tests/_support/HTTP/MockIncomingRequest.php | 4 +- tests/_support/HTTP/MockResponse.php | 42 +++++++++-------- tests/system/API/ResponseTraitTest.php | 17 +++---- tests/system/Database/Live/AliasTest.php | 4 +- .../system/Database/Live/CIDbTestCaseTest.php | 10 ++-- tests/system/Database/Live/CountTest.php | 6 ++- tests/system/Database/Live/DeleteTest.php | 3 +- tests/system/Database/Live/EmptyTest.php | 10 ++-- tests/system/Database/Live/ForgeTest.php | 4 +- tests/system/Database/Live/FromTest.php | 4 +- tests/system/Database/Live/GetTest.php | 6 ++- tests/system/Database/Live/GroupTest.php | 4 +- tests/system/Database/Live/IncrementTest.php | 6 ++- tests/system/Database/Live/InsertTest.php | 4 +- tests/system/Database/Live/JoinTest.php | 6 ++- tests/system/Database/Live/LikeTest.php | 4 +- tests/system/Database/Live/LimitTest.php | 4 +- tests/system/Database/Live/ModelTest.php | 3 +- tests/system/Database/Live/OrderTest.php | 4 +- .../Database/Live/PreparedQueryTest.php | 3 +- tests/system/Database/Live/PretendTest.php | 3 +- tests/system/Database/Live/SelectTest.php | 4 +- tests/system/Database/Live/UpdateTest.php | 3 +- tests/system/Database/Live/WhereTest.php | 4 +- tests/system/Filters/FiltersTest.php | 17 +------ tests/system/Helpers/NumberHelperTest.php | 10 ++-- tests/system/Pager/PagerTest.php | 10 +--- tests/system/Test/FeatureResponseTest.php | 42 +++++++++++++++++ 33 files changed, 207 insertions(+), 100 deletions(-) diff --git a/system/API/ResponseTrait.php b/system/API/ResponseTrait.php index 3d7d6477ce..426e41a76e 100644 --- a/system/API/ResponseTrait.php +++ b/system/API/ResponseTrait.php @@ -344,16 +344,15 @@ trait ResponseTrait return $data; } + // Determine correct response type through content negotiation + $config = new Format(); + $format = $this->request->negotiate('media', $config->supportedResponseFormats, true); + + $this->response->setContentType($format); + // if we don't have a formatter, make one if ( ! isset($this->formatter)) { - $config = new Format(); - - // Determine correct response type through content negotiation - $format = $this->request->negotiate('media', $config->supportedResponseFormats, true); - - $this->response->setContentType($format); - // if no formatter, use the default $this->formatter = $config->getFormatter($format); } diff --git a/system/Config/AutoloadConfig.php b/system/Config/AutoloadConfig.php index 60f92392f2..b75ac52424 100644 --- a/system/Config/AutoloadConfig.php +++ b/system/Config/AutoloadConfig.php @@ -91,7 +91,7 @@ class AutoloadConfig if (isset($_SERVER['CI_ENVIRONMENT']) && $_SERVER['CI_ENVIRONMENT'] === 'testing') { - $this->psr4['Tests\Support'] = BASEPATH . '../tests/_support/'; + $this->psr4['Tests\Support'] = BASEPATH . '../tests/_support'; } /** diff --git a/system/Config/Services.php b/system/Config/Services.php index 34cbe76489..26c52ccb68 100644 --- a/system/Config/Services.php +++ b/system/Config/Services.php @@ -39,6 +39,7 @@ use CodeIgniter\Database\ConnectionInterface; use CodeIgniter\Database\MigrationRunner; use CodeIgniter\HTTP\URI; use CodeIgniter\View\RendererInterface; +use Config\App; /** * Services Configuration file. @@ -547,7 +548,7 @@ class Services if ( ! is_object($config)) { - $config = new \Config\App(); + $config = new App(); } return new \CodeIgniter\HTTP\IncomingRequest( diff --git a/system/HTTP/Response.php b/system/HTTP/Response.php index 77843dd802..8abfda7a6d 100644 --- a/system/HTTP/Response.php +++ b/system/HTTP/Response.php @@ -444,7 +444,7 @@ class Response extends Message implements ResponseInterface $body = $formatter->format($body); } - return $body; + return $body ?: null; } //-------------------------------------------------------------------- diff --git a/system/Test/FeatureResponse.php b/system/Test/FeatureResponse.php index eadf2068e0..f05a5cdc70 100644 --- a/system/Test/FeatureResponse.php +++ b/system/Test/FeatureResponse.php @@ -302,9 +302,53 @@ class FeatureResponse extends TestCase */ public function getJSON() { - return $this->response->getJSON(); + $response = $this->response->getJSON(); + + if (is_null($response)) + { + $this->fail('The Response contained invalid JSON.'); + } + + return $response; } + /** + * + * + * @param array $fragment + * + * @throws \Exception + */ + public function assertJSONFragment(array $fragment) + { + $json = json_decode($this->getJSON(), true); + + $this->assertArraySubset($fragment, $json, false, "Response does not contain a matching JSON fragment."); + } + + /** + * Asserts that the JSON exactly matches the passed in data. + * If the value being passed in is a string, it must be a json_encoded string. + * + * @param string|array $test + * + * @throws \Exception + */ + public function assertJSONExact($test) + { + $json = $this->getJSON(); + + if (is_array($test)) + { + $config = new \Config\Format(); + $formatter = $config->getFormatter('application/json'); + $test = $formatter->format($test); + } + + $this->assertJsonStringEqualsJsonString($test, $json, "Response does not contain matching JSON."); + } + + //-------------------------------------------------------------------- // XML Methods //-------------------------------------------------------------------- diff --git a/tests/_support/HTTP/MockIncomingRequest.php b/tests/_support/HTTP/MockIncomingRequest.php index 7ee8e3ecfe..f6592e728e 100644 --- a/tests/_support/HTTP/MockIncomingRequest.php +++ b/tests/_support/HTTP/MockIncomingRequest.php @@ -1,4 +1,6 @@ -request = new MockIncomingRequest((object) $config, new URI($uri), null, new UserAgent()); - $this->response = new MockResponse((object) $config); + if (is_null($this->request)) + { + $this->request = new MockIncomingRequest((object)$config, new URI($uri), null, new UserAgent()); + $this->response = new MockResponse((object)$config); + } // Insert headers into request. $headers = [ diff --git a/tests/system/Database/Live/AliasTest.php b/tests/system/Database/Live/AliasTest.php index 9697868089..b5b933ca7b 100644 --- a/tests/system/Database/Live/AliasTest.php +++ b/tests/system/Database/Live/AliasTest.php @@ -1,6 +1,8 @@ seeInDatabase('user', ['name' => 'Ricky', 'email' => 'sofine@example.com', 'country' => 'US']); } - + //-------------------------------------------------------------------- public function testDontSeeInDatabase() @@ -43,5 +45,5 @@ class CIDbTestCaseTest extends \CIDatabaseTestCase //-------------------------------------------------------------------- - -} \ No newline at end of file + +} diff --git a/tests/system/Database/Live/CountTest.php b/tests/system/Database/Live/CountTest.php index 6813d36c2b..804e860879 100644 --- a/tests/system/Database/Live/CountTest.php +++ b/tests/system/Database/Live/CountTest.php @@ -1,9 +1,11 @@ db->table('misc')->emptyTable(); $this->assertEquals(0, $this->db->table('misc')->countAll()); } - + //-------------------------------------------------------------------- public function testTruncate() @@ -26,4 +28,4 @@ class EmptyTest extends \CIDatabaseTestCase } //-------------------------------------------------------------------- -} \ No newline at end of file +} diff --git a/tests/system/Database/Live/ForgeTest.php b/tests/system/Database/Live/ForgeTest.php index 35e8fef134..b570467250 100644 --- a/tests/system/Database/Live/ForgeTest.php +++ b/tests/system/Database/Live/ForgeTest.php @@ -1,9 +1,11 @@ request = Services::request(); $this->response = Services::response(); @@ -24,19 +24,6 @@ class FiltersTest extends \CIUnitTestCase //-------------------------------------------------------------------- - public function setUp() - { - - } - - //-------------------------------------------------------------------- - - public function tearDown() - { - - } - - //-------------------------------------------------------------------- public function testProcessMethodDetectsCLI() { diff --git a/tests/system/Helpers/NumberHelperTest.php b/tests/system/Helpers/NumberHelperTest.php index e161a4af5d..36a4b9da20 100755 --- a/tests/system/Helpers/NumberHelperTest.php +++ b/tests/system/Helpers/NumberHelperTest.php @@ -2,15 +2,13 @@ class numberHelperTest extends \CIUnitTestCase { - public function __construct(...$params) - { - parent::__construct(...$params); + public function setUp() + { + parent::setUp(); - helper('number'); + helper('number'); } - //-------------------------------------------------------------------- - public function test_roman_number() { $this->assertEquals('XCVI', number_to_roman(96)); diff --git a/tests/system/Pager/PagerTest.php b/tests/system/Pager/PagerTest.php index 558a4d97bb..8f47393575 100644 --- a/tests/system/Pager/PagerTest.php +++ b/tests/system/Pager/PagerTest.php @@ -15,16 +15,10 @@ class PagerTest extends \CIUnitTestCase protected $pager; protected $config; - public function __construct() - { - parent::__construct(); - helper('url'); - } - - //-------------------------------------------------------------------- - public function setUp() { + helper('url'); + $_SERVER['HTTP_HOST'] = 'example.com'; $_GET = []; $this->config = new Pager(); diff --git a/tests/system/Test/FeatureResponseTest.php b/tests/system/Test/FeatureResponseTest.php index 00e421198c..49ce991424 100644 --- a/tests/system/Test/FeatureResponseTest.php +++ b/tests/system/Test/FeatureResponseTest.php @@ -195,6 +195,48 @@ class FeatureResponseTest extends CIUnitTestCase $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']]); + } + + 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); + + $config = new \Config\Format(); + $formatter = $config->getFormatter('application/json'); + $expected = $formatter->format($data); + + $this->feature->assertJSONExact($expected); + } + + protected function getFeatureResponse($body=null, array $responseOptions = null, array $headers = null) { $this->response = new Response(new \Config\App());