Skip to content

Commit

Permalink
Merge branch '4.4'
Browse files Browse the repository at this point in the history
* 4.4:
  Polyfill assertion method on Filesystem
  [Mailer] added message events logger
  fix typo
  • Loading branch information
nicolas-grekas committed Aug 4, 2019
2 parents d336b11 + 64a2627 commit 9d9e8dc
Show file tree
Hide file tree
Showing 11 changed files with 353 additions and 6 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ install:
- |
# Install the phpunit-bridge from a PR if required
#
# To run a PR with a patched phpunit-bridge, first submit the path for the
# To run a PR with a patched phpunit-bridge, first submit the patch for the
# phpunit-bridge as a separate PR against the next feature-branch then
# uncomment and update the following line with that PR number
#SYMFONY_PHPUNIT_BRIDGE_PR=32886
Expand Down
194 changes: 193 additions & 1 deletion src/Symfony/Bridge/PhpUnit/Legacy/PolyfillAssertTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
use PHPUnit\Framework\Constraint\TraversableContains;

/**
* This trait is @internal
* This trait is @internal.
*/
trait PolyfillAssertTrait
{
Expand Down Expand Up @@ -251,4 +251,196 @@ public static function assertNan($actual, $message = '')
static::assertInternalType('float', $actual, $message);
static::assertTrue(is_nan($actual), $message ? $message : "Failed asserting that $actual is nan.");
}

/**
* @param string $filename
* @param string $message
*
* @return void
*/
public static function assertIsReadable($filename, $message = '')
{
static::assertInternalType('string', $filename, $message);
static::assertTrue(is_readable($filename), $message ? $message : "Failed asserting that $filename is readable.");
}

/**
* @param string $filename
* @param string $message
*
* @return void
*/
public static function assertNotIsReadable($filename, $message = '')
{
static::assertInternalType('string', $filename, $message);
static::assertFalse(is_readable($filename), $message ? $message : "Failed asserting that $filename is not readable.");
}

/**
* @param string $filename
* @param string $message
*
* @return void
*/
public static function assertIsWritable($filename, $message = '')
{
static::assertInternalType('string', $filename, $message);
static::assertTrue(is_writable($filename), $message ? $message : "Failed asserting that $filename is writable.");
}

/**
* @param string $filename
* @param string $message
*
* @return void
*/
public static function assertNotIsWritable($filename, $message = '')
{
static::assertInternalType('string', $filename, $message);
static::assertFalse(is_writable($filename), $message ? $message : "Failed asserting that $filename is not writable.");
}

/**
* @param string $directory
* @param string $message
*
* @return void
*/
public static function assertDirectoryExists($directory, $message = '')
{
static::assertInternalType('string', $directory, $message);
static::assertTrue(is_dir($directory), $message ? $message : "Failed asserting that $directory exists.");
}

/**
* @param string $directory
* @param string $message
*
* @return void
*/
public static function assertDirectoryNotExists($directory, $message = '')
{
static::assertInternalType('string', $directory, $message);
static::assertFalse(is_dir($directory), $message ? $message : "Failed asserting that $directory does not exist.");
}

/**
* @param string $directory
* @param string $message
*
* @return void
*/
public static function assertDirectoryIsReadable($directory, $message = '')
{
static::assertDirectoryExists($directory, $message);
static::assertIsReadable($directory, $message);
}

/**
* @param string $directory
* @param string $message
*
* @return void
*/
public static function assertDirectoryNotIsReadable($directory, $message = '')
{
static::assertDirectoryExists($directory, $message);
static::assertNotIsReadable($directory, $message);
}

/**
* @param string $directory
* @param string $message
*
* @return void
*/
public static function assertDirectoryIsWritable($directory, $message = '')
{
static::assertDirectoryExists($directory, $message);
static::assertIsWritable($directory, $message);
}

/**
* @param string $directory
* @param string $message
*
* @return void
*/
public static function assertDirectoryNotIsWritable($directory, $message = '')
{
static::assertDirectoryExists($directory, $message);
static::assertNotIsWritable($directory, $message);
}

/**
* @param string $filename
* @param string $message
*
* @return void
*/
public static function assertFileExists($filename, $message = '')
{
static::assertInternalType('string', $filename, $message);
static::assertTrue(file_exists($filename), $message ? $message : "Failed asserting that $filename exists.");
}

/**
* @param string $filename
* @param string $message
*
* @return void
*/
public static function assertFileNotExists($filename, $message = '')
{
static::assertInternalType('string', $filename, $message);
static::assertFalse(file_exists($filename), $message ? $message : "Failed asserting that $filename does not exist.");
}

/**
* @param string $filename
* @param string $message
*
* @return void
*/
public static function assertFileIsReadable($filename, $message = '')
{
static::assertFileExists($filename, $message);
static::assertIsReadable($filename, $message);
}

/**
* @param string $filename
* @param string $message
*
* @return void
*/
public static function assertFileNotIsReadable($filename, $message = '')
{
static::assertFileExists($filename, $message);
static::assertNotIsReadable($filename, $message);
}

/**
* @param string $filename
* @param string $message
*
* @return void
*/
public static function assertFileIsWritable($filename, $message = '')
{
static::assertFileExists($filename, $message);
static::assertIsWritable($filename, $message);
}

/**
* @param string $filename
* @param string $message
*
* @return void
*/
public static function assertFileNotIsWritable($filename, $message = '')
{
static::assertFileExists($filename, $message);
static::assertNotIsWritable($filename, $message);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -528,6 +528,10 @@ private function registerProfilerConfiguration(array $config, ContainerBuilder $
$loader->load('messenger_debug.xml');
}

if (class_exists(Mailer::class)) {
$loader->load('mailer_debug.xml');
}

$container->setParameter('profiler_listener.only_exceptions', $config['only_exceptions']);
$container->setParameter('profiler_listener.only_master_requests', $config['only_master_requests']);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8" ?>

<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">

<services>
<service id="mailer.logger_message_listener" class="Symfony\Component\Mailer\EventListener\MessageLoggerListener">
<tag name="kernel.event_subscriber"/>
</service>
</services>
</container>
1 change: 1 addition & 0 deletions src/Symfony/Component/Mailer/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ CHANGELOG
4.4.0
-----

* Added `MessageEvents` and `MessageLoggerListener` to allow collecting sent emails
* [BC BREAK] `TransportInterface` has a new `getName()` method
* [BC BREAK] Classes `AbstractApiTransport` and `AbstractHttpTransport` moved under `Transport` sub-namespace.
* [BC BREAK] Transports depend on `Symfony\Contracts\EventDispatcher\EventDispatcherInterface`
Expand Down
18 changes: 16 additions & 2 deletions src/Symfony/Component/Mailer/Event/MessageEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,23 @@
use Symfony\Contracts\EventDispatcher\Event;

/**
* Allows the transformation of a Message.
* Allows the transformation of a Message and the SMTP Envelope before the email is sent.
*
* @author Fabien Potencier <fabien@symfony.com>
*/
class MessageEvent extends Event
{
private $message;
private $envelope;
private $transportName;
private $queued;

public function __construct(RawMessage $message, SmtpEnvelope $envelope)
public function __construct(RawMessage $message, SmtpEnvelope $envelope, string $transportName, bool $queued = false)
{
$this->message = $message;
$this->envelope = $envelope;
$this->transportName = $transportName;
$this->queued = $queued;
}

public function getMessage(): RawMessage
Expand All @@ -50,4 +54,14 @@ public function setEnvelope(SmtpEnvelope $envelope): void
{
$this->envelope = $envelope;
}

public function getTransportName(): string
{
return $this->transportName;
}

public function isQueued(): bool
{
return $this->queued;
}
}
51 changes: 51 additions & 0 deletions src/Symfony/Component/Mailer/Event/MessageEvents.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Mailer\Event;

/**
* @author Fabien Potencier <fabien@symfony.com>
*/
class MessageEvents
{
private $events = [];
private $transports = [];

public function add(MessageEvent $event): void
{
$this->events[] = $event;
$this->transports[$event->getTransportName()] = true;
}

public function getTransports(): array
{
return array_keys($this->transports);
}

/**
* @return MessageEvent[]
*/
public function getEvents(string $name = null): array
{
if (null === $name) {
return $this->events;
}

$events = [];
foreach ($this->events as $event) {
if ($name === $event->getTransportName()) {
$events[] = $event;
}
}

return $events;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Mailer\EventListener;

use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Mailer\Event\MessageEvent;
use Symfony\Component\Mailer\Event\MessageEvents;
use Symfony\Contracts\Service\ResetInterface;

/**
* Logs Messages.
*
* @author Fabien Potencier <fabien@symfony.com>
*/
class MessageLoggerListener implements EventSubscriberInterface, ResetInterface
{
private $events;

public function __construct()
{
$this->events = new MessageEvents();
}

/**
* {@inheritdoc}
*/
public function reset()
{
$this->events = new MessageEvents();
}

public function onMessage(MessageEvent $event): void
{
$this->events->add($event);
}

public function getEvents(): MessageEvents
{
return $this->events;
}

public static function getSubscribedEvents()
{
return [
MessageEvent::class => ['onMessage', -255],
];
}
}

0 comments on commit 9d9e8dc

Please sign in to comment.