Skip to content

Commit

Permalink
Merge pull request #2 from rich-id/v0.1.3
Browse files Browse the repository at this point in the history
Fix the json bad handling when making a request
  • Loading branch information
NicolasGuilloux authored Sep 15, 2021
2 parents fcfb6f4 + 076f1f1 commit 319979e
Show file tree
Hide file tree
Showing 11 changed files with 114 additions and 58 deletions.
25 changes: 25 additions & 0 deletions Exception/AbstractException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php declare(strict_types=1);

namespace RichCongress\WebTestBundle\Exception;

abstract class AbstractException extends \LogicException
{
/** @var string */
protected static $error;

public function __construct()
{
$message = static::$error;
$message .= "\n";
$message .= 'Check the documentation: ';
$message .= 'https://github.com/richcongress/web-test-bundle/blob/master/Docs/Exceptions.md#';
$message .= static::class;

parent::__construct($message);
}

public static function throw(): void
{
throw new static();
}
}
16 changes: 1 addition & 15 deletions Exception/CsrfTokenManagerMissingException.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,8 @@
* @author Nicolas Guilloux <[email protected]>
* @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);
}
}
18 changes: 1 addition & 17 deletions Exception/EntityManagerNotFoundException.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,9 @@
* @package RichCongress\WebTestBundle\Exception
* @author Nicolas Guilloux <[email protected]>
* @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);
}
}
9 changes: 9 additions & 0 deletions Exception/JsonRequestWithContentException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php declare(strict_types=1);

namespace RichCongress\WebTestBundle\Exception;

class JsonRequestWithContentException extends AbstractException
{
/** @var string */
protected static $error = 'You cannot make a json request with both parameters and content set.';
}
16 changes: 1 addition & 15 deletions Exception/KernelNotInitializedException.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,8 @@
* @author Nicolas Guilloux <[email protected]>
* @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);
}
}
1 change: 1 addition & 0 deletions Tests/Exceptions/CsrfTokenManagerMissingExceptionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
1 change: 1 addition & 0 deletions Tests/Exceptions/EntityManagerNotFoundExceptionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
23 changes: 23 additions & 0 deletions Tests/Exceptions/JsonRequestWithContentExceptionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php declare(strict_types=1);

namespace RichCongress\WebTestBundle\Tests\Exceptions;

use RichCongress\WebTestBundle\Exception\JsonRequestWithContentException;
use RichCongress\WebTestBundle\TestCase\TestCase;

/**
* @covers \RichCongress\WebTestBundle\Exception\JsonRequestWithContentException
* @covers \RichCongress\WebTestBundle\Exception\AbstractException
*/
final class JsonRequestWithContentExceptionTest extends TestCase
{
public function testException(): void
{
$exception = new JsonRequestWithContentException();

self::assertStringContainsString(
'You cannot make a json request with both parameters and content set.',
$exception->getMessage()
);
}
}
1 change: 1 addition & 0 deletions Tests/Exceptions/KernelNotInitializedExceptionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
17 changes: 17 additions & 0 deletions Tests/WebTest/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}
45 changes: 34 additions & 11 deletions WebTest/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand All @@ -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',
Expand All @@ -67,7 +81,8 @@ public function get(
$files,
$server,
$content,
$changeHistory
$changeHistory,
$isJson
);
}

Expand All @@ -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',
Expand All @@ -87,7 +103,8 @@ public function post(
$files,
$server,
$content,
$changeHistory
$changeHistory,
$isJson
);
}

Expand All @@ -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',
Expand All @@ -107,7 +125,8 @@ public function put(
$files,
$server,
$content,
$changeHistory
$changeHistory,
$isJson
);
}

Expand All @@ -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',
Expand All @@ -127,7 +147,8 @@ public function patch(
$files,
$server,
$content,
$changeHistory
$changeHistory,
$isJson
);
}

Expand All @@ -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',
Expand All @@ -147,7 +169,8 @@ public function delete(
$files,
$server,
$content,
$changeHistory
$changeHistory,
$isJson
);
}

Expand Down

0 comments on commit 319979e

Please sign in to comment.