Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
maks-rafalko committed May 26, 2024
1 parent a58fa17 commit 495a624
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 5 deletions.
3 changes: 3 additions & 0 deletions app/src/Controller/AstController.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;
use Webmozart\Assert\Assert;

class AstController extends AbstractController
{
Expand Down Expand Up @@ -92,6 +93,8 @@ public function createExample(Request $request, EntityManagerInterface $em): Res
$form->handleRequest($request);

if ($form->isSubmitted() && $form->isValid()) {
Assert::notNull($createAstRunRequest->code);

$existingAstRun = $this->astRunRepository->findContentByHash(
AstRun::hashInput($createAstRunRequest->code)
);
Expand Down
5 changes: 1 addition & 4 deletions app/src/Form/CreateAstRunType.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,7 @@

class CreateAstRunType extends AbstractType
{
/**
* @var UrlGeneratorInterface
*/
private $urlGenerator;
private UrlGeneratorInterface $urlGenerator;

public function __construct(UrlGeneratorInterface $urlGenerator)
{
Expand Down
2 changes: 1 addition & 1 deletion app/src/Request/CreateAstRunRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class CreateAstRunRequest
{
#[AppAssert\ValidPhpCode]
#[Assert\NotBlank]
public string $code;
public ?string $code = null;

public static function fromEntity(AstRun $astRun): self
{
Expand Down
89 changes: 89 additions & 0 deletions app/tests/Functional/Controller/AstControllerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
<?php
/**
* This code is licensed under the BSD 3-Clause License.
*
* Copyright (c) 2017, Maks Rafalko
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* * Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

declare(strict_types=1);

namespace App\Tests\Functional\Controller;

use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;

class AstControllerTest extends WebTestCase
{
public function test_it_creates_an_ast_run(): void
{
$client = static::createClient();
$client->catchExceptions(false);

$client->request('GET', '/ast');
static::assertSame(200, $client->getResponse()->getStatusCode());

$client->submitForm('create_ast_run[buildAst]', [
'create_ast_run[code]' => 'code',
]);

static::assertSame(302, $client->getResponse()->getStatusCode());
static::assertStringNotContainsString('This value should not be blank', (string) $client->getResponse()->getContent());
}

public function test_it_fails_with_when_code_is_blank(): void
{
$client = static::createClient();
$client->followRedirects();
$client->catchExceptions(false);

$client->request('GET', '/ast');

$client->submitForm('create_ast_run[buildAst]', [
'create_ast_run[code]' => '',
]);

static::assertSame(200, $client->getResponse()->getStatusCode());
static::assertStringContainsString('This value should not be blank', (string) $client->getResponse()->getContent());
}

public function test_it_fails_with_when_code_is_invalid(): void
{
$client = static::createClient();
$client->followRedirects();
$client->catchExceptions(false);

$client->request('GET', '/ast');

$client->submitForm('create_ast_run[buildAst]', [
'create_ast_run[code]' => '<?php $a ==== $b;',
]);

static::assertSame(200, $client->getResponse()->getStatusCode());
static::assertStringContainsString('This is not a valid PHP code. Errors: Syntax error, unexpected', (string) $client->getResponse()->getContent());
}
}

0 comments on commit 495a624

Please sign in to comment.