diff --git a/Exception/AbstractException.php b/Exception/AbstractException.php new file mode 100644 index 0000000..a2d93a3 --- /dev/null +++ b/Exception/AbstractException.php @@ -0,0 +1,25 @@ + * @copyright 2014 - 2020 RichCongress (https://www.richcongress.com) */ -final class CsrfTokenManagerMissingException extends \Exception +final class CsrfTokenManagerMissingException extends AbstractException { /** @var string */ protected static $error = 'The Security\'s CSRF Token Manager is missing from the container.'; - - /** @var string */ - protected static $documentation = 'https://github.com/richcongress/web-test-bundle/blob/master/Docs/Exceptions.md#CsrfTokenManagerMissingException'; - - /** - * CsrfTokenManagerMissingException constructor. - */ - public function __construct() - { - $message = self::$error; - $message .= "\nCheck the documentation: " . self::$documentation; - - parent::__construct($message); - } } diff --git a/Exception/EntityManagerNotFoundException.php b/Exception/EntityManagerNotFoundException.php index 15cb527..7bc9ad3 100644 --- a/Exception/EntityManagerNotFoundException.php +++ b/Exception/EntityManagerNotFoundException.php @@ -8,25 +8,9 @@ * @package RichCongress\WebTestBundle\Exception * @author Nicolas Guilloux * @copyright 2014 - 2020 RichCongress (https://www.richcongress.com) - * - * @covers \RichCongress\WebTestBundle\Exception\EntityManagerNotFoundException */ -final class EntityManagerNotFoundException extends \Exception +final class EntityManagerNotFoundException extends AbstractException { /** @var string */ protected static $error = 'The Entity manager cannot be found. Check your Doctrine documentation.'; - - /** @var string */ - protected static $documentation = 'https://github.com/richcongress/web-test-bundle/blob/master/Docs/Exceptions.md#EntityManagerNotFoundException'; - - /** - * EntityManagerNotFoundException constructor. - */ - public function __construct() - { - $message = self::$error; - $message .= "\nCheck the documentation: " . self::$documentation; - - parent::__construct($message); - } } diff --git a/Exception/JsonRequestWithContentException.php b/Exception/JsonRequestWithContentException.php new file mode 100644 index 0000000..19deebd --- /dev/null +++ b/Exception/JsonRequestWithContentException.php @@ -0,0 +1,9 @@ + * @copyright 2014 - 2020 RichCongress (https://www.richcongress.com) */ -final class KernelNotInitializedException extends \Exception +final class KernelNotInitializedException extends AbstractException { /** @var string */ protected static $error = 'The kernel was not initialized. Did you add the annotation `@TestConfig("kernel")` to your method or class?'; - - /** @var string */ - protected static $documentation = 'https://github.com/richcongress/web-test-bundle/blob/master/Docs/Exceptions.md#KernelNotInitializedException'; - - /** - * KernelNotInitializedException constructor. - */ - public function __construct() - { - $message = self::$error; - $message .= "\nCheck the documentation: " . self::$documentation; - - parent::__construct($message); - } } diff --git a/Tests/Exceptions/CsrfTokenManagerMissingExceptionTest.php b/Tests/Exceptions/CsrfTokenManagerMissingExceptionTest.php index 3343e00..f025a31 100644 --- a/Tests/Exceptions/CsrfTokenManagerMissingExceptionTest.php +++ b/Tests/Exceptions/CsrfTokenManagerMissingExceptionTest.php @@ -13,6 +13,7 @@ * @copyright 2014 - 2020 RichCongress (https://www.richcongress.com) * * @covers \RichCongress\WebTestBundle\Exception\CsrfTokenManagerMissingException + * @covers \RichCongress\WebTestBundle\Exception\AbstractException */ final class CsrfTokenManagerMissingExceptionTest extends TestCase { diff --git a/Tests/Exceptions/EntityManagerNotFoundExceptionTest.php b/Tests/Exceptions/EntityManagerNotFoundExceptionTest.php index 5478de1..4351fea 100644 --- a/Tests/Exceptions/EntityManagerNotFoundExceptionTest.php +++ b/Tests/Exceptions/EntityManagerNotFoundExceptionTest.php @@ -13,6 +13,7 @@ * @copyright 2014 - 2020 RichCongress (https://www.richcongress.com) * * @covers \RichCongress\WebTestBundle\Exception\EntityManagerNotFoundException + * @covers \RichCongress\WebTestBundle\Exception\AbstractException */ final class EntityManagerNotFoundExceptionTest extends TestCase { diff --git a/Tests/Exceptions/JsonRequestWithContentExceptionTest.php b/Tests/Exceptions/JsonRequestWithContentExceptionTest.php new file mode 100644 index 0000000..a386bcd --- /dev/null +++ b/Tests/Exceptions/JsonRequestWithContentExceptionTest.php @@ -0,0 +1,23 @@ +getMessage() + ); + } +} diff --git a/Tests/Exceptions/KernelNotInitializedExceptionTest.php b/Tests/Exceptions/KernelNotInitializedExceptionTest.php index 3048ace..51c511f 100644 --- a/Tests/Exceptions/KernelNotInitializedExceptionTest.php +++ b/Tests/Exceptions/KernelNotInitializedExceptionTest.php @@ -13,6 +13,7 @@ * @copyright 2014 - 2020 RichCongress (https://www.richcongress.com) * * @covers \RichCongress\WebTestBundle\Exception\KernelNotInitializedException + * @covers \RichCongress\WebTestBundle\Exception\AbstractException */ final class KernelNotInitializedExceptionTest extends TestCase { diff --git a/Tests/WebTest/ClientTest.php b/Tests/WebTest/ClientTest.php index 2e18b23..0f74dd0 100644 --- a/Tests/WebTest/ClientTest.php +++ b/Tests/WebTest/ClientTest.php @@ -2,6 +2,7 @@ namespace RichCongress\WebTestBundle\Tests\WebTest; +use RichCongress\WebTestBundle\Exception\JsonRequestWithContentException; use RichCongress\WebTestBundle\TestCase\ControllerTestCase; use RichCongress\WebTestBundle\WebTest\Client; use RichCongress\WebTestBundle\WebTest\Response; @@ -71,4 +72,20 @@ public function testExtractBrowserWithNoBrowser(): void Client::extractBrowser(null); } + + public function testJsonRequestWithContent(): void + { + $client = $this->getClient(); + + $this->expectException(JsonRequestWithContentException::class); + $client->request('POST', '/test', ['test' => true], [], [], 'test'); + } + + public function testnotJsonRequestWithContent(): void + { + $client = $this->getClient(); + $response = $client->request('POST', '/test', ['test' => true], [], [], 'test', true, false); + + self::assertStatusCode(Response::HTTP_NOT_FOUND, $response); + } } diff --git a/WebTest/Client.php b/WebTest/Client.php index 4695035..f32ea48 100644 --- a/WebTest/Client.php +++ b/WebTest/Client.php @@ -3,6 +3,7 @@ namespace RichCongress\WebTestBundle\WebTest; use Psr\Container\ContainerInterface; +use RichCongress\WebTestBundle\Exception\JsonRequestWithContentException; use Symfony\Bundle\FrameworkBundle\KernelBrowser; use Symfony\Component\BrowserKit\Request; use Symfony\Component\DomCrawler\Crawler; @@ -36,8 +37,20 @@ public function request( array $files = [], array $server = [], string $content = null, - bool $changeHistory = true + bool $changeHistory = true, + bool $isJson = true ): Response { + if ($isJson) { + if ($content !== null) { + JsonRequestWithContentException::throw(); + } + + $content = json_encode($parameters, JSON_THROW_ON_ERROR); + $parameters = []; + $server['CONTENT_TYPE'] = 'application/json'; + $server['HTTP_ACCEPT'] = 'application/json'; + } + $this->browser->request( $method, $uri, @@ -58,7 +71,8 @@ public function get( array $files = [], array $server = [], string $content = null, - bool $changeHistory = true + bool $changeHistory = true, + bool $isJson = true ): Response { return $this->request( 'GET', @@ -67,7 +81,8 @@ public function get( $files, $server, $content, - $changeHistory + $changeHistory, + $isJson ); } @@ -78,7 +93,8 @@ public function post( array $files = [], array $server = [], string $content = null, - bool $changeHistory = true + bool $changeHistory = true, + bool $isJson = true ): Response { return $this->request( 'POST', @@ -87,7 +103,8 @@ public function post( $files, $server, $content, - $changeHistory + $changeHistory, + $isJson ); } @@ -98,7 +115,8 @@ public function put( array $files = [], array $server = [], string $content = null, - bool $changeHistory = true + bool $changeHistory = true, + bool $isJson = true ): Response { return $this->request( 'PUT', @@ -107,7 +125,8 @@ public function put( $files, $server, $content, - $changeHistory + $changeHistory, + $isJson ); } @@ -118,7 +137,8 @@ public function patch( array $files = [], array $server = [], string $content = null, - bool $changeHistory = true + bool $changeHistory = true, + bool $isJson = true ): Response { return $this->request( 'PATCH', @@ -127,7 +147,8 @@ public function patch( $files, $server, $content, - $changeHistory + $changeHistory, + $isJson ); } @@ -138,7 +159,8 @@ public function delete( array $files = [], array $server = [], string $content = null, - bool $changeHistory = true + bool $changeHistory = true, + bool $isJson = true ): Response { return $this->request( 'DELETE', @@ -147,7 +169,8 @@ public function delete( $files, $server, $content, - $changeHistory + $changeHistory, + $isJson ); }