Skip to content

Commit

Permalink
Add events
Browse files Browse the repository at this point in the history
  • Loading branch information
alexislefebvre committed Jan 7, 2021
1 parent 07e1a9f commit 11bcfbf
Show file tree
Hide file tree
Showing 15 changed files with 403 additions and 6 deletions.
20 changes: 20 additions & 0 deletions src/Event/FixtureEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

declare(strict_types=1);

/*
* This file is part of the Liip/TestFixturesBundle
*
* (c) Lukas Kahwe Smith <smith@pooteeweet.org>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace Liip\TestFixturesBundle\Event;

use Symfony\Contracts\EventDispatcher\Event;

class FixtureEvent extends Event
{
}
28 changes: 28 additions & 0 deletions src/Event/PostFixtureBackupRestoreEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

/*
* This file is part of the Liip/TestFixturesBundle
*
* (c) Lukas Kahwe Smith <smith@pooteeweet.org>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace Liip\TestFixturesBundle\Event;

class PostFixtureBackupRestoreEvent extends FixtureEvent
{
private $backupFilePath;

public function __construct(string $backupFilePath) {
$this->backupFilePath = $backupFilePath;
}

public function getBackupFilePath(): string
{
return $this->backupFilePath;
}
}
49 changes: 49 additions & 0 deletions src/Event/PreFixtureBackupRestoreEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

declare(strict_types=1);

/*
* This file is part of the Liip/TestFixturesBundle
*
* (c) Lukas Kahwe Smith <smith@pooteeweet.org>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace Liip\TestFixturesBundle\Event;

use Doctrine\Common\DataFixtures\ReferenceRepository;
use Doctrine\Persistence\ObjectManager;

class PreFixtureBackupRestoreEvent extends FixtureEvent
{
private $manager;
private $repository;
private $backupFilePath;

public function __construct(
ObjectManager $manager,
ReferenceRepository $executor,
string $backupFilePath
) {
$this->manager = $manager;
$this->repository = $executor;
$this->backupFilePath = $backupFilePath;
}

public function getManager(): ObjectManager
{
return $this->manager;
}

public function getRepository(): ReferenceRepository
{
return $this->repository;
}

public function getBackupFilePath(): string
{
return $this->backupFilePath;
}
}
49 changes: 49 additions & 0 deletions src/Event/ReferenceSaveEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

declare(strict_types=1);

/*
* This file is part of the Liip/TestFixturesBundle
*
* (c) Lukas Kahwe Smith <smith@pooteeweet.org>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace Liip\TestFixturesBundle\Event;

use Doctrine\Common\DataFixtures\Executor\AbstractExecutor;
use Doctrine\Persistence\ObjectManager;

class ReferenceSaveEvent extends FixtureEvent
{
private $manager;
private $executor;
private $backupFilePath;

public function __construct(
ObjectManager $manager,
AbstractExecutor $executor,
string $backupFilePath
) {
$this->manager = $manager;
$this->executor = $executor;
$this->backupFilePath = $backupFilePath;
}

public function getManager(): ObjectManager
{
return $this->manager;
}

public function getExecutor(): AbstractExecutor
{
return $this->executor;
}

public function getBackupFilePath(): string
{
return $this->backupFilePath;
}
}
37 changes: 37 additions & 0 deletions src/LiipTestFixturesEvents.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

declare(strict_types=1);

/*
* This file is part of the Liip/TestFixturesBundle
*
* (c) Lukas Kahwe Smith <smith@pooteeweet.org>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace Liip\TestFixturesBundle;

use Liip\TestFixturesBundle\Event\FixtureEvent;
use Liip\TestFixturesBundle\Event\PostFixtureBackupRestoreEvent;
use Liip\TestFixturesBundle\Event\PreFixtureBackupRestoreEvent;
use Liip\TestFixturesBundle\Event\ReferenceSaveEvent;

final class LiipTestFixturesEvents
{
/** @see PreFixtureBackupRestoreEvent */
const PRE_FIXTURE_BACKUP_RESTORE = 'liip_test_fixtures.pre_fixture_backup_restore';

/** @see FixtureEvent */
const POST_FIXTURE_SETUP = 'liip_test_fixtures.post_fixture_setup';

/** @see PostFixtureBackupRestoreEvent */
const POST_FIXTURE_BACKUP_RESTORE = 'liip_test_fixtures.post_fixture_backup_restore';

/** @see ReferenceSaveEvent */
const PRE_REFERENCE_SAVE = 'liip_test_fixtures.pre_reference_save';

/** @see ReferenceSaveEvent */
const POST_REFERENCE_SAVE = 'liip_test_fixtures.post_reference_save';
}
15 changes: 11 additions & 4 deletions src/Services/DatabaseTools/AbstractDatabaseTool.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,17 @@

namespace Liip\TestFixturesBundle\Services\DatabaseTools;

use BadMethodCallException;
use Doctrine\Common\DataFixtures\Executor\AbstractExecutor;
use Doctrine\Persistence\ManagerRegistry;
use Doctrine\Persistence\ObjectManager;
use Doctrine\DBAL\Connection;
use InvalidArgumentException;
use Liip\TestFixturesBundle\Services\DatabaseBackup\DatabaseBackupInterface;
use Liip\TestFixturesBundle\Services\FixturesLoaderFactory;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;

/**
* @author Aleksey Tupichenkov <alekseytupichenkov@gmail.com>
Expand All @@ -30,6 +33,9 @@ abstract class AbstractDatabaseTool

protected $container;

/** @var EventDispatcherInterface */
protected $eventDispatcher;

protected $fixturesLoaderFactory;

/**
Expand Down Expand Up @@ -77,6 +83,7 @@ abstract class AbstractDatabaseTool
public function __construct(ContainerInterface $container, FixturesLoaderFactory $fixturesLoaderFactory)
{
$this->container = $container;
$this->eventDispatcher = $container->get('event_dispatcher');
$this->fixturesLoaderFactory = $fixturesLoaderFactory;
}

Expand Down Expand Up @@ -137,13 +144,13 @@ protected function getBackupService(): ?DatabaseBackupInterface
abstract public function loadFixtures(array $classNames = [], bool $append = false): AbstractExecutor;

/**
* @throws \BadMethodCallException
* @throws BadMethodCallException
*/
public function loadAliceFixture(array $paths = [], bool $append = false): array
{
$persisterLoaderServiceName = 'fidry_alice_data_fixtures.loader.doctrine';
if (!$this->container->has($persisterLoaderServiceName)) {
throw new \BadMethodCallException('theofidry/alice-data-fixtures must be installed to use this method.');
throw new BadMethodCallException('theofidry/alice-data-fixtures must be installed to use this method.');
}

if (false === $append) {
Expand All @@ -163,7 +170,7 @@ protected function cleanDatabase(): void
/**
* Locate fixture files.
*
* @throws \InvalidArgumentException if a wrong path is given outside a bundle
* @throws InvalidArgumentException if a wrong path is given outside a bundle
*/
protected function locateResources(array $paths): array
{
Expand All @@ -174,7 +181,7 @@ protected function locateResources(array $paths): array
foreach ($paths as $path) {
if ('@' !== $path[0]) {
if (!file_exists($path)) {
throw new \InvalidArgumentException(sprintf('Unable to find file "%s".', $path));
throw new InvalidArgumentException(sprintf('Unable to find file "%s".', $path));
}
$files[] = $path;

Expand Down
15 changes: 15 additions & 0 deletions src/Services/DatabaseTools/MongoDBDatabaseTool.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
use Doctrine\Common\DataFixtures\Executor\MongoDBExecutor;
use Doctrine\Common\DataFixtures\ProxyReferenceRepository;
use Doctrine\Common\DataFixtures\Purger\MongoDBPurger;
use Liip\TestFixturesBundle\Event\PostFixtureBackupRestoreEvent;
use Liip\TestFixturesBundle\Event\PreFixtureBackupRestoreEvent;
use Liip\TestFixturesBundle\Event\ReferenceSaveEvent;
use Liip\TestFixturesBundle\LiipTestFixturesEvents;

/**
* @author Aleksey Tupichenkov <alekseytupichenkov@gmail.com>
Expand Down Expand Up @@ -67,10 +71,16 @@ public function loadFixtures(array $classNames = [], bool $append = false): Abst
$this->om->flush();
$this->om->clear();

$event = new PreFixtureBackupRestoreEvent($this->om, $referenceRepository, $backupService->getBackupFilePath());
$this->eventDispatcher->dispatch($event, LiipTestFixturesEvents::PRE_FIXTURE_BACKUP_RESTORE);
$this->testCase->preFixtureBackupRestore($this->om, $referenceRepository, $backupService->getBackupFilePath());

$executor = $this->getExecutor($this->getPurger());
$executor->setReferenceRepository($referenceRepository);
$backupService->restore($executor);

$event = new PostFixtureBackupRestoreEvent($backupService->getBackupFilePath());
$this->eventDispatcher->dispatch($event, LiipTestFixturesEvents::POST_FIXTURE_BACKUP_RESTORE);
$this->testCase->postFixtureBackupRestore($backupService->getBackupFilePath());

return $executor;
Expand All @@ -87,8 +97,13 @@ public function loadFixtures(array $classNames = [], bool $append = false): Abst
$executor->execute($loader->getFixtures(), true);

if ($backupService) {
$event = new ReferenceSaveEvent($this->om, $executor, $backupService->getBackupFilePath());
$this->eventDispatcher->dispatch($event, LiipTestFixturesEvents::PRE_REFERENCE_SAVE);
$this->testCase->preReferenceSave($this->om, $executor, $backupService->getBackupFilePath());

$backupService->backup($executor);

$this->eventDispatcher->dispatch($event, LiipTestFixturesEvents::POST_REFERENCE_SAVE);
$this->testCase->postReferenceSave($this->om, $executor, $backupService->getBackupFilePath());
}

Expand Down
18 changes: 18 additions & 0 deletions src/Services/DatabaseTools/ORMDatabaseTool.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@
use Doctrine\DBAL\Platforms\MySqlPlatform;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\Tools\SchemaTool;
use Liip\TestFixturesBundle\Event\FixtureEvent;
use Liip\TestFixturesBundle\Event\PostFixtureBackupRestoreEvent;
use Liip\TestFixturesBundle\Event\PreFixtureBackupRestoreEvent;
use Liip\TestFixturesBundle\Event\ReferenceSaveEvent;
use Liip\TestFixturesBundle\LiipTestFixturesEvents;

/**
* @author Aleksey Tupichenkov <alekseytupichenkov@gmail.com>
Expand Down Expand Up @@ -115,10 +120,16 @@ public function loadFixtures(array $classNames = [], bool $append = false): Abst
$this->om->flush();
$this->om->clear();

$event = new PreFixtureBackupRestoreEvent($this->om, $referenceRepository, $backupService->getBackupFilePath());
$this->eventDispatcher->dispatch($event, LiipTestFixturesEvents::PRE_FIXTURE_BACKUP_RESTORE);
$this->testCase->preFixtureBackupRestore($this->om, $referenceRepository, $backupService->getBackupFilePath());

$executor = $this->getExecutor($this->getPurger());
$executor->setReferenceRepository($referenceRepository);
$backupService->restore($executor, $this->excludedDoctrineTables);

$event = new PostFixtureBackupRestoreEvent($backupService->getBackupFilePath());
$this->eventDispatcher->dispatch($event, LiipTestFixturesEvents::POST_FIXTURE_BACKUP_RESTORE);
$this->testCase->postFixtureBackupRestore($backupService->getBackupFilePath());

return $executor;
Expand All @@ -141,6 +152,8 @@ public function loadFixtures(array $classNames = [], bool $append = false): Abst
}
}

$event = new FixtureEvent();
$this->eventDispatcher->dispatch($event, LiipTestFixturesEvents::POST_FIXTURE_SETUP);
$this->testCase->postFixtureSetup();

$executor = $this->getExecutor($this->getPurger());
Expand All @@ -155,8 +168,13 @@ public function loadFixtures(array $classNames = [], bool $append = false): Abst
$executor->execute($loader->getFixtures(), true);

if ($backupService) {
$event = new ReferenceSaveEvent($this->om, $executor, $backupService->getBackupFilePath());
$this->eventDispatcher->dispatch($event, LiipTestFixturesEvents::PRE_REFERENCE_SAVE);
$this->testCase->preReferenceSave($this->om, $executor, $backupService->getBackupFilePath());

$backupService->backup($executor);

$this->eventDispatcher->dispatch($event, LiipTestFixturesEvents::POST_REFERENCE_SAVE);
$this->testCase->postReferenceSave($this->om, $executor, $backupService->getBackupFilePath());
}

Expand Down
Loading

0 comments on commit 11bcfbf

Please sign in to comment.