Fix broken recordview introduced with last commit

Relates: #86
This commit is contained in:
Daniel Siepmann 2022-09-16 14:59:54 +02:00
parent 39529be49a
commit f0bb1d03a8
9 changed files with 101 additions and 45 deletions

View file

@ -24,14 +24,11 @@ declare(strict_types=1);
namespace DanielSiepmann\Tracking\Domain\ExpressionLanguage; namespace DanielSiepmann\Tracking\Domain\ExpressionLanguage;
use DanielSiepmann\Tracking\Domain\Model\Expression; use DanielSiepmann\Tracking\Domain\Model\Expression;
use Psr\Http\Message\ServerRequestInterface;
use TYPO3\CMS\Core\Context\Context;
interface Factory interface Factory
{ {
public function create( public function create(
string $expression, string $expression,
ServerRequestInterface $request, array $variables
Context $context
): Expression; ): Expression;
} }

View file

@ -36,18 +36,14 @@ class SymfonyExpressionLanguage implements Factory
{ {
public function create( public function create(
string $expression, string $expression,
ServerRequestInterface $request, array $variables
Context $context
): Expression { ): Expression {
$language = new ExpressionLanguage(); $language = new ExpressionLanguage();
$this->registerTraverseFunction($language); $this->registerTraverseFunction($language);
return new SymfonyExpression( return new SymfonyExpression(
$expression, $expression,
[ $variables,
'context' => $context,
'request' => $request,
],
$language $language
); );
} }

View file

@ -41,13 +41,13 @@ class Factory
$this->siteFinder = $siteFinder; $this->siteFinder = $siteFinder;
} }
public static function fromRequest(ServerRequestInterface $request): Pageview public function fromRequest(ServerRequestInterface $request): Pageview
{ {
return new Pageview( return new Pageview(
self::getRouting($request)->getPageId(), $this->getRouting($request)->getPageId(),
self::getLanguage($request), $this->getLanguage($request),
new \DateTimeImmutable(), new \DateTimeImmutable(),
(int) self::getRouting($request)->getPageType(), (int) $this->getRouting($request)->getPageType(),
(string) $request->getUri(), (string) $request->getUri(),
$request->getHeader('User-Agent')[0] ?? '' $request->getHeader('User-Agent')[0] ?? ''
); );
@ -66,7 +66,7 @@ class Factory
); );
} }
private static function getLanguage(ServerRequestInterface $request): SiteLanguage private function getLanguage(ServerRequestInterface $request): SiteLanguage
{ {
$language = $request->getAttribute('language'); $language = $request->getAttribute('language');
@ -77,7 +77,7 @@ class Factory
return $language; return $language;
} }
private static function getRouting(ServerRequestInterface $request): PageArguments private function getRouting(ServerRequestInterface $request): PageArguments
{ {
$routing = $request->getAttribute('routing'); $routing = $request->getAttribute('routing');

View file

@ -23,6 +23,7 @@ declare(strict_types=1);
namespace DanielSiepmann\Tracking\Domain\Recordview; namespace DanielSiepmann\Tracking\Domain\Recordview;
use DanielSiepmann\Tracking\Domain\ExpressionLanguage\Factory as ExpressionFactory;
use DanielSiepmann\Tracking\Domain\Model\RecordRule; use DanielSiepmann\Tracking\Domain\Model\RecordRule;
use DanielSiepmann\Tracking\Domain\Model\Recordview; use DanielSiepmann\Tracking\Domain\Model\Recordview;
use Psr\Http\Message\ServerRequestInterface; use Psr\Http\Message\ServerRequestInterface;
@ -32,15 +33,25 @@ use TYPO3\CMS\Core\Site\Entity\SiteLanguage;
class Factory class Factory
{ {
public static function fromRequest( /**
* @var ExpressionFactory
*/
private $expressionFactory;
public function __construct(
ExpressionFactory $expressionFactory
) {
$this->expressionFactory = $expressionFactory;
}
public function fromRequest(
ServerRequestInterface $request, ServerRequestInterface $request,
RecordRule $rule RecordRule $rule
): Recordview { ): Recordview {
// Need silent, as expression language doens't provide a way to check for array keys $recordUid = $this->expressionFactory->create(
$recordUid = @(new ExpressionLanguage())->evaluate(
$rule->getUidExpression(), $rule->getUidExpression(),
['request' => $request] ['request' => $request]
); )->evaluate();
if (is_numeric($recordUid) === false) { if (is_numeric($recordUid) === false) {
throw new \UnexpectedValueException( throw new \UnexpectedValueException(

View file

@ -44,6 +44,11 @@ class Pageview implements MiddlewareInterface
*/ */
private $context; private $context;
/**
* @var Factory
*/
private $factory;
/** /**
* @var ExpressionFactory * @var ExpressionFactory
*/ */
@ -57,11 +62,13 @@ class Pageview implements MiddlewareInterface
public function __construct( public function __construct(
Repository $repository, Repository $repository,
Context $context, Context $context,
Factory $factory,
ExpressionFactory $expressionFactory, ExpressionFactory $expressionFactory,
string $rule string $rule
) { ) {
$this->repository = $repository; $this->repository = $repository;
$this->context = $context; $this->context = $context;
$this->factory = $factory;
$this->expressionFactory = $expressionFactory; $this->expressionFactory = $expressionFactory;
$this->rule = $rule; $this->rule = $rule;
} }
@ -71,7 +78,7 @@ class Pageview implements MiddlewareInterface
RequestHandlerInterface $handler RequestHandlerInterface $handler
): ResponseInterface { ): ResponseInterface {
if ($this->shouldTrack($request, $this->context)) { if ($this->shouldTrack($request, $this->context)) {
$this->repository->add(Factory::fromRequest($request)); $this->repository->add($this->factory->fromRequest($request));
} }
return $handler->handle($request); return $handler->handle($request);
@ -83,8 +90,10 @@ class Pageview implements MiddlewareInterface
): bool { ): bool {
return (bool) $this->expressionFactory->create( return (bool) $this->expressionFactory->create(
$this->rule, $this->rule,
$request, [
$context 'request' => $request,
'context' => $context,
]
)->evaluate(); )->evaluate();
} }
} }

View file

@ -46,6 +46,11 @@ class Recordview implements MiddlewareInterface
*/ */
private $context; private $context;
/**
* @var Factory
*/
private $factory;
/** /**
* @var ExpressionFactory * @var ExpressionFactory
*/ */
@ -59,11 +64,13 @@ class Recordview implements MiddlewareInterface
public function __construct( public function __construct(
Repository $repository, Repository $repository,
Context $context, Context $context,
Factory $factory,
ExpressionFactory $expressionFactory, ExpressionFactory $expressionFactory,
array $rules array $rules
) { ) {
$this->repository = $repository; $this->repository = $repository;
$this->context = $context; $this->context = $context;
$this->factory = $factory;
$this->expressionFactory = $expressionFactory; $this->expressionFactory = $expressionFactory;
$this->rules = RecordRule::multipleFromArray($rules); $this->rules = RecordRule::multipleFromArray($rules);
@ -75,7 +82,7 @@ class Recordview implements MiddlewareInterface
): ResponseInterface { ): ResponseInterface {
foreach ($this->rules as $rule) { foreach ($this->rules as $rule) {
if ($this->shouldTrack($request, $this->context, $rule)) { if ($this->shouldTrack($request, $this->context, $rule)) {
$this->repository->add(Factory::fromRequest($request, $rule)); $this->repository->add($this->factory->fromRequest($request, $rule));
} }
} }
@ -89,8 +96,10 @@ class Recordview implements MiddlewareInterface
): bool { ): bool {
return (bool) $this->expressionFactory->create( return (bool) $this->expressionFactory->create(
$rule->getMatchesExpression(), $rule->getMatchesExpression(),
$request, [
$context 'request' => $request,
'context' => $context,
]
)->evaluate(); )->evaluate();
} }
} }

View file

@ -28,15 +28,19 @@ use Prophecy\PhpUnit\ProphecyTrait;
use Psr\Http\Message\ServerRequestInterface; use Psr\Http\Message\ServerRequestInterface;
use TYPO3\CMS\Core\Routing\PageArguments; use TYPO3\CMS\Core\Routing\PageArguments;
use TYPO3\CMS\Core\Site\Entity\SiteLanguage; use TYPO3\CMS\Core\Site\Entity\SiteLanguage;
use TYPO3\TestingFramework\Core\Unit\UnitTestCase as TestCase; use TYPO3\TestingFramework\Core\Functional\FunctionalTestCase;
/** /**
* @covers DanielSiepmann\Tracking\Domain\Recordview\Factory * @covers DanielSiepmann\Tracking\Domain\Recordview\Factory
*/ */
class FactoryTest extends TestCase class FactoryTest extends FunctionalTestCase
{ {
use ProphecyTrait; use ProphecyTrait;
protected $testExtensionsToLoad = [
'typo3conf/ext/tracking',
];
/** /**
* @test * @test
*/ */
@ -60,7 +64,9 @@ class FactoryTest extends TestCase
'category' => 10, 'category' => 10,
]); ]);
$result = Factory::fromRequest($request->reveal(), $rule->reveal()); $subject = $this->get(Factory::class);
$result = $subject->fromRequest($request->reveal(), $rule->reveal());
static::assertInstanceOf(Recordview::class, $result); static::assertInstanceOf(Recordview::class, $result);
} }
@ -87,7 +93,9 @@ class FactoryTest extends TestCase
'category' => 10, 'category' => 10,
]); ]);
$result = Factory::fromRequest($request->reveal(), $rule->reveal()); $subject = $this->get(Factory::class);
$result = $subject->fromRequest($request->reveal(), $rule->reveal());
static::assertSame('Some User Agent', $result->getUserAgent()); static::assertSame('Some User Agent', $result->getUserAgent());
} }
@ -114,7 +122,9 @@ class FactoryTest extends TestCase
'category' => 10, 'category' => 10,
]); ]);
$result = Factory::fromRequest($request->reveal(), $rule->reveal()); $subject = $this->get(Factory::class);
$result = $subject->fromRequest($request->reveal(), $rule->reveal());
static::assertSame('https://example.com', $result->getUrl()); static::assertSame('https://example.com', $result->getUrl());
} }
@ -141,7 +151,9 @@ class FactoryTest extends TestCase
'category' => 10, 'category' => 10,
]); ]);
$result = Factory::fromRequest($request->reveal(), $rule->reveal()); $subject = $this->get(Factory::class);
$result = $subject->fromRequest($request->reveal(), $rule->reveal());
static::assertInstanceOf(\DateTimeImmutable::class, $result->getCrdate()); static::assertInstanceOf(\DateTimeImmutable::class, $result->getCrdate());
} }
@ -168,7 +180,9 @@ class FactoryTest extends TestCase
'category' => 10, 'category' => 10,
]); ]);
$result = Factory::fromRequest($request->reveal(), $rule->reveal()); $subject = $this->get(Factory::class);
$result = $subject->fromRequest($request->reveal(), $rule->reveal());
static::assertSame($language->reveal(), $result->getLanguage()); static::assertSame($language->reveal(), $result->getLanguage());
} }
@ -195,7 +209,9 @@ class FactoryTest extends TestCase
'category' => 10, 'category' => 10,
]); ]);
$result = Factory::fromRequest($request->reveal(), $rule->reveal()); $subject = $this->get(Factory::class);
$result = $subject->fromRequest($request->reveal(), $rule->reveal());
static::assertSame(10, $result->getPageUid()); static::assertSame(10, $result->getPageUid());
} }
@ -222,7 +238,9 @@ class FactoryTest extends TestCase
'category' => 20, 'category' => 20,
]); ]);
$result = Factory::fromRequest($request->reveal(), $rule->reveal()); $subject = $this->get(Factory::class);
$result = $subject->fromRequest($request->reveal(), $rule->reveal());
static::assertSame(20, $result->getRecordUid()); static::assertSame(20, $result->getRecordUid());
} }
@ -249,7 +267,9 @@ class FactoryTest extends TestCase
'category' => 20, 'category' => 20,
]); ]);
$result = Factory::fromRequest($request->reveal(), $rule->reveal()); $subject = $this->get(Factory::class);
$result = $subject->fromRequest($request->reveal(), $rule->reveal());
static::assertSame('sys_category', $result->getTableName()); static::assertSame('sys_category', $result->getTableName());
} }
} }

View file

@ -9,6 +9,6 @@ services:
arguments: arguments:
$rules: $rules:
topics: topics:
matches: 'request.getQueryParams()["topic_id"] > 0' matches: 'traverse(request.getQueryParams(), "topic_id") > 0'
recordUid: 'request.getQueryParams()["topic_id"]' recordUid: 'traverse(request.getQueryParams(), "topic_id")'
tableName: 'sys_category' tableName: 'sys_category'

View file

@ -56,7 +56,9 @@ class FactoryTest extends TestCase
$request->getUri()->willReturn(''); $request->getUri()->willReturn('');
$request->getHeader('User-Agent')->willReturn([]); $request->getHeader('User-Agent')->willReturn([]);
$result = Factory::fromRequest($request->reveal()); $subject = new Factory($this->prophesize(SiteFinder::class)->reveal());
$result = $subject->fromRequest($request->reveal());
static::assertInstanceOf(Pageview::class, $result); static::assertInstanceOf(Pageview::class, $result);
} }
@ -79,7 +81,9 @@ class FactoryTest extends TestCase
'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:74.0) Gecko/20100101 Firefox/74.0' 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:74.0) Gecko/20100101 Firefox/74.0'
]); ]);
$result = Factory::fromRequest($request->reveal()); $subject = new Factory($this->prophesize(SiteFinder::class)->reveal());
$result = $subject->fromRequest($request->reveal());
static::assertSame( static::assertSame(
'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:74.0) Gecko/20100101 Firefox/74.0', 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:74.0) Gecko/20100101 Firefox/74.0',
$result->getUserAgent() $result->getUserAgent()
@ -103,7 +107,9 @@ class FactoryTest extends TestCase
$request->getUri()->willReturn('https://example.com/path?query=params&some=more#anchor'); $request->getUri()->willReturn('https://example.com/path?query=params&some=more#anchor');
$request->getHeader('User-Agent')->willReturn([]); $request->getHeader('User-Agent')->willReturn([]);
$result = Factory::fromRequest($request->reveal()); $subject = new Factory($this->prophesize(SiteFinder::class)->reveal());
$result = $subject->fromRequest($request->reveal());
static::assertSame( static::assertSame(
'https://example.com/path?query=params&some=more#anchor', 'https://example.com/path?query=params&some=more#anchor',
$result->getUrl() $result->getUrl()
@ -127,7 +133,9 @@ class FactoryTest extends TestCase
$request->getUri()->willReturn(''); $request->getUri()->willReturn('');
$request->getHeader('User-Agent')->willReturn([]); $request->getHeader('User-Agent')->willReturn([]);
$result = Factory::fromRequest($request->reveal()); $subject = new Factory($this->prophesize(SiteFinder::class)->reveal());
$result = $subject->fromRequest($request->reveal());
static::assertSame( static::assertSame(
50, 50,
$result->getPageType() $result->getPageType()
@ -151,7 +159,9 @@ class FactoryTest extends TestCase
$request->getUri()->willReturn(''); $request->getUri()->willReturn('');
$request->getHeader('User-Agent')->willReturn([]); $request->getHeader('User-Agent')->willReturn([]);
$result = Factory::fromRequest($request->reveal()); $subject = new Factory($this->prophesize(SiteFinder::class)->reveal());
$result = $subject->fromRequest($request->reveal());
static::assertInstanceOf(\DateTimeImmutable::class, $result->getCrdate()); static::assertInstanceOf(\DateTimeImmutable::class, $result->getCrdate());
} }
@ -172,7 +182,9 @@ class FactoryTest extends TestCase
$request->getUri()->willReturn(''); $request->getUri()->willReturn('');
$request->getHeader('User-Agent')->willReturn([]); $request->getHeader('User-Agent')->willReturn([]);
$result = Factory::fromRequest($request->reveal()); $subject = new Factory($this->prophesize(SiteFinder::class)->reveal());
$result = $subject->fromRequest($request->reveal());
static::assertInstanceOf(SiteLanguage::class, $result->getLanguage()); static::assertInstanceOf(SiteLanguage::class, $result->getLanguage());
} }
@ -193,7 +205,9 @@ class FactoryTest extends TestCase
$request->getUri()->willReturn(''); $request->getUri()->willReturn('');
$request->getHeader('User-Agent')->willReturn([]); $request->getHeader('User-Agent')->willReturn([]);
$result = Factory::fromRequest($request->reveal()); $subject = new Factory($this->prophesize(SiteFinder::class)->reveal());
$result = $subject->fromRequest($request->reveal());
static::assertSame( static::assertSame(
10, 10,
$result->getPageUid() $result->getPageUid()