Skip to content

Commit

Permalink
#48056, edit bundle
Browse files Browse the repository at this point in the history
  • Loading branch information
Dumazeau committed Jul 15, 2021
1 parent 14bf9dc commit 64faaa6
Show file tree
Hide file tree
Showing 12 changed files with 465 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public function __construct(TermsVersion $termsVersion)
$terms = $termsVersion->getTerms();

parent::__construct(
\sprintf('The terms version %d of terms %s is already enabled.', $termsVersion->getVersion(), $terms->getSlug())
\sprintf('Version %d of terms %s is already enabled.', $termsVersion->getVersion(), $terms->getSlug())
);

$this->termsVersion = $termsVersion;
Expand Down
25 changes: 25 additions & 0 deletions tests/Domain/Event/TermsVersionDeletedEventTest.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\TermsVersion;
use RichId\TermsModuleBundle\Domain\Event\TermsVersionDeletedEvent;

/**
* @covers \RichId\TermsModuleBundle\Domain\Event\TermsVersionDeletedEvent
* @TestConfig("kernel")
*/
final class TermsVersionDeletedEventTest extends TestCase
{
public function testEvent(): void
{
$termsVersion = new TermsVersion();
$event = new TermsVersionDeletedEvent($termsVersion);

$this->assertSame($termsVersion, $event->getTermsVersion());
}
}
28 changes: 28 additions & 0 deletions tests/Domain/Exception/AlreadyEnabledTermsVersionExceptionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace RichId\TermsModuleBundle\Tests\Domain\Exception;

use RichCongress\TestFramework\TestConfiguration\Annotation\TestConfig;
use RichCongress\TestSuite\TestCase\TestCase;
use RichId\TermsModuleBundle\Domain\Entity\TermsVersion;
use RichId\TermsModuleBundle\Domain\Exception\AlreadyEnabledTermsVersionException;
use RichId\TermsModuleBundle\Domain\Exception\TermsModuleException;

/**
* @covers \RichId\TermsModuleBundle\Domain\Exception\AlreadyEnabledTermsVersionException
* @TestConfig("fixtures")
*/
final class AlreadyEnabledTermsVersionExceptionTest extends TestCase
{
public function testException(): void
{
$termsVersion = $this->getReference(TermsVersion::class, 'v1-terms-1');
$exception = new AlreadyEnabledTermsVersionException($termsVersion);

$this->assertInstanceOf(TermsModuleException::class, $exception);
$this->assertSame($termsVersion, $exception->getTermsVersion());
$this->assertSame('Version 1 of terms terms-1 is already enabled.', $exception->getMessage());
}
}
3 changes: 3 additions & 0 deletions tests/Domain/Model/TermsEditionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public function testConstruct(): void
$this->assertSame('My content', $model->getContent());
$this->assertSame($date, $model->getPublicationDate());
$this->assertSame($termsVersion, $model->getEntity());
$this->assertNull($model->needVersionActivation());
}

public function testModel(): void
Expand All @@ -46,11 +47,13 @@ public function testModel(): void
$model->setTitle('My title');
$model->setContent('My content');
$model->setPublicationDate($date);
$model->setNeedVersionActivation(true);

$this->assertTrue($model->isTermsPublished());
$this->assertSame('My title', $model->getTitle());
$this->assertSame('My content', $model->getContent());
$this->assertSame($date, $model->getPublicationDate());
$this->assertSame($termsVersion, $model->getEntity());
$this->assertTrue($model->needVersionActivation());
}
}
70 changes: 70 additions & 0 deletions tests/Domain/UseCase/ActivateTermsVersionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php

declare(strict_types=1);

namespace RichId\TermsModuleBundle\Tests\Domain\UseCase;

use RichCongress\TestFramework\TestConfiguration\Annotation\TestConfig;
use RichCongress\TestSuite\TestCase\TestCase;
use RichId\TermsModuleBundle\Domain\Entity\Terms;
use RichId\TermsModuleBundle\Domain\Entity\TermsVersion;
use RichId\TermsModuleBundle\Domain\Event\TermsVersionEnabledEvent;
use RichId\TermsModuleBundle\Domain\Exception\AlreadyEnabledTermsVersionException;
use RichId\TermsModuleBundle\Domain\UseCase\ActivateTermsVersion;
use RichId\TermsModuleBundle\Tests\Resources\Stubs\EntityManagerStub;
use RichId\TermsModuleBundle\Tests\Resources\Stubs\EventDispatcherStub;

/**
* @covers \RichId\TermsModuleBundle\Domain\UseCase\ActivateTermsVersion
* @TestConfig("fixtures")
*/
final class ActivateTermsVersionTest extends TestCase
{
/** @var ActivateTermsVersion */
public $useCase;

/** @var EntityManagerStub */
public $entityManagerStub;

/** @var EventDispatcherStub */
public $eventDispatcherStub;

public function testUseCaseTermsVersionAlreadyEnabled(): void
{
$this->expectException(AlreadyEnabledTermsVersionException::class);
$this->expectExceptionMessage('Version 42 of terms my_terms is already enabled.');

$terms = new Terms();
$terms->setSlug('my_terms');

$termsVersion = new TermsVersion();
$termsVersion->setTerms($terms);
$termsVersion->setVersion(42);
$termsVersion->enable();

($this->useCase)($termsVersion);
}

public function testUseCase(): void
{
$terms = new Terms();
$terms->setSlug('my_terms');

$termsVersion = new TermsVersion();
$termsVersion->setTerms($terms);
$termsVersion->setVersion(42);

($this->useCase)($termsVersion);

$this->assertTrue($termsVersion->isEnabled());
$this->assertNotNull($termsVersion->getPublicationDate());

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

$event = $this->eventDispatcherStub->getEvents()[0];

$this->assertInstanceOf(TermsVersionEnabledEvent::class, $event);
$this->assertSame($termsVersion, $event->getTermsVersion());
}
}
108 changes: 108 additions & 0 deletions tests/Domain/UseCase/CreateTermsVersionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
<?php

declare(strict_types=1);

namespace RichId\TermsModuleBundle\Tests\Domain\UseCase;

use RichCongress\TestFramework\TestConfiguration\Annotation\TestConfig;
use RichCongress\TestSuite\TestCase\TestCase;
use RichId\TermsModuleBundle\Domain\Entity\Terms;
use RichId\TermsModuleBundle\Domain\Entity\TermsVersion;
use RichId\TermsModuleBundle\Domain\Event\TermsVersionCreatedEvent;
use RichId\TermsModuleBundle\Domain\Exception\CannotAddVersionToTermsException;
use RichId\TermsModuleBundle\Domain\UseCase\CreateTermsVersion;
use RichId\TermsModuleBundle\Tests\Resources\Stubs\EntityManagerStub;
use RichId\TermsModuleBundle\Tests\Resources\Stubs\EventDispatcherStub;

/**
* @covers \RichId\TermsModuleBundle\Domain\UseCase\CreateTermsVersion
* @TestConfig("fixtures")
*/
final class CreateTermsVersionTest extends TestCase
{
/** @var CreateTermsVersion */
public $useCase;

/** @var EntityManagerStub */
public $entityManagerStub;

/** @var EventDispatcherStub */
public $eventDispatcherStub;

public function testUseCaseTermsWithoutVersion(): void
{
$this->expectException(CannotAddVersionToTermsException::class);
$this->expectExceptionMessage('Cannot add new version to the terms my_terms.');

$terms = new Terms();
$terms->setSlug('my_terms');

$termsVersion = new TermsVersion();
$termsVersion->setTerms($terms);
$termsVersion->setVersion(42);

($this->useCase)($termsVersion);
}

public function testUseCaseLastVersionNotPublished(): void
{
$this->expectException(CannotAddVersionToTermsException::class);
$this->expectExceptionMessage('Cannot add new version to the terms my_terms.');

$terms = new Terms();
$terms->setSlug('my_terms');

$termsVersion = new TermsVersion();
$termsVersion->setVersion(42);
$termsVersion->setTerms($terms);
$terms->addVersion($termsVersion);

($this->useCase)($termsVersion);
}

public function testUseCase(): void
{
$terms = $this->getReference(Terms::class, '2');

$termsVersion1 = new TermsVersion();
$termsVersion1->setTitle('Title 1');
$termsVersion1->setContent('Content 1');
$termsVersion1->setPublicationDate(new \DateTime('today - 2 days'));
$termsVersion1->setVersion(42);
$termsVersion1->enable();
$termsVersion1->setTerms($terms);
$terms->addVersion($termsVersion1);

$termsVersion2 = new TermsVersion();
$termsVersion2->setTitle('Title 2');
$termsVersion2->setContent('Content 2');
$termsVersion2->setPublicationDate(new \DateTime('today - 1 days'));
$termsVersion2->setVersion(43);
$termsVersion2->enable();
$termsVersion2->setTerms($terms);
$terms->addVersion($termsVersion2);

$this->getManager()->persist($termsVersion1);
$this->getManager()->persist($termsVersion2);

($this->useCase)($termsVersion1);

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

$event = $this->eventDispatcherStub->getEvents()[0];

$this->assertInstanceOf(TermsVersionCreatedEvent::class, $event);

$newTermsVersion = $event->getTermsVersion();

$this->assertInstanceOf(TermsVersion::class, $event->getTermsVersion());
$this->assertNotNull($newTermsVersion->getId());
$this->assertSame('Title 1', $newTermsVersion->getTitle());
$this->assertSame('Content 1', $newTermsVersion->getCOntent());
$this->assertSame(44, $newTermsVersion->getVersion());
$this->assertSame($terms, $newTermsVersion->getTerms());
$this->assertNull($newTermsVersion->getPublicationDate());
$this->assertFalse($newTermsVersion->isEnabled());
}
}
Loading

0 comments on commit 64faaa6

Please sign in to comment.