-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from smartbooster/add_standard_abstract
add AbstractWebTestCase + AbstractValidatorTest
- Loading branch information
Showing
3 changed files
with
149 additions
and
4 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,91 @@ | ||
<?php | ||
|
||
namespace Smart\StandardBundle; | ||
|
||
use Doctrine\ORM\EntityManagerInterface; | ||
use Liip\TestFixturesBundle\Services\DatabaseToolCollection; | ||
use Liip\TestFixturesBundle\Services\DatabaseTools\AbstractDatabaseTool; | ||
use Symfony\Bundle\FrameworkBundle\KernelBrowser; | ||
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; | ||
|
||
abstract class AbstractWebTestCase extends WebTestCase | ||
{ | ||
protected ?KernelBrowser $client = null; | ||
protected ?EntityManagerInterface $entityManager = null; | ||
protected AbstractDatabaseTool $databaseTool; | ||
|
||
public function setUp(): void | ||
{ | ||
parent::setUp(); | ||
|
||
$this->client = self::createClient(); | ||
$this->entityManager = static::getContainer()->get(EntityManagerInterface::class); | ||
// https://github.com/liip/LiipTestFixturesBundle/blob/2.x/UPGRADE-2.0.md | ||
// @phpstan-ignore-next-line | ||
$this->databaseTool = static::getContainer()->get(DatabaseToolCollection::class)->get(); | ||
} | ||
|
||
protected function tearDown(): void | ||
{ | ||
parent::tearDown(); | ||
// https://github.com/liip/LiipTestFixturesBundle/pull/196/files | ||
unset($this->databaseTool); | ||
|
||
// avoid memory leaks | ||
if ($this->entityManager != null) { | ||
$this->entityManager->close(); | ||
$this->entityManager = null; | ||
} | ||
} | ||
|
||
protected function getParameter(string $name): mixed | ||
{ | ||
return static::getContainer()->getParameter($name); | ||
} | ||
|
||
protected function getProjectTestDir(): string | ||
{ | ||
return $this->getParameter('kernel.project_dir') . '/tests'; | ||
} | ||
|
||
protected function getFixturesDir(): string | ||
{ | ||
return $this->getProjectTestDir() . '/fixtures'; | ||
} | ||
|
||
protected function getMinimalFixturesDir(): string | ||
{ | ||
return $this->getProjectTestDir() . '/../fixtures/minimal'; | ||
} | ||
|
||
protected function getCsvDir(): string | ||
{ | ||
return $this->getProjectTestDir() . '/csv'; | ||
} | ||
|
||
/** | ||
* Return object property who can be private | ||
* https://www.yellowduck.be/posts/test-private-and-protected-properties-using-phpunit | ||
*/ | ||
public static function getProperty(object $object, string $property): mixed | ||
{ | ||
$reflectedClass = new \ReflectionClass($object); | ||
$reflection = $reflectedClass->getProperty($property); | ||
$reflection->setAccessible(true); | ||
|
||
return $reflection->getValue($object); | ||
} | ||
|
||
/** | ||
* Return object method who can be private | ||
* https://stackoverflow.com/questions/249664/best-practices-to-test-protected-methods-with-phpunit | ||
* @param class-string $class | ||
*/ | ||
protected static function getMethod(string $name, string $class): \ReflectionMethod | ||
{ | ||
$method = (new \ReflectionClass($class))->getMethod($name); | ||
$method->setAccessible(true); | ||
|
||
return $method; | ||
} | ||
} |
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,52 @@ | ||
<?php | ||
|
||
namespace Smart\StandardBundle\Validator\Constraints; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Symfony\Component\Validator\ConstraintValidator; | ||
use Symfony\Component\Validator\Context\ExecutionContext; | ||
use Symfony\Component\Validator\Violation\ConstraintViolationBuilder; | ||
|
||
abstract class AbstractValidatorTest extends TestCase | ||
{ | ||
/** | ||
* Return an instance from the validator to test | ||
* | ||
* @return ConstraintValidator | ||
*/ | ||
abstract protected function getValidatorInstance(); | ||
|
||
/** | ||
* @param string|null $expectedMessage | ||
* | ||
* @return ConstraintValidator | ||
*/ | ||
protected function initValidator($expectedMessage = null) | ||
{ | ||
$builder = $this->getMockBuilder(ConstraintViolationBuilder::class) | ||
->disableOriginalConstructor() | ||
->setMethods(['addViolation']) | ||
->getMock(); | ||
|
||
$context = $this->getMockBuilder(ExecutionContext::class) | ||
->disableOriginalConstructor() | ||
->setMethods(['buildViolation']) | ||
->getMock(); | ||
|
||
if ($expectedMessage) { | ||
$context->expects($this->atLeastOnce()) | ||
->method('buildViolation') | ||
->with($this->equalTo($expectedMessage)) | ||
->will($this->returnValue($builder)) | ||
; | ||
} else { | ||
$context->expects($this->never())->method('buildViolation'); | ||
} | ||
|
||
$validator = $this->getValidatorInstance(); | ||
/* @var ExecutionContext $context */ | ||
$validator->initialize($context); | ||
|
||
return $validator; | ||
} | ||
} |