Skip to content

Commit

Permalink
Add directory purger before fixtures run
Browse files Browse the repository at this point in the history
  • Loading branch information
AdamKasp committed Jun 20, 2022
1 parent e1c0c93 commit 3d0e9b0
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 0 deletions.
34 changes: 34 additions & 0 deletions spec/Fixture/Listener/DirectoryPurgerListenerSpec.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

/*
* This file is part of the Sylius package.
*
* (c) Paweł Jędrzejewski
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace spec\Sylius\InvoicingPlugin\Fixture\Listener;

use PhpSpec\ObjectBehavior;
use Sylius\Bundle\FixturesBundle\Listener\SuiteEvent;
use Sylius\Bundle\FixturesBundle\Suite\SuiteInterface;
use Symfony\Component\Filesystem\Filesystem;

final class DirectoryPurgerListenerSpec extends ObjectBehavior
{
public function let(Filesystem $filesystem): void
{
$this->beConstructedWith($filesystem, 'path/to/invoices/');
}

public function it_removes_invoices_before_fixture_suite(Filesystem $filesystem, SuiteInterface $suite): void
{
$filesystem->remove('path/to/invoices/')->shouldBeCalled();

$this->beforeSuite(new SuiteEvent($suite->getWrappedObject()), []);
}
}
36 changes: 36 additions & 0 deletions src/Fixture/Listener/InvoicesPurgerListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

/*
* This file is part of the Sylius package.
*
* (c) Paweł Jędrzejewski
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Sylius\InvoicingPlugin\Fixture\Listener;

use Sylius\Bundle\FixturesBundle\Listener\AbstractListener;
use Sylius\Bundle\FixturesBundle\Listener\BeforeSuiteListenerInterface;
use Sylius\Bundle\FixturesBundle\Listener\SuiteEvent;
use Symfony\Component\Filesystem\Filesystem;

final class InvoicesPurgerListener extends AbstractListener implements BeforeSuiteListenerInterface
{
public function __construct(private Filesystem $filesystem, private string $invoicesPath)
{
}

public function beforeSuite(SuiteEvent $suiteEvent, array $options): void
{
$this->filesystem->remove($this->invoicesPath);
}

public function getName(): string
{
return 'invoices_directory_purger';
}
}
6 changes: 6 additions & 0 deletions src/Resources/config/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -169,3 +169,9 @@ knp_gaufrette:
filesystems:
sylius_invoicing_invoice:
adapter: "%sylius_invoicing.filesystem_adapter.invoice%"

sylius_fixtures:
suites:
default:
listeners:
invoices_directory_purger: ~
6 changes: 6 additions & 0 deletions src/Resources/config/services/fixtures.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,11 @@
<argument id="sylius_invoicing_plugin.fixture.example_factory.invoicing_plugin_shop_billing_data" type="service"/>
<tag name="sylius_fixtures.fixture"/>
</service>

<service id="Sylius\InvoicingPlugin\Fixture\Listener\InvoicesPurgerListener">
<argument type="service" id="filesystem" />
<argument>%sylius_invoicing.invoice_save_path%</argument>
<tag name="sylius_fixtures.listener" />
</service>
</services>
</container>

0 comments on commit 3d0e9b0

Please sign in to comment.