Skip to content

Commit

Permalink
Test validation error code
Browse files Browse the repository at this point in the history
  • Loading branch information
Zales0123 committed Oct 18, 2022
1 parent 1127c3d commit cf06855
Show file tree
Hide file tree
Showing 6 changed files with 132 additions and 6 deletions.
7 changes: 7 additions & 0 deletions src/Bundle/test/config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,13 @@ services:
- '%app.model.book.class%'
tags: ['form.type']

app.form.type.science_book:
class: App\Form\Type\ScienceBookType
arguments:
- '%app.model.science_book.class%'
- ['sylius']
tags: [ 'form.type' ]

app.form.type.book_translation:
class: App\Form\Type\BookTranslationType
arguments:
Expand Down
1 change: 1 addition & 0 deletions src/Bundle/test/config/sylius/resources.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ sylius_resource:
app.science_book:
classes:
model: App\Entity\ScienceBook
form: App\Form\Type\ScienceBookType

app.gedmo:
classes:
Expand Down
16 changes: 16 additions & 0 deletions src/Bundle/test/config/validator/validation.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
App\Entity\ScienceBook:
properties:
title:
- NotBlank:
groups: [sylius]
author:
- Valid: ~

App\Entity\Author:
properties:
firstName:
- NotBlank:
groups: [sylius]
lastName:
- NotBlank:
groups: [sylius]
28 changes: 28 additions & 0 deletions src/Bundle/test/src/Form/Type/AuthorType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace App\Form\Type;

use App\Entity\Author;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

final class AuthorType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder
->add('firstName', TextType::class)
->add('lastName', TextType::class)
;
}

public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefault('data_class', Author::class);
$resolver->setDefault('validation_groups', ['sylius']);
}
}
29 changes: 29 additions & 0 deletions src/Bundle/test/src/Form/Type/ScienceBookType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

/*
* This file is part of the Sylius package.
*
* (c) Paweł Jędrzejewski
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace App\Form\Type;

use Sylius\Bundle\ResourceBundle\Form\Type\AbstractResourceType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;

final class ScienceBookType extends AbstractResourceType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('title', TextType::class)
->add('author', AuthorType::class)
;
}
}
57 changes: 51 additions & 6 deletions src/Bundle/test/src/Tests/Controller/ScienceBookUiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,9 @@ public function it_allows_creating_a_book(): void

$this->client->request('GET', '/science-books/new');
$this->client->submitForm('Create', [
'sylius_resource[title]' => $newBookTitle,
'sylius_resource[author][firstName]' => $newBookAuthorFirstName,
'sylius_resource[author][lastName]' => $newBookAuthorLastName,
'science_book[title]' => $newBookTitle,
'science_book[author][firstName]' => $newBookAuthorFirstName,
'science_book[author][lastName]' => $newBookAuthorLastName,
]);

$this->assertResponseRedirects(null, expectedCode: Response::HTTP_FOUND);
Expand All @@ -84,6 +84,28 @@ public function it_allows_creating_a_book(): void
$this->assertSame($newBookAuthorLastName, $book->getAuthorLastName());
}

/** @test */
public function it_does_not_allow_to_create_a_book_if_there_is_a_validation_error(): void
{
$newBookTitle = 'The Book of Why';
$newBookAuthorLastName = 'Pearl';

$this->loadFixturesFromFile('science_books.yml');

$this->client->request('GET', '/science-books/new');
$this->client->submitForm('Create', [
'science_book[title]' => $newBookTitle,
'science_book[author][lastName]' => $newBookAuthorLastName,
]);

$this->assertResponseCode($this->client->getResponse(), Response::HTTP_UNPROCESSABLE_ENTITY);

/** @var ScienceBook $book */
$book = static::getContainer()->get('app.repository.science_book')->findOneBy(['title' => $newBookTitle]);

$this->assertNull($book);
}

/** @test */
public function it_allows_updating_a_book(): void
{
Expand All @@ -95,9 +117,9 @@ public function it_allows_updating_a_book(): void

$this->client->request('GET', '/science-books/' . $scienceBooks['science-book1']->getId() . '/edit');
$this->client->submitForm('Save changes', [
'sylius_resource[title]' => $newBookTitle,
'sylius_resource[author][firstName]' => $newBookAuthorFirstName,
'sylius_resource[author][lastName]' => $newBookAuthorLastName,
'science_book[title]' => $newBookTitle,
'science_book[author][firstName]' => $newBookAuthorFirstName,
'science_book[author][lastName]' => $newBookAuthorLastName,
]);

$this->assertResponseRedirects(null, expectedCode: Response::HTTP_FOUND);
Expand All @@ -111,6 +133,29 @@ public function it_allows_updating_a_book(): void
$this->assertSame($newBookAuthorLastName, $book->getAuthorLastName());
}

/** @test */
public function it_does_not_allow_to_update_a_book_if_there_is_a_validation_error(): void
{
$newBookTitle = 'The Book of Why';
$newBookAuthorLastName = 'Pearl';

$scienceBooks = $this->loadFixturesFromFile('science_books.yml');

$this->client->request('GET', '/science-books/' . $scienceBooks['science-book1']->getId() . '/edit');
$this->client->submitForm('Save changes', [
'science_book[title]' => $newBookTitle,
'science_book[author][firstName]' => null,
'science_book[author][lastName]' => $newBookAuthorLastName,
]);

$this->assertResponseCode($this->client->getResponse(), Response::HTTP_UNPROCESSABLE_ENTITY);

/** @var ScienceBook $book */
$book = static::getContainer()->get('app.repository.science_book')->findOneBy(['title' => $newBookTitle]);

$this->assertNull($book);
}

/** @test */
public function it_allows_deleting_a_book(): void
{
Expand Down

0 comments on commit cf06855

Please sign in to comment.