8000 Use typed properties in tests as much as possible by nicolas-grekas · Pull Request #51067 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

Use typed properties in tests as much as possible #51067

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ class ContainerAwareEventManagerTest extends TestCase
{
use ExpectDeprecationTrait;

private $container;
private $evm;
private Container $container;
private ContainerAwareEventManager $evm;

protected function setUp(): void
{
Expand Down Expand Up @@ -299,8 +299,8 @@ public function testRemoveEventListenerAfterDispatchEvent()

class MyListener
{
public $calledByInvokeCount = 0;
public $calledByEventNameCount = 0;
public int $calledByInvokeCount = 0;
public int $calledByEventNameCount = 0;

public function __invoke()
{
Expand All @@ -315,8 +315,8 @@ public function foo()

class MySubscriber extends MyListener implements EventSubscriber
{
public $calledSubscribedEventsCount = 0;
private $listenedEvents;
public int $calledSubscribedEventsCount = 0;
private array $listenedEvents;

public function __construct(array $listenedEvents)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Symfony\Bridge\Doctrine\Tests\DependencyInjection;

use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Symfony\Bridge\Doctrine\DependencyInjection\AbstractDoctrineExtension;
use Symfony\Component\DependencyInjection\ContainerBuilder;
Expand All @@ -23,10 +24,7 @@
*/
class DoctrineExtensionTest extends TestCase
{
/**
* @var AbstractDoctrineExtension
*/
private $extension;
private MockObject&AbstractDoctrineExtension $extension;

protected function setUp(): void
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,62 +20,24 @@
use Symfony\Bridge\Doctrine\Form\ChoiceList\EntityLoaderInterface;
use Symfony\Bridge\Doctrine\Form\ChoiceList\IdReader;
use Symfony\Component\Form\ChoiceList\ArrayChoiceList;
use Symfony\Component\Form\ChoiceList\Factory\ChoiceListFactoryInterface;
use Symfony\Component\Form\Exception\LogicException;

/**
* @author Bernhard Schussek <bschussek@gmail.com>
*/
class DoctrineChoiceLoaderTest extends TestCase
{
/**
* @var MockObject&ChoiceListFactoryInterface
*/
private $factory;

/**
* @var MockObject&ObjectManager
*/
private $om;

/**
* @var MockObject&ObjectRepository
*/
private $repository;

/**
* @var string
*/
private $class;

/**
* @var MockObject&IdReader
*/
private $idReader;

/**
* @var MockObject&EntityLoaderInterface
*/
private $objectLoader;

/**
* @var \stdClass
*/
private $obj1;

/**
* @var \stdClass
*/
private $obj2;

/**
* @var \stdClass
*/
private $obj3;
private MockObject&ObjectManager $om;
private MockObject&ObjectRepository $repository;
private string $class;
private MockObject&IdReader $idReader;
private MockObject&EntityLoaderInterface $objectLoader;
private \stdClass $obj1;
private \stdClass $obj2;
private \stdClass $obj3;

protected function setUp(): void
{
$this->factory = $this->createMock(ChoiceListFactoryInterface::class);
$this->om = $this->createMock(ObjectManager::class);
$this->repository = $this->createMock(ObjectRepository::class);
$this->class = 'stdClass';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,7 @@
*/
class CollectionToArrayTransformerTest extends TestCase
{
/**
* @var CollectionToArrayTransformer
*/
private $transformer;
private CollectionToArrayTransformer $transformer;

protected function setUp(): void
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Symfony\Bridge\Doctrine\Tests\Form\EventListener;

use Doctrine\Common\Collections\ArrayCollection;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Symfony\Bridge\Doctrine\Form\EventListener\MergeDoctrineCollectionListener;
use Symfony\Component\EventDispatcher\EventDispatcher;
Expand All @@ -22,28 +23,15 @@

class MergeDoctrineCollectionListenerTest extends TestCase
{
/** @var \Doctrine\Common\Collections\ArrayCollection */
private $collection;
/** @var \Symfony\Component\EventDispatcher\EventDispatcher */
private $dispatcher;
private $factory;
private $form;
private ArrayCollection $collection;
private EventDispatcher $dispatcher;
private MockObject&FormFactoryInterface $factory;

protected function setUp(): void
{
$this->collection = new ArrayCollection(['test']);
$this->dispatcher = new EventDispatcher();
$this->factory = $this->createMock(FormFactoryInterface::class);
$this->form = $this->getBuilder()
->getForm();
}

protected function tearDown(): void
{
$this->collection = null;
$this->dispatcher = null;
$this->factory = null;
$this->form = null;
}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's stop adding those useless tearDown methods

Copy link
Member
@lyrixx lyrixx Jul 25, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I check if the memory consumption was the same => 🎉 it's the same! 👏🏼


But there is something a bit strange. about Framework bundle (I looked at this one because it fails)

  • On Github, it consumes more than 500Mb
  • locally, it consumes 244.00Mb

And locally, I have less skipped tests!

For the form component, it's almost the same


protected function getBuilder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Symfony\Bridge\Doctrine\Tests\Form\Type;

use Doctrine\ORM\EntityManager;
use Doctrine\ORM\Tools\SchemaTool;
use Doctrine\Persistence\ManagerRegistry;
use Symfony\Bridge\Doctrine\Form\DoctrineOrmExtension;
2364 Expand All @@ -26,10 +27,7 @@ class EntityTypePerformanceTest extends FormPerformanceTestCase
{
private const ENTITY_CLASS = 'Symfony\Bridge\Doctrine\Tests\Fixtures\SingleIntIdEntity';

/**
* @var \Doctrine\ORM\EntityManager
*/
private $em;
private EntityManager $em;

protected function getExtensions()
{
Expand Down
19 changes: 2 additions & 17 deletions src/Symfony/Bridge/Doctrine/Tests/Form/Type/EntityTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,8 @@ class EntityTypeTest extends BaseTypeTestCase
private const COMPOSITE_IDENT_CLASS = 'Symfony\Bridge\Doctrine\Tests\Fixtures\CompositeIntIdEntity';
private const COMPOSITE_STRING_IDENT_CLASS = 'Symfony\Bridge\Doctrine\Tests\Fixtures\CompositeStringIdEntity';

/**
* @var EntityManager
*/
private $em;

/**
* @var MockObject&ManagerRegistry
*/
private $emRegistry;
private EntityManager $em;
private MockObject&ManagerRegistry $emRegistry;

protected function setUp(): void
{
Expand Down Expand Up @@ -93,14 +86,6 @@ protected function setUp(): void
}
}

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

$this->em = null;
$this->emRegistry = null;
}

protected function getExtensions()
{
return array_merge(parent::getExtensions(), [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Doctrine\DBAL\Connection;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\Persistence\ManagerRegistry;
use PHPUnit\Framework\MockObject\MockObject;
use Symfony\Bridge\Doctrine\Messenger\DoctrineCloseConnectionMiddleware;
use Symfony\Component\Messenger\Envelope;
use Symfony\Component\Messenger\Exception\UnrecoverableMessageHandlingException;
Expand All @@ -22,11 +23,11 @@

class DoctrineCloseConnectionMiddlewareTest extends MiddlewareTestCase
{
private $connection;
private $entityManager;
private $managerRegistry;
private $middleware;
private $entityManagerName = 'default';
private MockObject&Connection $connection;
private MockObject&EntityManagerInterface $entityManager;
private MockObject&ManagerRegistry $managerRegistry;
private DoctrineCloseConnectionMiddleware $middleware;
private string $entityManagerName = 'default';

protected function setUp(): void
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,23 @@
use Doctrine\DBAL\Connection;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\Persistence\ManagerRegistry;
use PHPUnit\Framework\MockObject\MockObject;
use Psr\Log\AbstractLogger;
use Symfony\Bridge\Doctrine\Messenger\DoctrineOpenTransactionLoggerMiddleware;
use Symfony\Component\Messenger\Envelope;
use Symfony\Component\Messenger\Test\Middleware\MiddlewareTestCase;

class DoctrineOpenTransactionLoggerMiddlewareTest extends MiddlewareTestCase
{
private $logger;
private $connection;
private $entityManager;
private $middleware;
private AbstractLogger $logger;
private MockObject&Connection $connection;
private MockObject&EntityManagerInterface $entityManager;
private DoctrineOpenTransactionLoggerMiddleware $middleware;

protected function setUp(): void
{
$this->logger = new class() extends AbstractLogger {
public $logs = [];
public array $logs = [];

public function log($level, $message, $context = []): void
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Doctrine\DBAL\Exception as DBALException;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\Persistence\ManagerRegistry;
use PHPUnit\Framework\MockObject\MockObject;
use Symfony\Bridge\Doctrine\Messenger\DoctrinePingConnectionMiddleware;
use Symfony\Component\Messenger\Envelope;
use Symfony\Component\Messenger\Exception\UnrecoverableMessageHandlingException;
Expand All @@ -23,11 +24,11 @@

class DoctrinePingConnectionMiddlewareTest extends MiddlewareTestCase
{
private $connection;
private $entityManager;
private $managerRegistry;
private $middleware;
private $entityManagerName = 'default';
private MockObject&Connection $connection;
private MockObject&EntityManagerInterface $entityManager;
private MockObject&ManagerRegistry $managerRegistry;
private DoctrinePingConnectionMiddleware $middleware;
private string $entityManagerName = 'default';

protected function setUp(): void
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,17 @@
use Doctrine\DBAL\Connection;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\Persistence\ManagerRegistry;
use PHPUnit\Framework\MockObject\MockObject;
use Symfony\Bridge\Doctrine\Messenger\DoctrineTransactionMiddleware;
use Symfony\Component\Messenger\Envelope;
use Symfony\Component\Messenger\Exception\UnrecoverableMessageHandlingException;
use Symfony\Component\Messenger\Test\Middleware\MiddlewareTestCase;

class DoctrineTransactionMiddlewareTest extends MiddlewareTestCase
{
private $connection;
private $entityManager;
private $middleware;
private MockObject&Connection $connection;
private MockObject&EntityManagerInterface $entityManager;
private DoctrineTransactionMiddleware $middleware;

protected function setUp(): void
{
Expand Down
5 changes: 2 additions & 3 deletions src/Symfony/Bridge/Doctrine/Tests/Types/UlidTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,13 @@
use Symfony\Component\Uid\Ulid;

// DBAL 2 compatibility
class_exists('Doctrine\DBAL\Platforms\PostgreSqlPlatform');
class_exists(\Doctrine\DBAL\Platforms\PostgreSqlPlatform::class);

final class UlidTypeTest extends TestCase
{
private const DUMMY_ULID = '01EEDQEK6ZAZE93J8KG5B4MBJC';

/** @var UlidType */
private $type;
private UlidType $type;

public static function setUpBeforeClass(): void
{
Expand Down
7 changes: 3 additions & 4 deletions src/Symfony/Bridge/Doctrine/Tests/Types/UuidTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,14 @@
use Symfony\Component\Uid\Uuid;

// DBAL 2 compatibility
class_exists('Doctrine\DBAL\Platforms\MySqlPlatform');
class_exists('Doctrine\DBAL\Platforms\PostgreSqlPlatform');
class_exists(\Doctrine\DBAL\Platforms\MySqlPlatform::class);
class_exists(\Doctrine\DBAL\Platforms\PostgreSqlPlatform::class);

final class UuidTypeTest extends TestCase
{
private const DUMMY_UUID = '9f755235-5a2d-4aba-9605-e9962b312e50';

/** @var UuidType */
private $type;
private UuidType $type;

public static function setUpBeforeClass(): void
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,22 +48,10 @@ class UniqueEntityValidatorTest extends ConstraintValidatorTestCase
{
private const EM_NAME = 'foo';

/**
* @var ObjectManager
*/
protected $em;

/**
* @var ManagerRegistry
*/
protected $registry;

/**
* @var MockObject&EntityRepository
*/
protected $repository;

protected $repositoryFactory;
protected ?ObjectManager $em;
protected ManagerRegistry $registry;
protected MockObject&EntityRepository $repository;
protected TestRepositoryFactory $repositoryFactory;

protected function setUp(): void
{
Expand Down
Loading
0