Skip to content

Commit 8d03546

Browse files
committed
Merge remote-tracking branch 'upstream/6.1' into 6.1
* upstream/6.1: Minor tweak [Kernel] Mention extra interfaces in `MicroKernel` section Mailer: remove port 99 for requestbin.com [Testing] Add a section to mock service dependencies
2 parents 2b1e48a + 6798956 commit 8d03546

File tree

4 files changed

+125
-1
lines changed

4 files changed

+125
-1
lines changed

configuration/micro_kernel_trait.rst

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,44 @@ that define your bundles, your services and your routes:
9999
``RoutingConfigurator`` has methods that make adding routes in PHP more
100100
fun. You can also load external routing files (shown below).
101101

102+
Adding Interfaces to "Micro" Kernel
103+
-----------------------------------
104+
105+
When using the ``MicroKernelTrait``, you can also implement the
106+
``CompilerPassInterface`` to automatically register the kernel itself as a
107+
compiler pass as explained in the dedicated
108+
:ref:`compiler pass section <kernel-as-compiler-pass>`.
109+
110+
It is also possible to implement the ``EventSubscriberInterface`` to handle
111+
events directly from the kernel, again it will be registered automatically::
112+
113+
// ...
114+
use App\Exception\Danger;
115+
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
116+
use Symfony\Component\HttpKernel\Event\ExceptionEvent;
117+
use Symfony\Component\HttpKernel\KernelEvents;
118+
119+
class Kernel extends BaseKernel implements EventSubscriberInterface
120+
{
121+
use MicroKernelTrait;
122+
123+
// ...
124+
125+
public function onKernelException(ExceptionEvent $event): void
126+
{
127+
if ($event->getException() instanceof Danger) {
128+
$event->setResponse(new Response('It\'s dangerous to go alone. Take this ⚔'));
129+
}
130+
}
131+
132+
public static function getSubscribedEvents(): array
133+
{
134+
return [
135+
KernelEvents::EXCEPTION => 'onKernelException',
136+
];
137+
}
138+
}
139+
102140
Advanced Example: Twig, Annotations and the Web Debug Toolbar
103141
-------------------------------------------------------------
104142

mailer.rst

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,6 @@ OhMySMTP ohmysmtp+smtp://API_TOKEN@default n/a
191191
192192
# .env
193193
MAILER_DSN=mailgun+https://KEY:DOMAIN@requestbin.com
194-
MAILER_DSN=mailgun+https://KEY:DOMAIN@requestbin.com:99
195194
196195
Note that the protocol is *always* HTTPs and cannot be changed.
197196

service_container/compiler_passes.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ Compiler passes are registered in the ``build()`` method of the application kern
3232
}
3333
}
3434

35+
.. _kernel-as-compiler-pass:
36+
3537
One of the most common use-cases of compiler passes is to work with :doc:`tagged
3638
services </service_container/tags>`. In those cases, instead of creating a
3739
compiler pass, you can make the kernel implement

testing.rst

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,91 @@ It gives you access to both the public services and the non-removed
281281
are not used by any other services), you need to declare those private
282282
services as public in the ``config/services_test.yaml`` file.
283283

284+
Mocking Dependencies
285+
--------------------
286+
287+
Sometimes it can be useful to mock a dependency of a tested service.
288+
From the example in the previous section, let's assume the
289+
``NewsletterGenerator`` has a dependency to a private alias
290+
``NewsRepositoryInterface`` pointing to a private ``NewsRepository`` service
291+
and you'd like to use a mocked ``NewsRepositoryInterface`` instead of the
292+
concrete one::
293+
294+
// ...
295+
use App\Contracts\Repository\NewsRepositoryInterface;
296+
297+
class NewsletterGeneratorTest extends KernelTestCase
298+
{
299+
public function testSomething()
300+
{
301+
// ... same bootstrap as the section above
302+
303+
$newsRepository = $this->createMock(NewsRepositoryInterface::class);
304+
$newsRepository->expects(self::once())
305+
->method('findNewsFromLastMonth')
306+
->willReturn([
307+
new News('some news'),
308+
new News('some other news'),
309+
])
310+
;
311+
312+
// the following line won't work unless the alias is made public
313+
$container->set(NewsRepositoryInterface::class, $newsRepository);
314+
315+
// will be injected the mocked repository
316+
$newsletterGenerator = $container->get(NewsletterGenerator::class);
317+
318+
// ...
319+
}
320+
}
321+
322+
In order to make the alias public, you will need to update configuration for
323+
the ``test`` environment as follows:
324+
325+
.. configuration-block::
326+
327+
.. code-block:: yaml
328+
329+
# config/services_test.yaml
330+
services:
331+
# redefine the alias as it should be while making it public
332+
App\Contracts\Repository\NewsRepositoryInterface:
333+
alias: App\Repository\NewsRepository
334+
public: true
335+
336+
.. code-block:: xml
337+
338+
<!-- config/services_test.xml -->
339+
<?xml version="1.0" encoding="UTF-8" ?>
340+
<container xmlns="http://symfony.com/schema/dic/services"
341+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
342+
xsi:schemaLocation="http://symfony.com/schema/dic/services
343+
https://symfony.com/schema/dic/services/services-1.0.xsd
344+
">
345+
<services>
346+
<!-- redefine the alias as it should be while making it public -->
347+
<service id="App\Contracts\Repository\NewsRepositoryInterface"
348+
alias="App\Repository\NewsRepository"
349+
/>
350+
</services>
351+
</container>
352+
353+
.. code-block:: php
354+
355+
// config/services_test.php
356+
namespace Symfony\Component\DependencyInjection\Loader\Configurator;
357+
358+
use App\Contracts\Repository\NewsRepositoryInterface;
359+
use App\Repository\NewsRepository;
360+
361+
return static function (ContainerConfigurator $container) {
362+
$container->services()
363+
// redefine the alias as it should be while making it public
364+
->alias(NewsRepositoryInterface::class, NewsRepository::class)
365+
->public()
366+
;
367+
};
368+
284369
.. _testing-databases:
285370

286371
Configuring a Database for Tests

0 commit comments

Comments
 (0)