Skip to content

Commit 87db492

Browse files
committed
add NotifierAssertionTrait
1 parent d139706 commit 87db492

File tree

5 files changed

+242
-0
lines changed

5 files changed

+242
-0
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
CHANGELOG
22
=========
33

4+
6.2
5+
---
6+
7+
* Add PHPUnit constraints
8+
49
6.1
510
---
611

Test/Constraint/NotificationCount.php

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Notifier\Test\Constraint;
13+
14+
use PHPUnit\Framework\Constraint\Constraint;
15+
use Symfony\Component\Notifier\Event\NotificationEvents;
16+
17+
/**
18+
* @author Smaïne Milianni <smaine.milianni@gmail.com>
19+
*/
20+
final class NotificationCount extends Constraint
21+
{
22+
private $expectedValue;
23+
private $transport;
24+
private $queued;
25+
26+
public function __construct(int $expectedValue, string $transport = null, bool $queued = false)
27+
{
28+
$this->expectedValue = $expectedValue;
29+
$this->transport = $transport;
30+
$this->queued = $queued;
31+
}
32+
33+
/**
34+
* {@inheritdoc}
35+
*/
36+
public function toString(): string
37+
{
38+
return sprintf('%shas %s "%d" emails', $this->transport ? $this->transport.' ' : '', $this->queued ? 'queued' : 'sent', $this->expectedValue);
39+
}
40+
41+
/**
42+
* @param NotificationEvents $events
43+
*
44+
* {@inheritdoc}
45+
*/
46+
protected function matches($events): bool
47+
{
48+
return $this->expectedValue === $this->countNotifications($events);
49+
}
50+
51+
/**
52+
* @param NotificationEvents $events
53+
*
54+
* {@inheritdoc}
55+
*/
56+
protected function failureDescription($events): string
57+
{
58+
return sprintf('the Transport %s (%d %s)', $this->toString(), $this->countNotifications($events), $this->queued ? 'queued' : 'sent');
59+
}
60+
61+
private function countNotifications(NotificationEvents $events): int
62+
{
63+
$count = 0;
64+
foreach ($events->getEvents($this->transport) as $event) {
65+
if (
66+
($this->queued && $event->isQueued())
67+
||
68+
(!$this->queued && !$event->isQueued())
69+
) {
70+
++$count;
71+
}
72+
}
73+
74+
return $count;
75+
}
76+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Notifier\Test\Constraint;
13+
14+
use PHPUnit\Framework\Constraint\Constraint;
15+
use Symfony\Component\Notifier\Event\MessageEvent;
16+
17+
/**
18+
* @author Smaïne Milianni <smaine.milianni@gmail.com>
19+
*/
20+
final class NotificationIsQueued extends Constraint
21+
{
22+
/**
23+
* {@inheritdoc}
24+
*/
25+
public function toString(): string
26+
{
27+
return 'is queued';
28+
}
29+
30+
/**
31+
* @param MessageEvent $event
32+
*
33+
* {@inheritdoc}
34+
*/
35+
protected function matches($event): bool
36+
{
37+
return $event->isQueued();
38+
}
39+
40+
/**
41+
* @param MessageEvent $event
42+
*
43+
* {@inheritdoc}
44+
*/
45+
protected function failureDescription($event): string
46+
{
47+
return 'the Notification '.$this->toString();
48+
}
49+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Notifier\Test\Constraint;
13+
14+
use PHPUnit\Framework\Constraint\Constraint;
15+
use Symfony\Component\Notifier\Message\MessageInterface;
16+
17+
/**
18+
* @author Smaïne Milianni <smaine.milianni@gmail.com>
19+
*/
20+
final class NotificationSubjectContains extends Constraint
21+
{
22+
private $expectedText;
23+
24+
public function __construct(string $expectedText)
25+
{
26+
$this->expectedText = $expectedText;
27+
}
28+
29+
/**
30+
* {@inheritdoc}
31+
*/
32+
public function toString(): string
33+
{
34+
return sprintf('contains "%s"', $this->expectedText);
35+
}
36+
37+
/**
38+
* {@inheritdoc}
39+
*
40+
* @param MessageInterface $message
41+
*/
42+
protected function matches($message): bool
43+
{
44+
return false !== mb_strpos($message->getSubject(), $this->expectedText);
45+
}
46+
47+
/**
48+
* {@inheritdoc}
49+
*
50+
* @param MessageInterface $message
51+
*/
52+
protected function failureDescription($message): string
53+
{
54+
return 'the Notification subject '.$this->toString();
55+
}
56+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Notifier\Test\Constraint;
13+
14+
use PHPUnit\Framework\Constraint\Constraint;
15+
use Symfony\Component\Notifier\Message\MessageInterface;
16+
17+
/**
18+
* @author Smaïne Milianni <smaine.milianni@gmail.com>
19+
*/
20+
final class NotificationTransportIsEqual extends Constraint
21+
{
22+
private $expectedText;
23+
24+
public function __construct(string $expectedText)
25+
{
26+
$this->expectedText = $expectedText;
27+
}
28+
29+
/**
30+
* {@inheritdoc}
31+
*/
32+
public function toString(): string
33+
{
34+
return sprintf('is "%s"', $this->expectedText);
35+
}
36+
37+
/**
38+
* {@inheritdoc}
39+
*
40+
* @param MessageInterface $message
41+
*/
42+
protected function matches($message): bool
43+
{
44+
return $message->getTransport() === $this->expectedText;
45+
}
46+
47+
/**
48+
* {@inheritdoc}
49+
*
50+
* @param MessageInterface $message
51+
*/
52+
protected function failureDescription($message): string
53+
{
54+
return 'the Notification transport '.$this->toString();
55+
}
56+
}

0 commit comments

Comments
 (0)