-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c30cfb3
commit 163c402
Showing
4 changed files
with
217 additions
and
0 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
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; | ||
} | ||
} |
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,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); | ||
} | ||
} | ||
} |
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,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]; | ||
} | ||
} |
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,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]; | ||
} | ||
} |