<p dir="auto"><strong>Description</strong><br>
<a href="https://phpunit.de/announcements/phpunit-8.html" rel="nofollow">PHPUnit 8</a> has just been released. In addition to new features, the following has been deprecated and will be removed in PHPUnit 9:</p>
<ul dir="auto">
<li><code class="notranslate">assertInternalType()</code> and <code class="notranslate">assertNotInternalType()</code></li>
<li><code class="notranslate">assertArraySubset()</code></li>
<li>Annotation(s) for expecting exceptions</li>
<li>Assertions (and helper methods) that operate on (non-public) attributes</li>
<li>Optional parameters of <code class="notranslate">assertEquals()</code> and <code class="notranslate">assertNotEquals()</code></li>
<li><code class="notranslate">TestListener</code> interface</li>
<li>Optional parameters of <code class="notranslate">assertContains()</code> and <code class="notranslate">assertNotContains()</code> as well as using these methods with string haystacks</li>
</ul>
<p dir="auto">Full details at <a href="https://phpunit.de/announcements/phpunit-8.html" rel="nofollow">https://phpunit.de/announcements/phpunit-8.html</a></p>
<p dir="auto">Let's review our tests to make sure we don't use these. Thanks.</p>
<p dir="auto"><strong>Description</strong><br>
Allow to specifying a "message name" which define the type of message that can be handled.</p>
<p dir="auto"><strong>Example</strong></p>
<p dir="auto">Simple use-case where my main message can be extended:</p>
<div class="highlight highlight-text-html-php notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="&lt;?php 

use App\Messenger\MessageInterface;

class RandomMessage implements MessageInterface
{
    protected $name = 'test'

    // getter
}"><pre class="notranslate"><span class="pl-ent">&lt;?php</span> 

<span class="pl-k">use</span> <span class="pl-v">App</span>\<span class="pl-v">Messenger</span>\<span class="pl-v">MessageInterface</span>;

<span class="pl-k">class</span> <span class="pl-v">RandomMessage</span> <span class="pl-k">implements</span> <span class="pl-v">MessageInterface</span>
{
    <span class="pl-k">protected</span> <span class="pl-s1"><span class="pl-c1">$</span>name</span> = <span class="pl-s">'test'</span>

    <span class="pl-c">// getter</span>
}</pre></div>
<p dir="auto">The RandomMessage children:</p>
<div class="highlight highlight-text-html-php notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="&lt;?php 

class NewRandomMessage extends RandomMessage
{
    protected $name = 'newTest'

    // getter
}"><pre class="notranslate"><span class="pl-ent">&lt;?php</span> 

<span class="pl-k">class</span> <span class="pl-v">NewRandomMessage</span> <span class="pl-k">extends</span> <span class="pl-v">RandomMessage</span>
{
    <span class="pl-k">protected</span> <span class="pl-s1"><span class="pl-c1">$</span>name</span> = <span class="pl-s">'newTest'</span>

    <span class="pl-c">// getter</span>
}</pre></div>
<p dir="auto">Now the handler:</p>
<div class="highlight highlight-text-html-php notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="&lt;?php

use App\Messenger\MessageInterface;
use Symfony\Component\Messenger\Handler\MessageHandlerInterface;

class RandomHandler implements MessageHandlerInterface
{
    public static function getHandledMessage(): string
    {
        return 'newTest';
    }

    public function __invoke(MessageInterface $message): void 
    {
        /** $message NewRandomMessage */
        if ($message-&gt;getName() !== static::getHandledMessage()) {
            return;
        }
    }
}"><pre class="notranslate"><span class="pl-ent">&lt;?php</span>

<span class="pl-k">use</span> <span class="pl-v">App</span>\<span class="pl-v">Messenger</span>\<span class="pl-v">MessageInterface</span>;
<span class="pl-k">use</span> <span class="pl-v">Symfony</span>\<span class="pl-v">Component</span>\<span class="pl-v">Messenger</span>\<span class="pl-v">Handler</span>\<span class="pl-v">MessageHandlerInterface</span>;

<span class="pl-k">class</span> <span class="pl-v">RandomHandler</span> <span class="pl-k">implements</span> <span class="pl-v">MessageHandlerInterface</span>
{
    <span class="pl-k">public</span> <span class="pl-k">static</span> <span class="pl-k">function</span> <span class="pl-en">getHandledMessage</span>(): <span class="pl-smi">string</span>
    {
        <span class="pl-k">return</span> <span class="pl-s">'newTest'</span>;
    }

    <span class="pl-k">public</span> <span class="pl-k">function</span> <span class="pl-en">__invoke</span>(<span class="pl-smi"><span class="pl-smi">MessageInterface</span></span> <span class="pl-s1"><span class="pl-c1">$</span>message</span>): <span class="pl-smi">void</span> 
    {
        <span class="pl-c">/** $message NewRandomMessage */</span>
        <span class="pl-k">if</span> (<span class="pl-s1"><span class="pl-c1">$</span>message</span>-&gt;<span class="pl-en">getName</span>() !== <span class="pl-smi"><span class="pl-k">static</span></span>::<span class="pl-en">getHandledMessage</span>()) {
            <span class="pl-k">return</span>;
        }
    }
}</pre></div>
<p dir="auto">This method (the name should be improved but for me, that's like the <code class="notranslate">EventSusbscriberInterface</code> one) can ease the process when a message can be extended, once handled, the message keep the same type in the Handler but the developer can indicate that only this type of message can be handled, this allow to use <code class="notranslate">switch</code> in the same handler in order to keep a small amount of handlers.</p>
<p dir="auto">The <code class="notranslate">MessageInterface::getName()</code> is just for demonstration purpose (it can be a constant or anything else that can be called).</p>
<p dir="auto">Thanks for the feedback.</p>
<p dir="auto"><strong>EDIT: Example for a new interface</strong></p>
<p dir="auto">Here's the new interface that can be added in order to allow this approach:</p>
<div class="highlight highlight-text-html-php notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="&lt;?php

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

namespace Symfony\Component\Messenger\Handler;

interface MessageListenerInterface extends MessageHandlerInterface
{
    /**
     * Returns the name of the message to be handled.
     *
     * It returns a string which contain the name of the message:
     *
     *     return Message::NAME;
     *
     * Or:
     *
     *     return 'test';
     *
     * The `__invoke` method of the handler can use a defined interface.
     */
    public static function getHandledMessage(): string;
}"><pre class="notranslate"><span class="pl-ent">&lt;?php</span>

<span class="pl-c">/*</span>
<span class="pl-c"> * This file is part of the Symfony package.</span>
<span class="pl-c"> *</span>
<span class="pl-c"> * (c) Fabien Potencier &lt;fabien@symfony.com&gt;</span>
<span class="pl-c"> *</span>
<span class="pl-c"> * For the full copyright and license information, please view the LICENSE</span>
<span class="pl-c"> * file that was distributed with this source code.</span>
<span class="pl-c"> */</span>

<span class="pl-k">namespace</span> <span class="pl-v">Symfony</span>\<span class="pl-v">Component</span>\<span class="pl-v">Messenger</span>\<span class="pl-v">Handler</span>;

<span class="pl-k">interface</span> <span class="pl-v">MessageListenerInterface</span> <span class="pl-k">extends</span> <span class="pl-v">MessageHandlerInterface</span>
{
    <span class="pl-c">/**</span>
<span class="pl-c">     * Returns the name of the message to be handled.</span>
<span class="pl-c">     *</span>
<span class="pl-c">     * It returns a string which contain the name of the message:</span>
<span class="pl-c">     *</span>
<span class="pl-c">     *     return Message::NAME;</span>
<span class="pl-c">     *</span>
<span class="pl-c">     * Or:</span>
<span class="pl-c">     *</span>
<span class="pl-c">     *     return 'test';</span>
<span class="pl-c">     *</span>
<span class="pl-c">     * The `__invoke` method of the handler can use a defined interface.</span>
<span class="pl-c">     */</span>
    <span class="pl-k">public</span> <span class="pl-k">static</span> <span class="pl-k">function</span> <span class="pl-en">getHandledMessage</span>(): <span class="pl-smi">string</span>;
}</pre></div>