Skip to content

Commit b1724a0

Browse files
committed
Merge branch '4.4'
* 4.4: added PHPUnit constraints and assertions for the Mailer [Mailer] added support for the profiler
2 parents 080a0b8 + 0ec4eb9 commit b1724a0

File tree

11 files changed

+457
-0
lines changed

11 files changed

+457
-0
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ CHANGELOG
44
4.4.0
55
-----
66

7+
* Added PHPUnit constraints
8+
* Added `MessageDataCollector`
79
* Added `MessageEvents` and `MessageLoggerListener` to allow collecting sent emails
810
* [BC BREAK] `TransportInterface` has a new `getName()` method
911
* [BC BREAK] Classes `AbstractApiTransport` and `AbstractHttpTransport` moved under `Transport` sub-namespace.

Constraint/EmailCount.php

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
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\Mailer\Test\Constraint;
13+
14+
use PHPUnit\Framework\Constraint\Constraint;
15+
use Symfony\Component\Mailer\Event\MessageEvents;
16+
17+
final class EmailCount extends Constraint
18+
{
19+
private $expectedValue;
20+
private $transport;
21+
22+
public function __construct(int $expectedValue, string $transport = null)
23+
{
24+
$this->expectedValue = $expectedValue;
25+
$this->transport = $transport;
26+
}
27+
28+
/**
29+
* {@inheritdoc}
30+
*/
31+
public function toString(): string
32+
{
33+
return sprintf('%shas sent "%d" emails', $this->transport ? $this->transport.' ' : '', $this->expectedValue);
34+
}
35+
36+
/**
37+
* @param MessageEvents $events
38+
*
39+
* {@inheritdoc}
40+
*/
41+
protected function matches($events): bool
42+
{
43+
return $this->expectedValue === \count($events->getEvents($this->transport));
44+
}
45+
46+
/**
47+
* @param MessageEvents $events
48+
*
49+
* {@inheritdoc}
50+
*/
51+
protected function failureDescription($events): string
52+
{
53+
return sprintf('the Transport %s (%d sent)', $this->toString(), \count($events->getEvents($this->transport)));
54+
}
55+
}

Constraint/EmailIsQueued.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
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\Mailer\Test\Constraint;
13+
14+
use PHPUnit\Framework\Constraint\Constraint;
15+
use Symfony\Component\Mailer\Event\MessageEvent;
16+
17+
final class EmailIsQueued extends Constraint
18+
{
19+
/**
20+
* {@inheritdoc}
21+
*/
22+
public function toString(): string
23+
{
24+
return 'is queued';
25+
}
26+
27+
/**
28+
* @param MessageEvent $event
29+
*
30+
* {@inheritdoc}
31+
*/
32+
protected function matches($event): bool
33+
{
34+
return $event->isQueued();
35+
}
36+
37+
/**
38+
* @param MessageEvent $event
39+
*
40+
* {@inheritdoc}
41+
*/
42+
protected function failureDescription($event): string
43+
{
44+
return 'the Email '.$this->toString();
45+
}
46+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
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\Mailer\DataCollector;
13+
14+
use Symfony\Component\HttpFoundation\Request;
15+
use Symfony\Component\HttpFoundation\Response;
16+
use Symfony\Component\HttpKernel\DataCollector\DataCollector;
17+
use Symfony\Component\Mailer\Event\MessageEvents;
18+
use Symfony\Component\Mailer\EventListener\MessageLoggerListener;
19+
20+
/**
21+
* @author Fabien Potencier <[email protected]>
22+
*/
23+
class MessageDataCollector extends DataCollector
24+
{
25+
private $events;
26+
27+
public function __construct(MessageLoggerListener $logger)
28+
{
29+
$this->events = $logger->getEvents();
30+
}
31+
32+
/**
33+
* {@inheritdoc}
34+
*/
35+
public function collect(Request $request, Response $response, \Exception $exception = null)
36+
{
37+
$this->data['events'] = $this->events;
38+
}
39+
40+
public function getEvents(): MessageEvents
41+
{
42+
return $this->data['events'];
43+
}
44+
45+
/**
46+
* {@inheritdoc}
47+
*/
48+
public function reset()
49+
{
50+
$this->data = [];
51+
}
52+
53+
/**
54+
* {@inheritdoc}
55+
*/
56+
public function getName()
57+
{
58+
return 'mailer';
59+
}
60+
}

EmailCount.php

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
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\Mailer\Test\Constraint;
13+
14+
use PHPUnit\Framework\Constraint\Constraint;
15+
use Symfony\Component\Mailer\Event\MessageEvents;
16+
17+
final class EmailCount extends Constraint
18+
{
19+
private $expectedValue;
20+
private $transport;
21+
22+
public function __construct(int $expectedValue, string $transport = null)
23+
{
24+
$this->expectedValue = $expectedValue;
25+
$this->transport = $transport;
26+
}
27+
28+
/**
29+
* {@inheritdoc}
30+
*/
31+
public function toString(): string
32+
{
33+
return sprintf('%shas sent "%d" emails', $this->transport ? $this->transport.' ' : '', $this->expectedValue);
34+
}
35+
36+
/**
37+
* @param MessageEvents $events
38+
*
39+
* {@inheritdoc}
40+
*/
41+
protected function matches($events): bool
42+
{
43+
return $this->expectedValue === \count($events->getEvents($this->transport));
44+
}
45+
46+
/**
47+
* @param MessageEvents $events
48+
*
49+
* {@inheritdoc}
50+
*/
51+
protected function failureDescription($events): string
52+
{
53+
return sprintf('the Transport %s (%d sent)', $this->toString(), \count($events->getEvents($this->transport)));
54+
}
55+
}

EmailIsQueued.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
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\Mailer\Test\Constraint;
13+
14+
use PHPUnit\Framework\Constraint\Constraint;
15+
use Symfony\Component\Mailer\Event\MessageEvent;
16+
17+
final class EmailIsQueued extends Constraint
18+
{
19+
/**
20+
* {@inheritdoc}
21+
*/
22+
public function toString(): string
23+
{
24+
return 'is queued';
25+
}
26+
27+
/**
28+
* @param MessageEvent $event
29+
*
30+
* {@inheritdoc}
31+
*/
32+
protected function matches($event): bool
33+
{
34+
return $event->isQueued();
35+
}
36+
37+
/**
38+
* @param MessageEvent $event
39+
*
40+
* {@inheritdoc}
41+
*/
42+
protected function failureDescription($event): string
43+
{
44+
return 'the Email '.$this->toString();
45+
}
46+
}

Event/MessageEvents.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
namespace Symfony\Component\Mailer\Event;
1313

14+
use Symfony\Component\Mime\RawMessage;
15+
1416
/**
1517
* @author Fabien Potencier <[email protected]>
1618
*/
@@ -48,4 +50,18 @@ public function getEvents(string $name = null): array
4850

4951
return $events;
5052
}
53+
54+
/**
55+
* @return RawMessage[]
56+
*/
57+
public function getMessages(string $name = null): array
58+
{
59+
$events = $this->getEvents($name);
60+
$messages = [];
61+
foreach ($events as $event) {
62+
$messages[] = $event->getMessage();
63+
}
64+
65+
return $messages;
66+
}
5167
}

MessageDataCollector.php

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
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\Mailer\DataCollector;
13+
14+
use Symfony\Component\HttpFoundation\Request;
15+
use Symfony\Component\HttpFoundation\Response;
16+
use Symfony\Component\HttpKernel\DataCollector\DataCollector;
17+
use Symfony\Component\Mailer\Event\MessageEvents;
18+
use Symfony\Component\Mailer\EventListener\MessageLoggerListener;
19+
20+
/**
21+
* @author Fabien Potencier <[email protected]>
22+
*/
23+
class MessageDataCollector extends DataCollector
24+
{
25+
private $events;
26+
27+
public function __construct(MessageLoggerListener $logger)
28+
{
29+
$this->events = $logger->getEvents();
30+
}
31+
32+
/**
33+
* {@inheritdoc}
34+
*/
35+
public function collect(Request $request, Response $response, \Exception $exception = null)
36+
{
37+
$this->data['events'] = $this->events;
38+
}
39+
40+
public function getEvents(): MessageEvents
41+
{
42+
return $this->data['events'];
43+
}
44+
45+
/**
46+
* {@inheritdoc}
47+
*/
48+
public function reset()
49+
{
50+
$this->data = [];
51+
}
52+
53+
/**
54+
* {@inheritdoc}
55+
*/
56+
public function getName()
57+
{
58+
return 'mailer';
59+
}
60+
}

MessageEvents.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
namespace Symfony\Component\Mailer\Event;
1313

14+
use Symfony\Component\Mime\RawMessage;
15+
1416
/**
1517
* @author Fabien Potencier <[email protected]>
1618
*/
@@ -48,4 +50,18 @@ public function getEvents(string $name = null): array
4850

4951
return $events;
5052
}
53+
54+
/**
55+
* @return RawMessage[]
56+
*/
57+
public function getMessages(string $name = null): array
58+
{
59+
$events = $this->getEvents($name);
60+
$messages = [];
61+
foreach ($events as $event) {
62+
$messages[] = $event->getMessage();
63+
}
64+
65+
return $messages;
66+
}
5167
}

0 commit comments

Comments
 (0)