PHP code example of patchlevel / event-sourcing-phpunit
1. Go to this page and download the library: Download patchlevel/event-sourcing-phpunit library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
patchlevel / event-sourcing-phpunit example snippets
final class ProfileTest extends AggregateRootTestCase
{
protected function aggregateClass(): string
{
return Profile::class;
}
}
final class ProfileTest extends AggregateRootTestCase
{
// protected function aggregateClass(): string;
public function testBehaviour(): void
{
$this
->given(
new ProfileCreated(
ProfileId::fromString('1'),
Email::fromString('[email protected]'),
),
)
->when(static fn (Profile $profile) => $profile->visitProfile(ProfileId::fromString('2')))
->then(new ProfileVisited(ProfileId::fromString('2')));
}
}
final class ProfileTest extends AggregateRootTestCase
{
// protected function aggregateClass(): string;
public function testBehaviour(): void
{
$this
->given(
new ProfileCreated(
ProfileId::fromString('1'),
Email::fromString('[email protected]'),
),
new ProfileVisited(ProfileId::fromString('2')),
)
->when(
static function (Profile $profile) {
$profile->visitProfile(ProfileId::fromString('3'));
$profile->visitProfile(ProfileId::fromString('4'));
}
)
->then(
new ProfileVisited(ProfileId::fromString('3')),
new ProfileVisited(ProfileId::fromString('4')),
);
}
}
final class ProfileTest extends AggregateRootTestCase
{
// protected function aggregateClass(): string;
public function testBehaviour(): void
{
$this
->when(static fn () => Profile::createProfile(ProfileId::fromString('1'), Email::fromString('[email protected]')))
->then(new ProfileCreated(ProfileId::fromString('1'), Email::fromString('[email protected]')));
}
}
final class ProfileTest extends AggregateRootTestCase
{
// protected function aggregateClass(): string;
public function testBehaviour(): void
{
$this
->given(
new ProfileCreated(
ProfileId::fromString('1'),
Email::fromString('[email protected]'),
),
)
->when(static fn (Profile $profile) => $profile->throwException())
->expectsException(ProfileError::class)
->expectsExceptionMessage('throwing so that you can catch it!');
}
}
final class ProfileTest extends AggregateRootTestCase
{
// protected function aggregateClass(): string;
public function testBehaviour(): void
{
$this
->when(new CreateProfile(ProfileId::fromString('1'), Email::fromString('[email protected]')))
->then(new ProfileCreated(ProfileId::fromString('1'), Email::fromString('[email protected]')));
}
}
final class ProfileTest extends AggregateRootTestCase
{
// protected function aggregateClass(): string;
public function testBehaviour(): void
{
$this
->given(
new ProfileCreated(
ProfileId::fromString('1'),
Email::fromString('[email protected]'),
),
)
->when(new VisitProfile(ProfileId::fromString('2')), 'Extra Parameter / Dependency')
->then(new ProfileVisited(ProfileId::fromString('2'), 'Extra Parameter / Dependency'));
}
}
use Patchlevel\EventSourcing\Attribute\Setup;
use Patchlevel\EventSourcing\Attribute\Subscribe;
use Patchlevel\EventSourcing\Attribute\Subscriber;
use Patchlevel\EventSourcing\Attribute\Teardown;
#[Subscriber('profile_subscriber', RunMode::FromBeginning)]
final class ProfileSubscriber
{
public int $called = 0;
#[Subscribe(ProfileCreated::class)]
public function run(): void
{
$this->called++;
}
#[Setup]
public function setup(): void
{
$this->called++;
}
#[Teardown]
public function teardown(): void
{
$this->called++;
}
}
use Patchlevel\EventSourcing\Attribute\Subscriber;
use Patchlevel\EventSourcing\Subscription\RunMode;
use Patchlevel\EventSourcing\PhpUnit\Test\SubscriberUtilities;
final class ProfileSubscriberTest extends TestCase
{
use SubscriberUtilities;
public function testProfileCreated(): void
{
$subscriber = new ProfileSubscriber(/* inject deps, if needed */);
$util = new SubscriberUtilities($subscriber);
$util->executeSetup();
$util->executeRun(
new ProfileCreated(
ProfileId::fromString('1'),
Email::fromString('[email protected]'),
)
);
$util->executeTeardown();
self::assertSame(3, $subscriber->count);
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.