-
-
Notifications
You must be signed in to change notification settings - Fork 344
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add tests for collection removal without triggering validation (#1737)
- Loading branch information
1 parent
8c47894
commit 18d5bd5
Showing
9 changed files
with
346 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of the Sonata Project package. | ||
* | ||
* (c) Thomas Rabaix <thomas.rabaix@sonata-project.org> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Sonata\DoctrineORMAdminBundle\Tests\App\Admin; | ||
|
||
use Sonata\AdminBundle\Admin\AbstractAdmin; | ||
use Sonata\AdminBundle\Datagrid\ListMapper; | ||
use Sonata\AdminBundle\Form\FormMapper; | ||
use Sonata\DoctrineORMAdminBundle\Tests\App\Entity\Child; | ||
|
||
/** | ||
* @phpstan-extends AbstractAdmin<Child> | ||
*/ | ||
final class ChildAdmin extends AbstractAdmin | ||
{ | ||
protected function configureListFields(ListMapper $list): void | ||
{ | ||
$list | ||
->add('id') | ||
->addIdentifier('name') | ||
->add('anotherName'); | ||
} | ||
|
||
protected function configureFormFields(FormMapper $form): void | ||
{ | ||
$form | ||
->add('name') | ||
->add('anotherName'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of the Sonata Project package. | ||
* | ||
* (c) Thomas Rabaix <thomas.rabaix@sonata-project.org> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Sonata\DoctrineORMAdminBundle\Tests\App\Admin; | ||
|
||
use Sonata\AdminBundle\Admin\AbstractAdmin; | ||
use Sonata\AdminBundle\Datagrid\ListMapper; | ||
use Sonata\AdminBundle\Form\FormMapper; | ||
use Sonata\DoctrineORMAdminBundle\Tests\App\Entity\Mother; | ||
use Sonata\Form\Type\CollectionType; | ||
use Symfony\Component\Validator\Constraints as Assert; | ||
|
||
/** | ||
* @phpstan-extends AbstractAdmin<Mother> | ||
*/ | ||
final class MotherAdmin extends AbstractAdmin | ||
{ | ||
protected function configureListFields(ListMapper $list): void | ||
{ | ||
$list->addIdentifier('id'); | ||
} | ||
|
||
protected function configureFormFields(FormMapper $form): void | ||
{ | ||
$form->add('children', CollectionType::class, [ | ||
'by_reference' => false, | ||
'constraints' => [ | ||
new Assert\Valid(), | ||
], | ||
], [ | ||
'edit' => 'inline', | ||
'inline' => 'table', | ||
]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of the Sonata Project package. | ||
* | ||
* (c) Thomas Rabaix <thomas.rabaix@sonata-project.org> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Sonata\DoctrineORMAdminBundle\Tests\App\DataFixtures; | ||
|
||
use Doctrine\Bundle\FixturesBundle\Fixture; | ||
use Doctrine\Common\DataFixtures\FixtureInterface; | ||
use Doctrine\Persistence\ObjectManager; | ||
use Sonata\DoctrineORMAdminBundle\Tests\App\Entity\Child; | ||
use Sonata\DoctrineORMAdminBundle\Tests\App\Entity\Mother; | ||
|
||
final class MotherFixtures extends Fixture implements FixtureInterface | ||
{ | ||
public const MOTHER = 'mother'; | ||
|
||
public function load(ObjectManager $manager): void | ||
{ | ||
$mother = new Mother(); | ||
$this->addChildren($mother, 2); | ||
|
||
$manager->persist($mother); | ||
$manager->flush(); | ||
|
||
$this->addReference(self::MOTHER, $mother); | ||
} | ||
|
||
private function addChildren(Mother $mother, int $children): void | ||
{ | ||
for ($i = 0; $i < $children; ++$i) { | ||
$child = new Child(); | ||
$child->setName('Child '.$i); | ||
$child->setAnotherName('Another Name '.$i); | ||
|
||
$mother->addChild($child); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of the Sonata Project package. | ||
* | ||
* (c) Thomas Rabaix <thomas.rabaix@sonata-project.org> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Sonata\DoctrineORMAdminBundle\Tests\App\Entity; | ||
|
||
use Doctrine\DBAL\Types\Types; | ||
use Doctrine\ORM\Mapping as ORM; | ||
use Symfony\Component\Validator\Constraints as Assert; | ||
|
||
#[ORM\Entity] | ||
class Child implements \Stringable | ||
{ | ||
#[ORM\Id] | ||
#[ORM\Column(type: Types::INTEGER)] | ||
#[ORM\GeneratedValue] | ||
private ?int $id = null; | ||
|
||
#[ORM\Column(type: Types::STRING)] | ||
#[Assert\NotBlank] | ||
private ?string $name = null; | ||
|
||
#[ORM\Column(type: Types::STRING)] | ||
#[Assert\NotBlank] | ||
private ?string $anotherName = null; | ||
|
||
public function __toString(): string | ||
{ | ||
return $this->name ?? '-'; | ||
} | ||
|
||
public function getId(): ?int | ||
{ | ||
return $this->id; | ||
} | ||
|
||
public function setId(?int $id): void | ||
{ | ||
$this->id = $id; | ||
} | ||
|
||
public function getName(): ?string | ||
{ | ||
return $this->name; | ||
} | ||
|
||
public function setName(?string $name): void | ||
{ | ||
$this->name = $name; | ||
} | ||
|
||
public function getAnotherName(): ?string | ||
{ | ||
return $this->anotherName; | ||
} | ||
|
||
public function setAnotherName(?string $anotherName): void | ||
{ | ||
$this->anotherName = $anotherName; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of the Sonata Project package. | ||
* | ||
* (c) Thomas Rabaix <thomas.rabaix@sonata-project.org> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Sonata\DoctrineORMAdminBundle\Tests\App\Entity; | ||
|
||
use Doctrine\Common\Collections\ArrayCollection; | ||
use Doctrine\Common\Collections\Collection; | ||
use Doctrine\DBAL\Types\Types; | ||
use Doctrine\ORM\Mapping as ORM; | ||
|
||
#[ORM\Entity] | ||
class Mother implements \Stringable | ||
{ | ||
#[ORM\Id] | ||
#[ORM\Column(type: Types::INTEGER)] | ||
#[ORM\GeneratedValue] | ||
private ?int $id = null; | ||
|
||
/** | ||
* @var Collection<array-key, Child> | ||
*/ | ||
#[ORM\ManyToMany(targetEntity: Child::class, cascade: ['persist', 'remove'])] | ||
private Collection $children; | ||
|
||
public function __construct() | ||
{ | ||
$this->children = new ArrayCollection(); | ||
} | ||
|
||
public function __toString(): string | ||
{ | ||
return (string) $this->id; | ||
} | ||
|
||
public function getId(): ?int | ||
{ | ||
return $this->id; | ||
} | ||
|
||
public function setId(?int $id): void | ||
{ | ||
$this->id = $id; | ||
} | ||
|
||
public function addChild(Child $child): void | ||
{ | ||
$this->children->add($child); | ||
} | ||
|
||
public function removeChild(Child $child): void | ||
{ | ||
$this->children->removeElement($child); | ||
} | ||
|
||
/** | ||
* @return Collection<array-key, Child> | ||
*/ | ||
public function getChildren(): Collection | ||
{ | ||
return $this->children; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of the Sonata Project package. | ||
* | ||
* (c) Thomas Rabaix <thomas.rabaix@sonata-project.org> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Sonata\DoctrineORMAdminBundle\Tests\Functional; | ||
|
||
use Symfony\Component\HttpFoundation\Request; | ||
use Symfony\Component\Panther\DomCrawler\Crawler; | ||
|
||
final class CollectionTypeTest extends BasePantherTestCase | ||
{ | ||
public function testRemoveCollectionItemWithoutValidation(): void | ||
{ | ||
$crawler = $this->client->request(Request::METHOD_GET, '/admin/tests/app/mother/1/edit?uniqid=mother'); | ||
|
||
$form = $crawler->selectButton('Update')->form(); | ||
$form['mother[children][0][name]'] = ''; | ||
|
||
$crawler->filter('#mother_children_0__delete + ins')->each(static function (Crawler $checkbox): void { | ||
$checkbox->click(); | ||
}); | ||
|
||
$this->client->submit($form); | ||
|
||
self::assertSelectorTextContains('.alert-success', 'Item "1" has been successfully updated.'); | ||
} | ||
|
||
public function testTriggerCollectionValidation(): void | ||
{ | ||
$crawler = $this->client->request(Request::METHOD_GET, '/admin/tests/app/mother/1/edit?uniqid=mother'); | ||
|
||
$form = $crawler->selectButton('Update')->form(); | ||
$form['mother[children][0][name]'] = ''; | ||
|
||
$this->client->submit($form); | ||
|
||
self::assertSelectorTextContains('.alert-danger', 'An error has occurred during update of item "1".'); | ||
} | ||
} |