Skip to content

Commit

Permalink
Add new events
Browse files Browse the repository at this point in the history
  • Loading branch information
Dumazeau committed Jul 22, 2021
1 parent 368aa72 commit af64040
Show file tree
Hide file tree
Showing 6 changed files with 125 additions and 3 deletions.
23 changes: 23 additions & 0 deletions src/Domain/Event/TermsPublishedEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

declare(strict_types=1);

namespace RichId\TermsModuleBundle\Domain\Event;

use RichId\TermsModuleBundle\Domain\Entity\Terms;

final class TermsPublishedEvent extends TermsEvent
{
/** @var Terms */
private $terms;

public function __construct(Terms $terms)
{
$this->terms = $terms;
}

public function getTerms(): Terms
{
return $this->terms;
}
}
23 changes: 23 additions & 0 deletions src/Domain/Event/TermsUnpublishedEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

declare(strict_types=1);

namespace RichId\TermsModuleBundle\Domain\Event;

use RichId\TermsModuleBundle\Domain\Entity\Terms;

final class TermsUnpublishedEvent extends TermsEvent
{
/** @var Terms */
private $terms;

public function __construct(Terms $terms)
{
$this->terms = $terms;
}

public function getTerms(): Terms
{
return $this->terms;
}
}
15 changes: 15 additions & 0 deletions src/Domain/UseCase/EditTerms.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace RichId\TermsModuleBundle\Domain\UseCase;

use RichId\TermsModuleBundle\Domain\Event\TermsPublishedEvent;
use RichId\TermsModuleBundle\Domain\Event\TermsUnpublishedEvent;
use RichId\TermsModuleBundle\Domain\Event\TermsVersionUpdatedEvent;
use RichId\TermsModuleBundle\Domain\Model\TermsEdition;
use RichId\TermsModuleBundle\Domain\Port\EntityRecoderInterface;
Expand Down Expand Up @@ -54,6 +56,7 @@ public function __invoke(TermsEdition $termsEdition): void

$termsVersion = $termsEdition->getEntity();
$terms = $termsVersion->getTerms();
$termsPublished = $terms->isPublished();

($this->termsVersionUpdater)($termsVersion, $termsEdition);
($this->termsUpdater)($terms, $termsEdition);
Expand All @@ -68,5 +71,17 @@ public function __invoke(TermsEdition $termsEdition): void
$this->eventDispatcher->dispatchTermsEvent(
new TermsVersionUpdatedEvent($termsVersion)
);

if (!$termsPublished && $terms->isPublished()) {
$this->eventDispatcher->dispatchTermsEvent(
new TermsPublishedEvent($terms)
);
}

if ($termsPublished && !$terms->isPublished()) {
$this->eventDispatcher->dispatchTermsEvent(
new TermsUnpublishedEvent($terms)
);
}
}
}
25 changes: 25 additions & 0 deletions tests/Domain/Event/TermsPublishedEventTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

declare(strict_types=1);

namespace RichId\TermsModuleBundle\Tests\Domain\Event;

use RichCongress\TestFramework\TestConfiguration\Annotation\TestConfig;
use RichCongress\TestSuite\TestCase\TestCase;
use RichId\TermsModuleBundle\Domain\Entity\Terms;
use RichId\TermsModuleBundle\Domain\Event\TermsPublishedEvent;

/**
* @covers \RichId\TermsModuleBundle\Domain\Event\TermsPublishedEvent
* @TestConfig("kernel")
*/
final class TermsPublishedEventTest extends TestCase
{
public function testEvent(): void
{
$terms = new Terms();
$event = new TermsPublishedEvent($terms);

$this->assertSame($terms, $event->getTerms());
}
}
25 changes: 25 additions & 0 deletions tests/Domain/Event/TermsUnpublishedEventTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

declare(strict_types=1);

namespace RichId\TermsModuleBundle\Tests\Domain\Event;

use RichCongress\TestFramework\TestConfiguration\Annotation\TestConfig;
use RichCongress\TestSuite\TestCase\TestCase;
use RichId\TermsModuleBundle\Domain\Entity\Terms;
use RichId\TermsModuleBundle\Domain\Event\TermsUnpublishedEvent;

/**
* @covers \RichId\TermsModuleBundle\Domain\Event\TermsUnpublishedEvent
* @TestConfig("kernel")
*/
final class TermsUnpublishedEventTest extends TestCase
{
public function testEvent(): void
{
$terms = new Terms();
$event = new TermsUnpublishedEvent($terms);

$this->assertSame($terms, $event->getTerms());
}
}
17 changes: 14 additions & 3 deletions tests/Domain/UseCase/EditTermsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
use RichCongress\TestSuite\TestCase\TestCase;
use RichId\TermsModuleBundle\Domain\Entity\Terms;
use RichId\TermsModuleBundle\Domain\Entity\TermsVersion;
use RichId\TermsModuleBundle\Domain\Event\TermsPublishedEvent;
use RichId\TermsModuleBundle\Domain\Event\TermsUnpublishedEvent;
use RichId\TermsModuleBundle\Domain\Event\TermsVersionEnabledEvent;
use RichId\TermsModuleBundle\Domain\Event\TermsVersionUpdatedEvent;
use RichId\TermsModuleBundle\Domain\Exception\InvalidTermsEditionException;
Expand Down Expand Up @@ -88,10 +90,13 @@ public function testUseCaseWithChanges(): void
$this->assertFalse($termsVersion->getTerms()->isPublished());

$this->assertCount(2, $this->entityManagerStub->getPersistedEntities());
$this->assertCount(1, $this->eventDispatcherStub->getEvents());
$this->assertCount(2, $this->eventDispatcherStub->getEvents());

$event = $this->eventDispatcherStub->getEvents()[0];
$this->assertInstanceOf(TermsVersionUpdatedEvent::class, $event);

$event = $this->eventDispatcherStub->getEvents()[1];
$this->assertInstanceOf(TermsUnpublishedEvent::class, $event);
}

public function testUseCaseWithChangesAndActivation(): void
Expand All @@ -113,13 +118,16 @@ public function testUseCaseWithChangesAndActivation(): void
$this->assertFalse($termsVersion->getTerms()->isPublished());

$this->assertCount(4, $this->entityManagerStub->getPersistedEntities());
$this->assertCount(2, $this->eventDispatcherStub->getEvents());
$this->assertCount(3, $this->eventDispatcherStub->getEvents());

$event = $this->eventDispatcherStub->getEvents()[0];
$this->assertInstanceOf(TermsVersionEnabledEvent::class, $event);

$event = $this->eventDispatcherStub->getEvents()[1];
$this->assertInstanceOf(TermsVersionUpdatedEvent::class, $event);

$event = $this->eventDispatcherStub->getEvents()[2];
$this->assertInstanceOf(TermsUnpublishedEvent::class, $event);
}

public function testUseCaseFirstTermsVersionAndTermsPublished(): void
Expand All @@ -145,12 +153,15 @@ public function testUseCaseFirstTermsVersionAndTermsPublished(): void
$this->assertTrue($termsVersion->getTerms()->isPublished());

$this->assertCount(4, $this->entityManagerStub->getPersistedEntities());
$this->assertCount(2, $this->eventDispatcherStub->getEvents());
$this->assertCount(3, $this->eventDispatcherStub->getEvents());

$event = $this->eventDispatcherStub->getEvents()[0];
$this->assertInstanceOf(TermsVersionUpdatedEvent::class, $event);

$event = $this->eventDispatcherStub->getEvents()[1];
$this->assertInstanceOf(TermsVersionEnabledEvent::class, $event);

$event = $this->eventDispatcherStub->getEvents()[2];
$this->assertInstanceOf(TermsPublishedEvent::class, $event);
}
}

0 comments on commit af64040

Please sign in to comment.