From ed88014aa1a132e0a8dd176444942cdad525374a Mon Sep 17 00:00:00 2001 From: Steve Boyd Date: Thu, 21 Oct 2021 16:47:01 +1300 Subject: [PATCH] API phpunit 9 support --- composer.json | 4 +- phpunit.xml.dist | 8 ++-- tests/unit/JSONDataFormatterTest.php | 28 ++++++++------ tests/unit/RestfulServerTest.php | 58 ++++++++++++++++------------ 4 files changed, 56 insertions(+), 42 deletions(-) diff --git a/composer.json b/composer.json index f3223f8..60d0da8 100644 --- a/composer.json +++ b/composer.json @@ -19,10 +19,10 @@ } ], "require": { - "silverstripe/framework": "^4" + "silverstripe/framework": "^4.10" }, "require-dev": { - "sminnee/phpunit": "^5.7", + "phpunit/phpunit": "^9", "squizlabs/php_codesniffer": "^3.0", "silverstripe/versioned": "^1" }, diff --git a/phpunit.xml.dist b/phpunit.xml.dist index e190cd3..8a5bffa 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -1,7 +1,9 @@ - - tests/ - + + + tests/ + + src/ diff --git a/tests/unit/JSONDataFormatterTest.php b/tests/unit/JSONDataFormatterTest.php index fe280b5..86ed7c1 100644 --- a/tests/unit/JSONDataFormatterTest.php +++ b/tests/unit/JSONDataFormatterTest.php @@ -27,20 +27,24 @@ public function testJSONTypes() $formatter = new JSONDataFormatter(); $parent = $this->objFromFixture(JSONDataFormatterTypeTestObject::class, 'parent'); $json = $formatter->convertDataObject($parent); - $this->assertRegexp('/"ID":\d+/', $json, 'PK casted to integer'); - $this->assertRegexp('/"Created":"\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}"/', $json, 'Datetime casted to string'); - $this->assertContains('"Name":"Parent"', $json, 'String casted to string'); - $this->assertContains('"Active":true', $json, 'Boolean casted to boolean'); - $this->assertContains('"Sort":17', $json, 'Integer casted to integer'); - $this->assertContains('"Average":1.2345', $json, 'Float casted to float'); - $this->assertContains('"ParentID":0', $json, 'Empty FK is 0'); + $this->assertMatchesRegularExpression('/"ID":\d+/', $json, 'PK casted to integer'); + $this->assertMatchesRegularExpression( + '/"Created":"\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}"/', + $json, + 'Datetime casted to string' + ); + $this->assertStringContainsString('"Name":"Parent"', $json, 'String casted to string'); + $this->assertStringContainsString('"Active":true', $json, 'Boolean casted to boolean'); + $this->assertStringContainsString('"Sort":17', $json, 'Integer casted to integer'); + $this->assertStringContainsString('"Average":1.2345', $json, 'Float casted to float'); + $this->assertStringContainsString('"ParentID":0', $json, 'Empty FK is 0'); $child3 = $this->objFromFixture(JSONDataFormatterTypeTestObject::class, 'child3'); $json = $formatter->convertDataObject($child3); - $this->assertContains('"Name":null', $json, 'Empty string is null'); - $this->assertContains('"Active":false', $json, 'Empty boolean is false'); - $this->assertContains('"Sort":0', $json, 'Empty integer is 0'); - $this->assertContains('"Average":0', $json, 'Empty float is 0'); - $this->assertRegexp('/"ParentID":\d+/', $json, 'FK casted to integer'); + $this->assertStringContainsString('"Name":null', $json, 'Empty string is null'); + $this->assertStringContainsString('"Active":false', $json, 'Empty boolean is false'); + $this->assertStringContainsString('"Sort":0', $json, 'Empty integer is 0'); + $this->assertStringContainsString('"Average":0', $json, 'Empty float is 0'); + $this->assertMatchesRegularExpression('/"ParentID":\d+/', $json, 'FK casted to integer'); } } diff --git a/tests/unit/RestfulServerTest.php b/tests/unit/RestfulServerTest.php index fbd4428..004caa8 100644 --- a/tests/unit/RestfulServerTest.php +++ b/tests/unit/RestfulServerTest.php @@ -49,7 +49,7 @@ protected function urlSafeClassname($classname) return str_replace('\\', '-', $classname); } - protected function setUp() + protected function setUp(): void { parent::setUp(); Director::config()->set('alternate_base_url', $this->baseURI); @@ -88,11 +88,11 @@ public function testApiAccessBoolean() $urlSafeClassname = $this->urlSafeClassname(RestfulServerTestComment::class); $url = "{$this->baseURI}/api/v1/$urlSafeClassname/" . $comment1->ID; $response = Director::test($url, null, null, 'GET'); - $this->assertContains('', $response->getBody()); - $this->assertContains('', $response->getBody()); - $this->assertContains('', $response->getBody()); - $this->assertContains('getBody()); - $this->assertContains('getBody()); + $this->assertStringContainsString('', $response->getBody()); + $this->assertStringContainsString('', $response->getBody()); + $this->assertStringContainsString('', $response->getBody()); + $this->assertStringContainsString('getBody()); + $this->assertStringContainsString('getBody()); } public function testAuthenticatedGET() @@ -430,8 +430,8 @@ public function testXMLValueFormatting() $urlSafeClassname = $this->urlSafeClassname(RestfulServerTestAuthorRating::class); $url = "{$this->baseURI}/api/v1/$urlSafeClassname/" . $rating1->ID; $response = Director::test($url, null, null, 'GET'); - $this->assertContains('' . $rating1->ID . '', $response->getBody()); - $this->assertContains('' . $rating1->Rating . '', $response->getBody()); + $this->assertStringContainsString('' . $rating1->ID . '', $response->getBody()); + $this->assertStringContainsString('' . $rating1->Rating . '', $response->getBody()); } public function testXMLValueFormattingWithFieldAlias() @@ -442,7 +442,7 @@ public function testXMLValueFormattingWithFieldAlias() $urlSafeClassname = $this->urlSafeClassname(RestfulServerTestAuthorRating::class); $url = "{$this->baseURI}/api/v1/$urlSafeClassname/" . $rating1->ID; $response = Director::test($url, null, null, 'GET'); - $this->assertContains('' . $rating1->Rating . '', $response->getBody()); + $this->assertStringContainsString('' . $rating1->Rating . '', $response->getBody()); } public function testApiAccessFieldRestrictions() @@ -453,21 +453,21 @@ public function testApiAccessFieldRestrictions() $urlSafeClassname = $this->urlSafeClassname(RestfulServerTestAuthorRating::class); $url = "{$this->baseURI}/api/v1/$urlSafeClassname/" . $rating1->ID; $response = Director::test($url, null, null, 'GET'); - $this->assertContains('', $response->getBody()); - $this->assertContains('', $response->getBody()); - $this->assertContains('getBody()); - $this->assertNotContains('', $response->getBody()); - $this->assertNotContains('', $response->getBody()); + $this->assertStringContainsString('', $response->getBody()); + $this->assertStringContainsString('', $response->getBody()); + $this->assertStringContainsString('getBody()); + $this->assertStringNotContainsString('', $response->getBody()); + $this->assertStringNotContainsString('', $response->getBody()); $urlSafeClassname = $this->urlSafeClassname(RestfulServerTestAuthorRating::class); $url = "{$this->baseURI}/api/v1/$urlSafeClassname/" . $rating1->ID . '?add_fields=SecretField,SecretRelation'; $response = Director::test($url, null, null, 'GET'); - $this->assertNotContains( + $this->assertStringNotContainsString( '', $response->getBody(), '"add_fields" URL parameter filters out disallowed fields from $api_access' ); - $this->assertNotContains( + $this->assertStringNotContainsString( '', $response->getBody(), '"add_fields" URL parameter filters out disallowed relations from $api_access' @@ -476,12 +476,12 @@ public function testApiAccessFieldRestrictions() $urlSafeClassname = $this->urlSafeClassname(RestfulServerTestAuthorRating::class); $url = "{$this->baseURI}/api/v1/$urlSafeClassname/" . $rating1->ID . '?fields=SecretField,SecretRelation'; $response = Director::test($url, null, null, 'GET'); - $this->assertNotContains( + $this->assertStringNotContainsString( '', $response->getBody(), '"fields" URL parameter filters out disallowed fields from $api_access' ); - $this->assertNotContains( + $this->assertStringNotContainsString( '', $response->getBody(), '"fields" URL parameter filters out disallowed relations from $api_access' @@ -490,12 +490,12 @@ public function testApiAccessFieldRestrictions() $urlSafeClassname = $this->urlSafeClassname(RestfulServerTestAuthor::class); $url = "{$this->baseURI}/api/v1/$urlSafeClassname/" . $author1->ID . '/Ratings'; $response = Director::test($url, null, null, 'GET'); - $this->assertContains( + $this->assertStringContainsString( '', $response->getBody(), 'Relation viewer shows fields allowed through $api_access' ); - $this->assertNotContains( + $this->assertStringNotContainsString( '', $response->getBody(), 'Relation viewer on has-many filters out disallowed fields from $api_access' @@ -509,8 +509,16 @@ public function testApiAccessRelationRestrictionsInline() $urlSafeClassname = $this->urlSafeClassname(RestfulServerTestAuthor::class); $url = "{$this->baseURI}/api/v1/$urlSafeClassname/" . $author1->ID; $response = Director::test($url, null, null, 'GET'); - $this->assertNotContains('getBody(), 'Restricts many-many with api_access=false'); - $this->assertNotContains('getBody(), 'Restricts has-many with api_access=false'); + $this->assertStringNotContainsString( + 'getBody(), + 'Restricts many-many with api_access=false' + ); + $this->assertStringNotContainsString( + 'getBody(), + 'Restricts has-many with api_access=false' + ); } public function testApiAccessRelationRestrictionsOnEndpoint() @@ -695,13 +703,13 @@ public function testCanViewRespectedInList() $url = "{$this->baseURI}/api/v1/$urlSafeClassname/"; $response = Director::test($url, null, null, 'GET'); $this->assertEquals(200, $response->getStatusCode()); - $this->assertNotContains('Unspeakable', $response->getBody()); + $this->assertStringNotContainsString('Unspeakable', $response->getBody()); // JSON content type $url = "{$this->baseURI}/api/v1/$urlSafeClassname.json"; $response = Director::test($url, null, null, 'GET'); $this->assertEquals(200, $response->getStatusCode()); - $this->assertNotContains('Unspeakable', $response->getBody()); + $this->assertStringNotContainsString('Unspeakable', $response->getBody()); $responseArray = json_decode($response->getBody(), true); $this->assertSame(0, $responseArray['totalSize']); @@ -712,7 +720,7 @@ public function testCanViewRespectedInList() $url = "{$this->baseURI}/api/v1/$urlSafeClassname/"; $response = Director::test($url, null, null, 'GET'); $this->assertEquals(200, $response->getStatusCode()); - $this->assertContains('Unspeakable', $response->getBody()); + $this->assertStringContainsString('Unspeakable', $response->getBody()); // Assumption: default formatter is XML $responseArray = Convert::xml2array($response->getBody()); $this->assertEquals(1, $responseArray['@attributes']['totalSize']);