Skip to content

Commit

Permalink
Extract MatomoRuntime
Browse files Browse the repository at this point in the history
  • Loading branch information
core23 committed Oct 12, 2023
1 parent 52622b7 commit a1c3283
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 79 deletions.
8 changes: 4 additions & 4 deletions src/Resources/config/twig.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@
namespace Symfony\Component\DependencyInjection\Loader\Configurator;

use Nucleos\MatomoBundle\Twig\Extension\MatomoTwigExtension;
use Symfony\Component\DependencyInjection\Reference;
use Nucleos\MatomoBundle\Twig\Runtime\MatomoRuntime;

return static function (ContainerConfigurator $container): void {
$container->services()

->set(MatomoTwigExtension::class)
->tag('twig.extension')
->args([
new Reference('twig'),
])

->set(MatomoRuntime::class)
->tag('twig.runtime')
;
};
39 changes: 4 additions & 35 deletions src/Twig/Extension/MatomoTwigExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,50 +11,19 @@

namespace Nucleos\MatomoBundle\Twig\Extension;

use Twig\Environment;
use Twig\Error\LoaderError;
use Twig\Error\RuntimeError;
use Twig\Error\SyntaxError;
use Nucleos\MatomoBundle\Twig\Runtime\MatomoRuntime;
use Twig\Extension\AbstractExtension;
use Twig\TwigFunction;

final class MatomoTwigExtension extends AbstractExtension
{
private Environment $environment;

public function __construct(Environment $environment)
{
$this->environment = $environment;
}

public function getFunctions(): array
{
return [
new TwigFunction('matomo_tracker', [$this, 'renderTracker'], [
'is_safe' => ['html'],
new TwigFunction('matomo_tracker', [MatomoRuntime::class, 'renderTracker'], [
'needs_environment' => true,
'is_safe' => ['html'],
]),
];
}

/**
* @param array<string, mixed> $options
*
* @throws LoaderError
* @throws RuntimeError
* @throws SyntaxError
*/
public function renderTracker(array $options = []): string
{
$data = array_merge([
'site_id' => null,
'matomo_host' => 'localhost',
'cookie_domain' => null,
], $options);

if (null === $data['site_id']) {
return '';
}

return $this->environment->render('@NucleosMatomo/tracker_code.html.twig', $data);
}
}
34 changes: 34 additions & 0 deletions src/Twig/Runtime/MatomoRuntime.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

/*
* (c) Christian Gripp <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Nucleos\MatomoBundle\Twig\Runtime;

use Twig\Environment;
use Twig\Extension\RuntimeExtensionInterface;

final class MatomoRuntime implements RuntimeExtensionInterface
{
/**
* @param array<string, mixed> $options
*/
public function renderTracker(Environment $environment, array $options = []): string
{
$data = array_merge([
'site_id' => null,
'matomo_host' => 'localhost',
'cookie_domain' => null,
], $options);

if (null === $data['site_id']) {
return '';
}

return $environment->render('@NucleosMatomo/tracker_code.html.twig', $data);
}
}
43 changes: 3 additions & 40 deletions tests/Twig/Extension/MatomoTwigExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,60 +12,23 @@
namespace Nucleos\MatomoBundle\Tests\Twig\Extension;

use Nucleos\MatomoBundle\Twig\Extension\MatomoTwigExtension;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Twig\Environment;
use Twig\TwigFunction;

final class MatomoTwigExtensionTest extends TestCase
{
/**
* @var Environment&MockObject
*/
private Environment $environment;

protected function setUp(): void
{
$this->environment = $this->createMock(Environment::class);
}

public function testGetFunctions(): void
{
$extension = new MatomoTwigExtension($this->environment);
$extension = new MatomoTwigExtension();

$functions = $extension->getFunctions();

static::assertCount(1, $functions);

foreach ($functions as $function) {
static::assertInstanceOf(TwigFunction::class, $function);
static::assertIsCallable($function->getCallable());
static::assertIsArray($callable = $function->getCallable());
static::assertTrue(method_exists($callable[0], $callable[1]));
}
}

public function testRenderTracker(): void
{
$this->environment->method('render')->with('@NucleosMatomo/tracker_code.html.twig', [
'site_id' => 13,
'matomo_host' => 'localhost',
'cookie_domain' => null,
])
->willReturn('HTML CONTENT')
;

$extension = new MatomoTwigExtension($this->environment);

static::assertSame('HTML CONTENT', $extension->renderTracker([
'site_id' => 13,
]));
}

public function testRenderTrackerWithoutSiteId(): void
{
$extension = new MatomoTwigExtension($this->environment);

$this->environment->expects(static::never())->method('render');

static::assertSame('', $extension->renderTracker());
}
}
55 changes: 55 additions & 0 deletions tests/Twig/Runtime/MatomoRuntimeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

declare(strict_types=1);

/*
* (c) Christian Gripp <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Nucleos\MatomoBundle\Tests\Twig\Runtime;

use Nucleos\MatomoBundle\Twig\Runtime\MatomoRuntime;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Twig\Environment;

final class MatomoRuntimeTest extends TestCase
{
/**
* @var Environment&MockObject
*/
private Environment $environment;

private MatomoRuntime $runtime;

protected function setUp(): void
{
$this->environment = $this->createMock(Environment::class);
$this->runtime = new MatomoRuntime();
}

public function testRenderTracker(): void
{
$this->environment->method('render')->with('@NucleosMatomo/tracker_code.html.twig', [
'site_id' => 13,
'matomo_host' => 'localhost',
'cookie_domain' => null,
])
->willReturn('HTML CONTENT')
;

static::assertSame('HTML CONTENT', $this->runtime->renderTracker($this->environment, [
'site_id' => 13,
]));
}

public function testRenderTrackerWithoutSiteId(): void
{
$this->environment->expects(static::never())->method('render');

static::assertSame('', $this->runtime->renderTracker($this->environment));
}
}

0 comments on commit a1c3283

Please sign in to comment.