Skip to content

Commit

Permalink
Day 15: added form tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sergey-bleih authored and gregurco committed Oct 8, 2019
1 parent c30cfb3 commit 163c402
Show file tree
Hide file tree
Showing 4 changed files with 217 additions and 0 deletions.
100 changes: 100 additions & 0 deletions tests/Form/AbstractTypeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
<?php

namespace App\Tests\Form;

use Doctrine\Common\Persistence\ManagerRegistry;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\Tools\SchemaTool;
use PHPUnit\Framework\MockObject\MockObject;
use Symfony\Bridge\Doctrine\Form\DoctrineOrmExtension;
use Symfony\Bridge\Doctrine\Test\DoctrineTestHelper;
use Symfony\Component\Form\Extension\Validator\ValidatorExtension;
use Symfony\Component\Form\Test\TypeTestCase;
use Symfony\Component\Validator\Validation;

abstract class AbstractTypeTest extends TypeTestCase
{
/**
* @var EntityManagerInterface
*/
private $em;

/**
* @var MockObject|ManagerRegistry
*/
private $emRegistry;

/**
* @throws \Doctrine\ORM\Tools\ToolsException
*/
protected function setUp(): void
{
$this->em = DoctrineTestHelper::createTestEntityManager();
$this->emRegistry = $this->createRegistryMock($this->em);

parent::setUp();

$schemaTool = new SchemaTool($this->em);
$classNames = $this->getEntityClassNames();
$classData = [];
foreach ($classNames as $class) {
$classData[] = $this->em->getClassMetadata($class);
}
$schemaTool->dropSchema($classData);
$schemaTool->createSchema($classData);
}

/**
* @param $em
*
* @return MockObject|ManagerRegistry
*/
protected function createRegistryMock($em): MockObject
{
$registry = $this
->getMockBuilder(ManagerRegistry::class)
->getMock();

$registry
->method('getManagerForClass')
->willReturn($em);

return $registry;
}

/**
* @return array
*/
protected function getExtensions(): array
{
$validator = Validation::createValidatorBuilder()
->enableAnnotationMapping()
->getValidator();

return array_merge(
parent::getExtensions(),
[
new DoctrineOrmExtension($this->emRegistry),
new ValidatorExtension($validator),
]
);
}

/**
* Should return FQCN for all used types
*
* @return array
*/
protected function getEntityClassNames(): array
{
return [];
}

protected function tearDown()
{
parent::tearDown();

$this->em = null;
$this->emRegistry = null;
}
}
29 changes: 29 additions & 0 deletions tests/Form/Admin/CategoryTypeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace App\Tests\Form\Admin;

use App\Entity\Category;
use App\Form\Admin\CategoryType;
use App\Tests\Form\AbstractTypeTest;

class CategoryTypeTest extends AbstractTypeTest
{
public function testSubmitValidData(): void
{
$testCategory = new Category();
$form = $this->factory->create(CategoryType::class, $testCategory);

$submittedData = [
'name' => '_COMPANY_',
];

$form->submit($submittedData);
$this->assertTrue($form->isSynchronized());

$view = $form->createView();
$children = $view->children;
foreach (array_keys($submittedData) as $key) {
$this->assertArrayHasKey($key, $children);
}
}
}
39 changes: 39 additions & 0 deletions tests/Form/AffiliateTypeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace App\Tests\Form;

use App\Entity\Affiliate;
use App\Entity\Category;
use App\Form\AffiliateType;

class AffiliateTypeTest extends AbstractTypeTest
{
public function testSubmitValidData(): void
{
$testAffiliate = new Affiliate();
$form = $this->factory->create(AffiliateType::class, $testAffiliate);

$submittedData = [
'url' => 'https://test.local',
'email' => '[email protected]',
'categories' => [new Category()],
];

$form->submit($submittedData);
$this->assertTrue($form->isSynchronized());

$view = $form->createView();
$children = $view->children;
foreach (array_keys($submittedData) as $key) {
$this->assertArrayHasKey($key, $children);
}
}

/**
* {@inheritdoc}
*/
protected function getEntityClassNames(): array
{
return [Category::class];
}
}
49 changes: 49 additions & 0 deletions tests/Form/JobTypeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

namespace App\Tests\Form;

use App\Entity\Category;
use App\Entity\Job;
use App\Form\JobType;
use Symfony\Component\HttpFoundation\File\UploadedFile;

class JobTypeTest extends AbstractTypeTest
{
public function testSubmitValidData(): void
{
$testJob = new Job();
$form = $this->factory->create(JobType::class, $testJob);

$submittedData = [
'type' => 'full-time',
'company' => '_COMPANY_',
'logo' => $this->createMock(UploadedFile::class),
'url' => 'https://test.local',
'position' => '_POSITION_',
'location' => '_LOCATION_',
'description' => '_DESCRIPTION_',
'howToApply' => '_HOW_TO_APPLY_',
'public' => true,
'activated' => true,
'email' => '[email protected]',
'category' => new Category(),
];

$form->submit($submittedData);
$this->assertTrue($form->isSynchronized());

$view = $form->createView();
$children = $view->children;
foreach (array_keys($submittedData) as $key) {
$this->assertArrayHasKey($key, $children);
}
}

/**
* {@inheritdoc}
*/
protected function getEntityClassNames(): array
{
return [Category::class];
}
}

0 comments on commit 163c402

Please sign in to comment.