Alessandro Lai / @AlessandroLai
PUG Milano - 20 gennaio 2020, online
If a build / test suite takes more than 10 minutes to run, it's worthless.
https://phpstan.org | https://psalm.dev |
When you ran multiple tests on the same DB at the same time, you run intro troubles...
facile-it/paraunit-testcase
(deprecated)
dama/doctrine-test-bundle
facile-it/paraunit-testcase
was built on top of something else...
liip/functional-test-bundle
(forked)
class MyTest extends WebTestCase
{
public function test(): void
{
$this->getContainer()->get('...');
}
}
facile-it/symfony-functional-testcase
*FixtureBuilder
utility classes to create fixtures in known valid states
src/
maglnet/composer-require-checker
public function testDeleteFoo(): void
{
// ...
$client->request('DELETE', '/api/foo/123');
$this->assertNull($fooRepo->find(123));
// no cleanup or fixture reload required!
}
public function testPutFoo(): void
{
// ...
$client->request('PUT', '/api/foo/123', $payload);
// ...
}
/**
* @template T of object
* @param class-string<T> $serviceId
*
* @return T
*/
protected function getService(string $serviceId): object
{
$service = $this->getContainer()->get($serviceId);
$this->assertInstanceOf($serviceId, $service);
return $service;
}
PHPStan and Psalm Support Coming to PhpStorm Soon - JetBrains' Blog
public function testFindLatestReturnsOnlyOneResult(): void
{
$repository = $this->getService(FooRepository::class);
$result = $repository->findLatest();
$this->assertCount(1, $result);
$this->assertContainsOnlyInstancesOf(Foo::class, $result);
}
protected function assertIsValid(object $entity): void
{
$validator = $this->getContainer()->get('validator');
$result = $validator->validate($entity);
$this->assertCount(0, $result, 'Validation failed: ' . $result);
}
protected function assertValidationFailsAtPath(string $path, object $entity): void
{
$violationList = $this->getContainer()->get('validator')->validate($entity);
foreach ($violationList as $violation) {
if ($violation->getPropertyPath() === $path) {
$this->increaseAssertionCount();
return;
}
}
$this->fail('No violation found at path ' . $path);
}
public function testNewFooIsValid(): void
{
$this->assertIsValid(new Foo('baz'));
}
/** @dataProvider invalidDataProvider */
public function testFooIsInvalid(Foo $foo, string $errorPath): void
{
$this->assertValidationFailsAtPath($errorPath, $foo);
}
public function testFooIsInvalidWithWrongBar(): void
{
$foo = new Foo();
$this->assertIsValid($foo);
$foo->setBar('invalid');
$this->assertValidationFailsAtPath('bar', $foo);
}
TRUNCATE
or any schema change)
\DAMA\DoctrineTestBundle\Doctrine\DBAL\StaticDriver::commit()
public function test(): void
{
$client = self::createClient();
$this->assertNotSame(
$this->getContainer(),
$client->getContainer(),
"Those are not the services you're looking for..."
);
}
protected function getService(
string $serviceId,
KernelBrowser $client = null
): object
{
return $this->getContainer($client)->get($serviceId);
}
protected function getContainer(KernelBrowser $client = null): ContainerInterface
{
if ($client) {
$clientContainer = $client->getContainer();
return $clientContainer->get('test.service_container');
}
return parent::getContainer();
}