Skip to content

Commit

Permalink
add IPresenterTesterListener
Browse files Browse the repository at this point in the history
  • Loading branch information
matej21 committed Mar 30, 2019
1 parent 53c76db commit 3ede106
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 3 deletions.
19 changes: 16 additions & 3 deletions src/Bridges/Infrastructure/PresenterTesterExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
namespace Mangoweb\Tester\PresenterTester\Bridges\Infrastructure;

use Mangoweb\Tester\Infrastructure\MangoTesterExtension;
use Mangoweb\Tester\PresenterTester\IPresenterTesterListener;
use Mangoweb\Tester\PresenterTester\PresenterTester;
use Nette\Application\Application;
use Nette\Application\IPresenterFactory;
use Nette\Application\IRouter;
use Nette\DI\CompilerExtension;
Expand All @@ -24,11 +26,8 @@ public function loadConfiguration()
{
$builder = $this->getContainerBuilder();

$config = $this->validateConfig($this->defaults);

$builder->addDefinition($this->prefix('presenterTester'))
->setClass(PresenterTester::class)
->setArguments(['baseUrl' => $config['baseUrl'], 'identityFactory' => $config['identityFactory']])
->addSetup(new Statement('?->? = ?',
[
$this->prefix('@presenterTesterTearDown'),
Expand All @@ -43,6 +42,20 @@ public function loadConfiguration()
$this->requireService(IRouter::class);
$this->requireService(IRequest::class);
$this->requireService(Session::class);
$this->requireService(Application::class);
}


public function beforeCompile()
{
$config = $this->validateConfig($this->defaults);
$builder = $this->getContainerBuilder();;
$builder->getDefinition($this->prefix('presenterTester'))
->setArguments([
'baseUrl' => $config['baseUrl'],
'identityFactory' => $config['identityFactory'],
'listeners' => $builder->findByType(IPresenterTesterListener::class),
]);
}


Expand Down
10 changes: 10 additions & 0 deletions src/IPresenterTesterListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php declare(strict_types = 1);

namespace Mangoweb\Tester\PresenterTester;

interface IPresenterTesterListener
{
public function onRequest(TestPresenterRequest $request): TestPresenterRequest;

public function onResult(TestPresenterResult $result): void;
}
14 changes: 14 additions & 0 deletions src/PresenterTester.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,20 +36,27 @@ class PresenterTester
/** @var User */
private $user;

/** @var IPresenterTesterListener[] */
private $listeners;

/** @var callable|NULL */
private $identityFactory;

/** @var TestPresenterResult[] */
private $results = [];


/**
* @param IPresenterTesterListener[] $listeners
*/
public function __construct(
string $baseUrl,
Session $session,
IPresenterFactory $presenterFactory,
IRouter $router,
IRequest $httpRequest,
User $user,
array $listeners = [],
callable $identityFactory = null
)
{
Expand All @@ -61,12 +68,16 @@ public function __construct(
$this->httpRequest = $httpRequest;
$this->baseUrl = $baseUrl;
$this->user = $user;
$this->listeners = $listeners;
$this->identityFactory = $identityFactory;
}


public function execute(TestPresenterRequest $testRequest): TestPresenterResult
{
foreach ($this->listeners as $listener) {
$testRequest = $listener->onRequest($testRequest);
}
$applicationRequest = $this->createApplicationRequest($testRequest);
$presenter = $this->createPresenter($testRequest);
if ($applicationRequest->getMethod() === 'GET') {
Expand All @@ -93,6 +104,9 @@ public function execute(TestPresenterRequest $testRequest): TestPresenterResult
}

$result = new TestPresenterResult($this->router, $applicationRequest, $presenter, $response, $badRequestException);
foreach ($this->listeners as $listener) {
$listener->onResult($result);
}
$this->results[] = $result;

return $result;
Expand Down

0 comments on commit 3ede106

Please sign in to comment.