mirror of
https://github.com/php-flasher/php-flasher.git
synced 2026-03-31 15:07:47 +01:00
add tests for FlasherAssert and Constraint classes
This commit is contained in:
@@ -0,0 +1,210 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Flasher\Tests\Prime\Test\Constraint;
|
||||
|
||||
use Flasher\Prime\EventDispatcher\Event\NotificationEvents;
|
||||
use Flasher\Prime\Notification\Envelope;
|
||||
use Flasher\Prime\Notification\Notification as NotificationEntity;
|
||||
use Flasher\Prime\Test\Constraint\Notification;
|
||||
use PHPUnit\Framework\Constraint\Constraint;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
final class NotificationConstraintTest extends TestCase
|
||||
{
|
||||
public function testItIsInstanceOfConstraint(): void
|
||||
{
|
||||
$constraint = new Notification('success');
|
||||
$this->assertInstanceOf(Constraint::class, $constraint);
|
||||
}
|
||||
|
||||
public function testToStringWithTypeOnly(): void
|
||||
{
|
||||
$constraint = new Notification('success');
|
||||
|
||||
$this->assertStringContainsString('type: "success"', $constraint->toString());
|
||||
}
|
||||
|
||||
public function testToStringWithMessage(): void
|
||||
{
|
||||
$constraint = new Notification('success', 'Test message');
|
||||
|
||||
$result = $constraint->toString();
|
||||
|
||||
$this->assertStringContainsString('type: "success"', $result);
|
||||
$this->assertStringContainsString('message: "Test message"', $result);
|
||||
}
|
||||
|
||||
public function testToStringWithTitle(): void
|
||||
{
|
||||
$constraint = new Notification('success', null, [], 'Test Title');
|
||||
|
||||
$result = $constraint->toString();
|
||||
|
||||
$this->assertStringContainsString('type: "success"', $result);
|
||||
$this->assertStringContainsString('title: "Test Title"', $result);
|
||||
}
|
||||
|
||||
public function testToStringWithOptions(): void
|
||||
{
|
||||
$constraint = new Notification('success', null, ['timeout' => 5000]);
|
||||
|
||||
$result = $constraint->toString();
|
||||
|
||||
$this->assertStringContainsString('type: "success"', $result);
|
||||
$this->assertStringContainsString('timeout', $result);
|
||||
}
|
||||
|
||||
public function testMatchesWithMatchingType(): void
|
||||
{
|
||||
$constraint = new Notification('success');
|
||||
|
||||
$notification = new NotificationEntity();
|
||||
$notification->setType('success');
|
||||
|
||||
$events = new NotificationEvents();
|
||||
$events->addEnvelope(new Envelope($notification));
|
||||
|
||||
$result = $constraint->evaluate($events, '', true);
|
||||
|
||||
$this->assertTrue($result);
|
||||
}
|
||||
|
||||
public function testMatchesWithAllPropertiesMatching(): void
|
||||
{
|
||||
$constraint = new Notification('success', 'Test message', ['timeout' => 5000], 'Test Title');
|
||||
|
||||
$notification = new NotificationEntity();
|
||||
$notification->setType('success');
|
||||
$notification->setMessage('Test message');
|
||||
$notification->setOptions(['timeout' => 5000, 'position' => 'top']);
|
||||
$notification->setTitle('Test Title');
|
||||
|
||||
$events = new NotificationEvents();
|
||||
$events->addEnvelope(new Envelope($notification));
|
||||
|
||||
$result = $constraint->evaluate($events, '', true);
|
||||
|
||||
$this->assertTrue($result);
|
||||
}
|
||||
|
||||
public function testMatchesWithNonMatchingType(): void
|
||||
{
|
||||
$constraint = new Notification('success');
|
||||
|
||||
$notification = new NotificationEntity();
|
||||
$notification->setType('error');
|
||||
|
||||
$events = new NotificationEvents();
|
||||
$events->addEnvelope(new Envelope($notification));
|
||||
|
||||
$result = $constraint->evaluate($events, '', true);
|
||||
|
||||
$this->assertFalse($result);
|
||||
}
|
||||
|
||||
public function testMatchesWithNonMatchingMessage(): void
|
||||
{
|
||||
$constraint = new Notification('success', 'Expected message');
|
||||
|
||||
$notification = new NotificationEntity();
|
||||
$notification->setType('success');
|
||||
$notification->setMessage('Different message');
|
||||
|
||||
$events = new NotificationEvents();
|
||||
$events->addEnvelope(new Envelope($notification));
|
||||
|
||||
$result = $constraint->evaluate($events, '', true);
|
||||
|
||||
$this->assertFalse($result);
|
||||
}
|
||||
|
||||
public function testMatchesWithNonMatchingTitle(): void
|
||||
{
|
||||
$constraint = new Notification('success', null, [], 'Expected Title');
|
||||
|
||||
$notification = new NotificationEntity();
|
||||
$notification->setType('success');
|
||||
$notification->setTitle('Different Title');
|
||||
|
||||
$events = new NotificationEvents();
|
||||
$events->addEnvelope(new Envelope($notification));
|
||||
|
||||
$result = $constraint->evaluate($events, '', true);
|
||||
|
||||
$this->assertFalse($result);
|
||||
}
|
||||
|
||||
public function testMatchesWithNonMatchingOptions(): void
|
||||
{
|
||||
$constraint = new Notification('success', null, ['timeout' => 5000]);
|
||||
|
||||
$notification = new NotificationEntity();
|
||||
$notification->setType('success');
|
||||
$notification->setOptions(['timeout' => 3000]);
|
||||
|
||||
$events = new NotificationEvents();
|
||||
$events->addEnvelope(new Envelope($notification));
|
||||
|
||||
$result = $constraint->evaluate($events, '', true);
|
||||
|
||||
$this->assertFalse($result);
|
||||
}
|
||||
|
||||
public function testMatchesWithPartialOptionsMatching(): void
|
||||
{
|
||||
$constraint = new Notification('success', null, ['timeout' => 5000]);
|
||||
|
||||
$notification = new NotificationEntity();
|
||||
$notification->setType('success');
|
||||
$notification->setOptions(['timeout' => 5000, 'position' => 'top']);
|
||||
|
||||
$events = new NotificationEvents();
|
||||
$events->addEnvelope(new Envelope($notification));
|
||||
|
||||
$result = $constraint->evaluate($events, '', true);
|
||||
|
||||
$this->assertTrue($result);
|
||||
}
|
||||
|
||||
public function testMatchesWithEmptyEvents(): void
|
||||
{
|
||||
$constraint = new Notification('success');
|
||||
$events = new NotificationEvents();
|
||||
|
||||
$result = $constraint->evaluate($events, '', true);
|
||||
|
||||
$this->assertFalse($result);
|
||||
}
|
||||
|
||||
public function testMatchesWithNonNotificationEvents(): void
|
||||
{
|
||||
$constraint = new Notification('success');
|
||||
|
||||
$result = $constraint->evaluate('not an event', '', true);
|
||||
|
||||
$this->assertFalse($result);
|
||||
}
|
||||
|
||||
public function testMatchesWithMultipleNotifications(): void
|
||||
{
|
||||
$constraint = new Notification('warning', 'Warning message');
|
||||
|
||||
$notification1 = new NotificationEntity();
|
||||
$notification1->setType('success');
|
||||
$notification1->setMessage('Success message');
|
||||
|
||||
$notification2 = new NotificationEntity();
|
||||
$notification2->setType('warning');
|
||||
$notification2->setMessage('Warning message');
|
||||
|
||||
$events = new NotificationEvents();
|
||||
$events->addEnvelope(new Envelope($notification1));
|
||||
$events->addEnvelope(new Envelope($notification2));
|
||||
|
||||
$result = $constraint->evaluate($events, '', true);
|
||||
|
||||
$this->assertTrue($result);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,70 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Flasher\Tests\Prime\Test\Constraint;
|
||||
|
||||
use Flasher\Prime\EventDispatcher\Event\NotificationEvents;
|
||||
use Flasher\Prime\Notification\Envelope;
|
||||
use Flasher\Prime\Notification\Notification;
|
||||
use Flasher\Prime\Test\Constraint\NotificationCount;
|
||||
use PHPUnit\Framework\Constraint\Constraint;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
final class NotificationCountTest extends TestCase
|
||||
{
|
||||
private NotificationCount $constraint;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->constraint = new NotificationCount(2);
|
||||
}
|
||||
|
||||
public function testItIsInstanceOfConstraint(): void
|
||||
{
|
||||
$this->assertInstanceOf(Constraint::class, $this->constraint);
|
||||
}
|
||||
|
||||
public function testToString(): void
|
||||
{
|
||||
$this->assertSame('matches the expected notification count of 2.', $this->constraint->toString());
|
||||
}
|
||||
|
||||
public function testMatchesWithCorrectCount(): void
|
||||
{
|
||||
$events = new NotificationEvents();
|
||||
$events->addEnvelope(new Envelope(new Notification()));
|
||||
$events->addEnvelope(new Envelope(new Notification()));
|
||||
|
||||
$result = $this->constraint->evaluate($events, '', true);
|
||||
|
||||
$this->assertTrue($result);
|
||||
}
|
||||
|
||||
public function testMatchesWithIncorrectCount(): void
|
||||
{
|
||||
$events = new NotificationEvents();
|
||||
$events->addEnvelope(new Envelope(new Notification()));
|
||||
|
||||
$result = $this->constraint->evaluate($events, '', true);
|
||||
|
||||
$this->assertFalse($result);
|
||||
}
|
||||
|
||||
public function testMatchesWithEmptyEvents(): void
|
||||
{
|
||||
$constraint = new NotificationCount(0);
|
||||
$events = new NotificationEvents();
|
||||
|
||||
$result = $constraint->evaluate($events, '', true);
|
||||
|
||||
$this->assertTrue($result);
|
||||
}
|
||||
|
||||
public function testMatchesWithNonNotificationEvents(): void
|
||||
{
|
||||
$result = $this->constraint->evaluate('not an event', '', true);
|
||||
|
||||
$this->assertFalse($result);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,87 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Flasher\Tests\Prime\Test\Constraint;
|
||||
|
||||
use Flasher\Prime\EventDispatcher\Event\NotificationEvents;
|
||||
use Flasher\Prime\Notification\Envelope;
|
||||
use Flasher\Prime\Notification\Notification;
|
||||
use Flasher\Prime\Test\Constraint\NotificationMessage;
|
||||
use PHPUnit\Framework\Constraint\Constraint;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
final class NotificationMessageTest extends TestCase
|
||||
{
|
||||
private NotificationMessage $constraint;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->constraint = new NotificationMessage('Test message');
|
||||
}
|
||||
|
||||
public function testItIsInstanceOfConstraint(): void
|
||||
{
|
||||
$this->assertInstanceOf(Constraint::class, $this->constraint);
|
||||
}
|
||||
|
||||
public function testToString(): void
|
||||
{
|
||||
$this->assertSame('contains a notification with message "Test message"', $this->constraint->toString());
|
||||
}
|
||||
|
||||
public function testMatchesWithExactMessage(): void
|
||||
{
|
||||
$notification = new Notification();
|
||||
$notification->setMessage('Test message');
|
||||
|
||||
$events = new NotificationEvents();
|
||||
$events->addEnvelope(new Envelope($notification));
|
||||
|
||||
$result = $this->constraint->evaluate($events, '', true);
|
||||
|
||||
$this->assertTrue($result);
|
||||
}
|
||||
|
||||
public function testMatchesWithPartialMessage(): void
|
||||
{
|
||||
$notification = new Notification();
|
||||
$notification->setMessage('This is a Test message for you');
|
||||
|
||||
$events = new NotificationEvents();
|
||||
$events->addEnvelope(new Envelope($notification));
|
||||
|
||||
$result = $this->constraint->evaluate($events, '', true);
|
||||
|
||||
$this->assertTrue($result);
|
||||
}
|
||||
|
||||
public function testMatchesWithMessageNotPresent(): void
|
||||
{
|
||||
$notification = new Notification();
|
||||
$notification->setMessage('Different message');
|
||||
|
||||
$events = new NotificationEvents();
|
||||
$events->addEnvelope(new Envelope($notification));
|
||||
|
||||
$result = $this->constraint->evaluate($events, '', true);
|
||||
|
||||
$this->assertFalse($result);
|
||||
}
|
||||
|
||||
public function testMatchesWithEmptyEvents(): void
|
||||
{
|
||||
$events = new NotificationEvents();
|
||||
|
||||
$result = $this->constraint->evaluate($events, '', true);
|
||||
|
||||
$this->assertFalse($result);
|
||||
}
|
||||
|
||||
public function testMatchesWithNonNotificationEvents(): void
|
||||
{
|
||||
$result = $this->constraint->evaluate('not an event', '', true);
|
||||
|
||||
$this->assertFalse($result);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,119 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Flasher\Tests\Prime\Test\Constraint;
|
||||
|
||||
use Flasher\Prime\EventDispatcher\Event\NotificationEvents;
|
||||
use Flasher\Prime\Notification\Envelope;
|
||||
use Flasher\Prime\Notification\Notification;
|
||||
use Flasher\Prime\Test\Constraint\NotificationOption;
|
||||
use PHPUnit\Framework\Constraint\Constraint;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
final class NotificationOptionTest extends TestCase
|
||||
{
|
||||
public function testItIsInstanceOfConstraint(): void
|
||||
{
|
||||
$constraint = new NotificationOption('timeout');
|
||||
$this->assertInstanceOf(Constraint::class, $constraint);
|
||||
}
|
||||
|
||||
public function testToStringWithoutValue(): void
|
||||
{
|
||||
$constraint = new NotificationOption('timeout');
|
||||
|
||||
$this->assertSame('contains a notification with an option "timeout"', $constraint->toString());
|
||||
}
|
||||
|
||||
public function testToStringWithValue(): void
|
||||
{
|
||||
$constraint = new NotificationOption('timeout', 5000);
|
||||
|
||||
$this->assertStringContainsString('contains a notification with an option "timeout"', $constraint->toString());
|
||||
$this->assertStringContainsString('5000', $constraint->toString());
|
||||
}
|
||||
|
||||
public function testMatchesWithMatchingOption(): void
|
||||
{
|
||||
$constraint = new NotificationOption('timeout', 5000);
|
||||
|
||||
$notification = new Notification();
|
||||
$notification->setOptions(['timeout' => 5000, 'position' => 'top']);
|
||||
|
||||
$events = new NotificationEvents();
|
||||
$events->addEnvelope(new Envelope($notification));
|
||||
|
||||
$result = $constraint->evaluate($events, '', true);
|
||||
|
||||
$this->assertTrue($result);
|
||||
}
|
||||
|
||||
public function testMatchesWithNonMatchingValue(): void
|
||||
{
|
||||
$constraint = new NotificationOption('timeout', 5000);
|
||||
|
||||
$notification = new Notification();
|
||||
$notification->setOptions(['timeout' => 3000]);
|
||||
|
||||
$events = new NotificationEvents();
|
||||
$events->addEnvelope(new Envelope($notification));
|
||||
|
||||
$result = $constraint->evaluate($events, '', true);
|
||||
|
||||
$this->assertFalse($result);
|
||||
}
|
||||
|
||||
public function testMatchesWithMissingOption(): void
|
||||
{
|
||||
$constraint = new NotificationOption('timeout', 5000);
|
||||
|
||||
$notification = new Notification();
|
||||
$notification->setOptions(['position' => 'top']);
|
||||
|
||||
$events = new NotificationEvents();
|
||||
$events->addEnvelope(new Envelope($notification));
|
||||
|
||||
$result = $constraint->evaluate($events, '', true);
|
||||
|
||||
$this->assertFalse($result);
|
||||
}
|
||||
|
||||
public function testMatchesWithEmptyEvents(): void
|
||||
{
|
||||
$constraint = new NotificationOption('timeout', 5000);
|
||||
$events = new NotificationEvents();
|
||||
|
||||
$result = $constraint->evaluate($events, '', true);
|
||||
|
||||
$this->assertFalse($result);
|
||||
}
|
||||
|
||||
public function testMatchesWithNonNotificationEvents(): void
|
||||
{
|
||||
$constraint = new NotificationOption('timeout', 5000);
|
||||
|
||||
$result = $constraint->evaluate('not an event', '', true);
|
||||
|
||||
$this->assertFalse($result);
|
||||
}
|
||||
|
||||
public function testMatchesWithMultipleNotifications(): void
|
||||
{
|
||||
$constraint = new NotificationOption('timeout', 5000);
|
||||
|
||||
$notification1 = new Notification();
|
||||
$notification1->setOptions(['timeout' => 3000]);
|
||||
|
||||
$notification2 = new Notification();
|
||||
$notification2->setOptions(['timeout' => 5000]);
|
||||
|
||||
$events = new NotificationEvents();
|
||||
$events->addEnvelope(new Envelope($notification1));
|
||||
$events->addEnvelope(new Envelope($notification2));
|
||||
|
||||
$result = $constraint->evaluate($events, '', true);
|
||||
|
||||
$this->assertTrue($result);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,121 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Flasher\Tests\Prime\Test\Constraint;
|
||||
|
||||
use Flasher\Prime\EventDispatcher\Event\NotificationEvents;
|
||||
use Flasher\Prime\Notification\Envelope;
|
||||
use Flasher\Prime\Notification\Notification;
|
||||
use Flasher\Prime\Test\Constraint\NotificationOptions;
|
||||
use PHPUnit\Framework\Constraint\Constraint;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
final class NotificationOptionsTest extends TestCase
|
||||
{
|
||||
private NotificationOptions $constraint;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->constraint = new NotificationOptions(['timeout' => 5000, 'position' => 'top']);
|
||||
}
|
||||
|
||||
public function testItIsInstanceOfConstraint(): void
|
||||
{
|
||||
$this->assertInstanceOf(Constraint::class, $this->constraint);
|
||||
}
|
||||
|
||||
public function testToString(): void
|
||||
{
|
||||
$result = $this->constraint->toString();
|
||||
|
||||
$this->assertStringContainsString('contains a notification with options matching', $result);
|
||||
$this->assertStringContainsString('timeout', $result);
|
||||
$this->assertStringContainsString('position', $result);
|
||||
}
|
||||
|
||||
public function testMatchesWithAllOptionsMatching(): void
|
||||
{
|
||||
$notification = new Notification();
|
||||
$notification->setOptions(['timeout' => 5000, 'position' => 'top', 'theme' => 'dark']);
|
||||
|
||||
$events = new NotificationEvents();
|
||||
$events->addEnvelope(new Envelope($notification));
|
||||
|
||||
$result = $this->constraint->evaluate($events, '', true);
|
||||
|
||||
$this->assertTrue($result);
|
||||
}
|
||||
|
||||
public function testMatchesWithExactOptionsMatching(): void
|
||||
{
|
||||
$notification = new Notification();
|
||||
$notification->setOptions(['timeout' => 5000, 'position' => 'top']);
|
||||
|
||||
$events = new NotificationEvents();
|
||||
$events->addEnvelope(new Envelope($notification));
|
||||
|
||||
$result = $this->constraint->evaluate($events, '', true);
|
||||
|
||||
$this->assertTrue($result);
|
||||
}
|
||||
|
||||
public function testMatchesWithPartialOptionsNotMatching(): void
|
||||
{
|
||||
$notification = new Notification();
|
||||
$notification->setOptions(['timeout' => 5000]);
|
||||
|
||||
$events = new NotificationEvents();
|
||||
$events->addEnvelope(new Envelope($notification));
|
||||
|
||||
$result = $this->constraint->evaluate($events, '', true);
|
||||
|
||||
$this->assertFalse($result);
|
||||
}
|
||||
|
||||
public function testMatchesWithDifferentOptionValue(): void
|
||||
{
|
||||
$notification = new Notification();
|
||||
$notification->setOptions(['timeout' => 3000, 'position' => 'top']);
|
||||
|
||||
$events = new NotificationEvents();
|
||||
$events->addEnvelope(new Envelope($notification));
|
||||
|
||||
$result = $this->constraint->evaluate($events, '', true);
|
||||
|
||||
$this->assertFalse($result);
|
||||
}
|
||||
|
||||
public function testMatchesWithEmptyEvents(): void
|
||||
{
|
||||
$events = new NotificationEvents();
|
||||
|
||||
$result = $this->constraint->evaluate($events, '', true);
|
||||
|
||||
$this->assertFalse($result);
|
||||
}
|
||||
|
||||
public function testMatchesWithNonNotificationEvents(): void
|
||||
{
|
||||
$result = $this->constraint->evaluate('not an event', '', true);
|
||||
|
||||
$this->assertFalse($result);
|
||||
}
|
||||
|
||||
public function testMatchesWithMultipleNotifications(): void
|
||||
{
|
||||
$notification1 = new Notification();
|
||||
$notification1->setOptions(['timeout' => 3000]);
|
||||
|
||||
$notification2 = new Notification();
|
||||
$notification2->setOptions(['timeout' => 5000, 'position' => 'top']);
|
||||
|
||||
$events = new NotificationEvents();
|
||||
$events->addEnvelope(new Envelope($notification1));
|
||||
$events->addEnvelope(new Envelope($notification2));
|
||||
|
||||
$result = $this->constraint->evaluate($events, '', true);
|
||||
|
||||
$this->assertTrue($result);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,87 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Flasher\Tests\Prime\Test\Constraint;
|
||||
|
||||
use Flasher\Prime\EventDispatcher\Event\NotificationEvents;
|
||||
use Flasher\Prime\Notification\Envelope;
|
||||
use Flasher\Prime\Notification\Notification;
|
||||
use Flasher\Prime\Test\Constraint\NotificationTitle;
|
||||
use PHPUnit\Framework\Constraint\Constraint;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
final class NotificationTitleTest extends TestCase
|
||||
{
|
||||
private NotificationTitle $constraint;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->constraint = new NotificationTitle('Test Title');
|
||||
}
|
||||
|
||||
public function testItIsInstanceOfConstraint(): void
|
||||
{
|
||||
$this->assertInstanceOf(Constraint::class, $this->constraint);
|
||||
}
|
||||
|
||||
public function testToString(): void
|
||||
{
|
||||
$this->assertSame('contains a notification with a title containing "Test Title"', $this->constraint->toString());
|
||||
}
|
||||
|
||||
public function testMatchesWithExactTitle(): void
|
||||
{
|
||||
$notification = new Notification();
|
||||
$notification->setTitle('Test Title');
|
||||
|
||||
$events = new NotificationEvents();
|
||||
$events->addEnvelope(new Envelope($notification));
|
||||
|
||||
$result = $this->constraint->evaluate($events, '', true);
|
||||
|
||||
$this->assertTrue($result);
|
||||
}
|
||||
|
||||
public function testMatchesWithPartialTitle(): void
|
||||
{
|
||||
$notification = new Notification();
|
||||
$notification->setTitle('This is a Test Title for you');
|
||||
|
||||
$events = new NotificationEvents();
|
||||
$events->addEnvelope(new Envelope($notification));
|
||||
|
||||
$result = $this->constraint->evaluate($events, '', true);
|
||||
|
||||
$this->assertTrue($result);
|
||||
}
|
||||
|
||||
public function testMatchesWithTitleNotPresent(): void
|
||||
{
|
||||
$notification = new Notification();
|
||||
$notification->setTitle('Different Title');
|
||||
|
||||
$events = new NotificationEvents();
|
||||
$events->addEnvelope(new Envelope($notification));
|
||||
|
||||
$result = $this->constraint->evaluate($events, '', true);
|
||||
|
||||
$this->assertFalse($result);
|
||||
}
|
||||
|
||||
public function testMatchesWithEmptyEvents(): void
|
||||
{
|
||||
$events = new NotificationEvents();
|
||||
|
||||
$result = $this->constraint->evaluate($events, '', true);
|
||||
|
||||
$this->assertFalse($result);
|
||||
}
|
||||
|
||||
public function testMatchesWithNonNotificationEvents(): void
|
||||
{
|
||||
$result = $this->constraint->evaluate('not an event', '', true);
|
||||
|
||||
$this->assertFalse($result);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,91 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Flasher\Tests\Prime\Test\Constraint;
|
||||
|
||||
use Flasher\Prime\EventDispatcher\Event\NotificationEvents;
|
||||
use Flasher\Prime\Notification\Envelope;
|
||||
use Flasher\Prime\Notification\Notification;
|
||||
use Flasher\Prime\Test\Constraint\NotificationType;
|
||||
use PHPUnit\Framework\Constraint\Constraint;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
final class NotificationTypeTest extends TestCase
|
||||
{
|
||||
private NotificationType $constraint;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->constraint = new NotificationType('success');
|
||||
}
|
||||
|
||||
public function testItIsInstanceOfConstraint(): void
|
||||
{
|
||||
$this->assertInstanceOf(Constraint::class, $this->constraint);
|
||||
}
|
||||
|
||||
public function testToString(): void
|
||||
{
|
||||
$this->assertSame('contains a notification of type "success".', $this->constraint->toString());
|
||||
}
|
||||
|
||||
public function testMatchesWithTypePresent(): void
|
||||
{
|
||||
$notification = new Notification();
|
||||
$notification->setType('success');
|
||||
|
||||
$events = new NotificationEvents();
|
||||
$events->addEnvelope(new Envelope($notification));
|
||||
|
||||
$result = $this->constraint->evaluate($events, '', true);
|
||||
|
||||
$this->assertTrue($result);
|
||||
}
|
||||
|
||||
public function testMatchesWithTypeNotPresent(): void
|
||||
{
|
||||
$notification = new Notification();
|
||||
$notification->setType('error');
|
||||
|
||||
$events = new NotificationEvents();
|
||||
$events->addEnvelope(new Envelope($notification));
|
||||
|
||||
$result = $this->constraint->evaluate($events, '', true);
|
||||
|
||||
$this->assertFalse($result);
|
||||
}
|
||||
|
||||
public function testMatchesWithEmptyEvents(): void
|
||||
{
|
||||
$events = new NotificationEvents();
|
||||
|
||||
$result = $this->constraint->evaluate($events, '', true);
|
||||
|
||||
$this->assertFalse($result);
|
||||
}
|
||||
|
||||
public function testMatchesWithNonNotificationEvents(): void
|
||||
{
|
||||
$result = $this->constraint->evaluate('not an event', '', true);
|
||||
|
||||
$this->assertFalse($result);
|
||||
}
|
||||
|
||||
public function testMatchesWithMultipleNotifications(): void
|
||||
{
|
||||
$notification1 = new Notification();
|
||||
$notification1->setType('error');
|
||||
|
||||
$notification2 = new Notification();
|
||||
$notification2->setType('success');
|
||||
|
||||
$events = new NotificationEvents();
|
||||
$events->addEnvelope(new Envelope($notification1));
|
||||
$events->addEnvelope(new Envelope($notification2));
|
||||
|
||||
$result = $this->constraint->evaluate($events, '', true);
|
||||
|
||||
$this->assertTrue($result);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,468 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Flasher\Tests\Prime\Test;
|
||||
|
||||
use Flasher\Prime\Container\FlasherContainer;
|
||||
use Flasher\Prime\EventDispatcher\Event\NotificationEvents;
|
||||
use Flasher\Prime\EventDispatcher\Event\PresentationEvent;
|
||||
use Flasher\Prime\EventDispatcher\EventListener\NotificationLoggerListener;
|
||||
use Flasher\Prime\Notification\Envelope;
|
||||
use Flasher\Prime\Notification\Notification;
|
||||
use Flasher\Prime\Test\FlasherAssert;
|
||||
use PHPUnit\Framework\ExpectationFailedException;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Psr\Container\ContainerInterface;
|
||||
|
||||
final class FlasherAssertTest extends TestCase
|
||||
{
|
||||
private NotificationLoggerListener $listener;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
FlasherContainer::reset();
|
||||
|
||||
$this->listener = new NotificationLoggerListener();
|
||||
|
||||
$container = $this->createMock(ContainerInterface::class);
|
||||
$container->method('has')
|
||||
->with('flasher.notification_logger_listener')
|
||||
->willReturn(true);
|
||||
$container->method('get')
|
||||
->with('flasher.notification_logger_listener')
|
||||
->willReturn($this->listener);
|
||||
|
||||
FlasherContainer::from($container);
|
||||
}
|
||||
|
||||
protected function tearDown(): void
|
||||
{
|
||||
parent::tearDown();
|
||||
FlasherContainer::reset();
|
||||
}
|
||||
|
||||
private function addNotification(Notification $notification): void
|
||||
{
|
||||
$envelope = new Envelope($notification);
|
||||
$event = new PresentationEvent([$envelope], []);
|
||||
$this->listener->onPresentation($event);
|
||||
}
|
||||
|
||||
public function testThatReturnsSelf(): void
|
||||
{
|
||||
$result = FlasherAssert::that();
|
||||
|
||||
$this->assertInstanceOf(FlasherAssert::class, $result);
|
||||
}
|
||||
|
||||
public function testHasNotificationsPasses(): void
|
||||
{
|
||||
$this->addNotification(new Notification());
|
||||
|
||||
$result = FlasherAssert::hasNotifications();
|
||||
|
||||
$this->assertInstanceOf(FlasherAssert::class, $result);
|
||||
}
|
||||
|
||||
public function testHasNotificationsFails(): void
|
||||
{
|
||||
$this->expectException(ExpectationFailedException::class);
|
||||
$this->expectExceptionMessage('Expected at least one notification to exist.');
|
||||
|
||||
FlasherAssert::hasNotifications();
|
||||
}
|
||||
|
||||
public function testNoNotificationsPasses(): void
|
||||
{
|
||||
$result = FlasherAssert::noNotifications();
|
||||
|
||||
$this->assertInstanceOf(FlasherAssert::class, $result);
|
||||
}
|
||||
|
||||
public function testNoNotificationsFails(): void
|
||||
{
|
||||
$this->expectException(ExpectationFailedException::class);
|
||||
$this->expectExceptionMessage('Expected no notifications to exist.');
|
||||
|
||||
$this->addNotification(new Notification());
|
||||
|
||||
FlasherAssert::noNotifications();
|
||||
}
|
||||
|
||||
public function testWithNotificationPasses(): void
|
||||
{
|
||||
$notification = new Notification();
|
||||
$notification->setType('success');
|
||||
$notification->setMessage('Test message');
|
||||
|
||||
$this->addNotification($notification);
|
||||
|
||||
$result = FlasherAssert::withNotification('success', 'Test message');
|
||||
|
||||
$this->assertInstanceOf(FlasherAssert::class, $result);
|
||||
}
|
||||
|
||||
public function testWithNotificationFails(): void
|
||||
{
|
||||
$this->expectException(ExpectationFailedException::class);
|
||||
|
||||
$notification = new Notification();
|
||||
$notification->setType('error');
|
||||
|
||||
$this->addNotification($notification);
|
||||
|
||||
FlasherAssert::withNotification('success');
|
||||
}
|
||||
|
||||
public function testNotificationAlias(): void
|
||||
{
|
||||
$notification = new Notification();
|
||||
$notification->setType('success');
|
||||
$notification->setMessage('Test message');
|
||||
|
||||
$this->addNotification($notification);
|
||||
|
||||
$result = FlasherAssert::notification('success', 'Test message');
|
||||
|
||||
$this->assertInstanceOf(FlasherAssert::class, $result);
|
||||
}
|
||||
|
||||
public function testWithCountPasses(): void
|
||||
{
|
||||
$this->addNotification(new Notification());
|
||||
$this->addNotification(new Notification());
|
||||
|
||||
$result = FlasherAssert::withCount(2);
|
||||
|
||||
$this->assertInstanceOf(FlasherAssert::class, $result);
|
||||
}
|
||||
|
||||
public function testWithCountFails(): void
|
||||
{
|
||||
$this->expectException(ExpectationFailedException::class);
|
||||
|
||||
$this->addNotification(new Notification());
|
||||
|
||||
FlasherAssert::withCount(2);
|
||||
}
|
||||
|
||||
public function testCountAlias(): void
|
||||
{
|
||||
$this->addNotification(new Notification());
|
||||
$this->addNotification(new Notification());
|
||||
|
||||
$result = FlasherAssert::count(2);
|
||||
|
||||
$this->assertInstanceOf(FlasherAssert::class, $result);
|
||||
}
|
||||
|
||||
public function testWithTypePasses(): void
|
||||
{
|
||||
$notification = new Notification();
|
||||
$notification->setType('success');
|
||||
|
||||
$this->addNotification($notification);
|
||||
|
||||
$result = FlasherAssert::withType('success');
|
||||
|
||||
$this->assertInstanceOf(FlasherAssert::class, $result);
|
||||
}
|
||||
|
||||
public function testWithTypeFails(): void
|
||||
{
|
||||
$this->expectException(ExpectationFailedException::class);
|
||||
|
||||
$notification = new Notification();
|
||||
$notification->setType('error');
|
||||
|
||||
$this->addNotification($notification);
|
||||
|
||||
FlasherAssert::withType('success');
|
||||
}
|
||||
|
||||
public function testTypeAlias(): void
|
||||
{
|
||||
$notification = new Notification();
|
||||
$notification->setType('success');
|
||||
|
||||
$this->addNotification($notification);
|
||||
|
||||
$result = FlasherAssert::type('success');
|
||||
|
||||
$this->assertInstanceOf(FlasherAssert::class, $result);
|
||||
}
|
||||
|
||||
public function testWithSuccessPasses(): void
|
||||
{
|
||||
$notification = new Notification();
|
||||
$notification->setType('success');
|
||||
|
||||
$this->addNotification($notification);
|
||||
|
||||
$result = FlasherAssert::withSuccess();
|
||||
|
||||
$this->assertInstanceOf(FlasherAssert::class, $result);
|
||||
}
|
||||
|
||||
public function testSuccessAlias(): void
|
||||
{
|
||||
$notification = new Notification();
|
||||
$notification->setType('success');
|
||||
|
||||
$this->addNotification($notification);
|
||||
|
||||
$result = FlasherAssert::success();
|
||||
|
||||
$this->assertInstanceOf(FlasherAssert::class, $result);
|
||||
}
|
||||
|
||||
public function testWithWarningPasses(): void
|
||||
{
|
||||
$notification = new Notification();
|
||||
$notification->setType('warning');
|
||||
|
||||
$this->addNotification($notification);
|
||||
|
||||
$result = FlasherAssert::withWarning();
|
||||
|
||||
$this->assertInstanceOf(FlasherAssert::class, $result);
|
||||
}
|
||||
|
||||
public function testWarningAlias(): void
|
||||
{
|
||||
$notification = new Notification();
|
||||
$notification->setType('warning');
|
||||
|
||||
$this->addNotification($notification);
|
||||
|
||||
$result = FlasherAssert::warning();
|
||||
|
||||
$this->assertInstanceOf(FlasherAssert::class, $result);
|
||||
}
|
||||
|
||||
public function testWithErrorPasses(): void
|
||||
{
|
||||
$notification = new Notification();
|
||||
$notification->setType('error');
|
||||
|
||||
$this->addNotification($notification);
|
||||
|
||||
$result = FlasherAssert::withError();
|
||||
|
||||
$this->assertInstanceOf(FlasherAssert::class, $result);
|
||||
}
|
||||
|
||||
public function testErrorAlias(): void
|
||||
{
|
||||
$notification = new Notification();
|
||||
$notification->setType('error');
|
||||
|
||||
$this->addNotification($notification);
|
||||
|
||||
$result = FlasherAssert::error();
|
||||
|
||||
$this->assertInstanceOf(FlasherAssert::class, $result);
|
||||
}
|
||||
|
||||
public function testWithInfoPasses(): void
|
||||
{
|
||||
$notification = new Notification();
|
||||
$notification->setType('info');
|
||||
|
||||
$this->addNotification($notification);
|
||||
|
||||
$result = FlasherAssert::withInfo();
|
||||
|
||||
$this->assertInstanceOf(FlasherAssert::class, $result);
|
||||
}
|
||||
|
||||
public function testInfoAlias(): void
|
||||
{
|
||||
$notification = new Notification();
|
||||
$notification->setType('info');
|
||||
|
||||
$this->addNotification($notification);
|
||||
|
||||
$result = FlasherAssert::info();
|
||||
|
||||
$this->assertInstanceOf(FlasherAssert::class, $result);
|
||||
}
|
||||
|
||||
public function testWithTitlePasses(): void
|
||||
{
|
||||
$notification = new Notification();
|
||||
$notification->setTitle('Test Title');
|
||||
|
||||
$this->addNotification($notification);
|
||||
|
||||
$result = FlasherAssert::withTitle('Test Title');
|
||||
|
||||
$this->assertInstanceOf(FlasherAssert::class, $result);
|
||||
}
|
||||
|
||||
public function testWithTitleFails(): void
|
||||
{
|
||||
$this->expectException(ExpectationFailedException::class);
|
||||
|
||||
$notification = new Notification();
|
||||
$notification->setTitle('Different Title');
|
||||
|
||||
$this->addNotification($notification);
|
||||
|
||||
FlasherAssert::withTitle('Test Title');
|
||||
}
|
||||
|
||||
public function testTitleAlias(): void
|
||||
{
|
||||
$notification = new Notification();
|
||||
$notification->setTitle('Test Title');
|
||||
|
||||
$this->addNotification($notification);
|
||||
|
||||
$result = FlasherAssert::title('Test Title');
|
||||
|
||||
$this->assertInstanceOf(FlasherAssert::class, $result);
|
||||
}
|
||||
|
||||
public function testWithMessagePasses(): void
|
||||
{
|
||||
$notification = new Notification();
|
||||
$notification->setMessage('Test message');
|
||||
|
||||
$this->addNotification($notification);
|
||||
|
||||
$result = FlasherAssert::withMessage('Test message');
|
||||
|
||||
$this->assertInstanceOf(FlasherAssert::class, $result);
|
||||
}
|
||||
|
||||
public function testWithMessageFails(): void
|
||||
{
|
||||
$this->expectException(ExpectationFailedException::class);
|
||||
|
||||
$notification = new Notification();
|
||||
$notification->setMessage('Different message');
|
||||
|
||||
$this->addNotification($notification);
|
||||
|
||||
FlasherAssert::withMessage('Test message');
|
||||
}
|
||||
|
||||
public function testMessageAlias(): void
|
||||
{
|
||||
$notification = new Notification();
|
||||
$notification->setMessage('Test message');
|
||||
|
||||
$this->addNotification($notification);
|
||||
|
||||
$result = FlasherAssert::message('Test message');
|
||||
|
||||
$this->assertInstanceOf(FlasherAssert::class, $result);
|
||||
}
|
||||
|
||||
public function testWithOptionsPasses(): void
|
||||
{
|
||||
$notification = new Notification();
|
||||
$notification->setOptions(['timeout' => 5000, 'position' => 'top']);
|
||||
|
||||
$this->addNotification($notification);
|
||||
|
||||
$result = FlasherAssert::withOptions(['timeout' => 5000]);
|
||||
|
||||
$this->assertInstanceOf(FlasherAssert::class, $result);
|
||||
}
|
||||
|
||||
public function testWithOptionsFails(): void
|
||||
{
|
||||
$this->expectException(ExpectationFailedException::class);
|
||||
|
||||
$notification = new Notification();
|
||||
$notification->setOptions(['timeout' => 3000]);
|
||||
|
||||
$this->addNotification($notification);
|
||||
|
||||
FlasherAssert::withOptions(['timeout' => 5000]);
|
||||
}
|
||||
|
||||
public function testOptionsAlias(): void
|
||||
{
|
||||
$notification = new Notification();
|
||||
$notification->setOptions(['timeout' => 5000]);
|
||||
|
||||
$this->addNotification($notification);
|
||||
|
||||
$result = FlasherAssert::options(['timeout' => 5000]);
|
||||
|
||||
$this->assertInstanceOf(FlasherAssert::class, $result);
|
||||
}
|
||||
|
||||
public function testWithOptionPasses(): void
|
||||
{
|
||||
$notification = new Notification();
|
||||
$notification->setOptions(['timeout' => 5000]);
|
||||
|
||||
$this->addNotification($notification);
|
||||
|
||||
$result = FlasherAssert::withOption('timeout', 5000);
|
||||
|
||||
$this->assertInstanceOf(FlasherAssert::class, $result);
|
||||
}
|
||||
|
||||
public function testWithOptionFails(): void
|
||||
{
|
||||
$this->expectException(ExpectationFailedException::class);
|
||||
|
||||
$notification = new Notification();
|
||||
$notification->setOptions(['timeout' => 3000]);
|
||||
|
||||
$this->addNotification($notification);
|
||||
|
||||
FlasherAssert::withOption('timeout', 5000);
|
||||
}
|
||||
|
||||
public function testOptionAlias(): void
|
||||
{
|
||||
$notification = new Notification();
|
||||
$notification->setOptions(['timeout' => 5000]);
|
||||
|
||||
$this->addNotification($notification);
|
||||
|
||||
$result = FlasherAssert::option('timeout', 5000);
|
||||
|
||||
$this->assertInstanceOf(FlasherAssert::class, $result);
|
||||
}
|
||||
|
||||
public function testGetNotificationEventsWithoutListener(): void
|
||||
{
|
||||
FlasherContainer::reset();
|
||||
|
||||
$container = $this->createMock(ContainerInterface::class);
|
||||
$container->method('has')->willReturn(false);
|
||||
FlasherContainer::from($container);
|
||||
|
||||
$events = FlasherAssert::getNotificationEvents();
|
||||
|
||||
$this->assertInstanceOf(NotificationEvents::class, $events);
|
||||
$this->assertCount(0, $events->getEnvelopes());
|
||||
}
|
||||
|
||||
public function testChainedAssertions(): void
|
||||
{
|
||||
$notification = new Notification();
|
||||
$notification->setType('success');
|
||||
$notification->setMessage('Test message');
|
||||
$notification->setTitle('Test Title');
|
||||
$notification->setOptions(['timeout' => 5000]);
|
||||
|
||||
$this->addNotification($notification);
|
||||
|
||||
FlasherAssert::hasNotifications()
|
||||
->count(1)
|
||||
->type('success')
|
||||
->message('Test message')
|
||||
->title('Test Title')
|
||||
->option('timeout', 5000);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user