Skip to content

Commit

Permalink
Merge pull request #7 from smartbooster/add_standard_abstract
Browse files Browse the repository at this point in the history
add AbstractWebTestCase + AbstractValidatorTest
  • Loading branch information
mathieu-ducrot committed Jan 2, 2024
2 parents 2ecae6c + b76a7e5 commit 4d317d3
Show file tree
Hide file tree
Showing 3 changed files with 149 additions and 4 deletions.
10 changes: 6 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,16 @@
],
"require": {
"php": "^8.1",
"symfony/framework-bundle": "^5.4|^6.2",
"dama/doctrine-test-bundle": "^7.1",
"doctrine/orm": "^2.13",
"liip/test-fixtures-bundle": "^2.4",
"phpmetrics/phpmetrics": "^2.8",
"phpstan/phpstan-doctrine": "^1.3",
"phpstan/phpstan-symfony": "^1.2",
"sebastian/phpcpd": "^6.0",
"squizlabs/php_codesniffer": "^3.7",
"phpmetrics/phpmetrics": "^2.8",
"sebastian/phpcpd": "^6.0"
"symfony/framework-bundle": "^5.4|^6.2",
"symfony/validator": "^5.4|^6.2"
},
"autoload": {
"psr-4": {
Expand All @@ -44,4 +46,4 @@
"dev-master": "0.4.x-dev"
}
}
}
}
91 changes: 91 additions & 0 deletions src/AbstractWebTestCase.php
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;
}
}
52 changes: 52 additions & 0 deletions src/Validator/Constraints/AbstractValidatorTest.php
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;
}
}

0 comments on commit 4d317d3

Please sign in to comment.