Skip to content
This repository was archived by the owner on Jun 29, 2022. It is now read-only.

Commit

Permalink
Add WebActionsCaller, ActionCaller and TagRequest tests (#137)
Browse files Browse the repository at this point in the history
  • Loading branch information
zhukovra authored and devanych committed Feb 1, 2021
1 parent 709149e commit 22fb8e7
Show file tree
Hide file tree
Showing 3 changed files with 147 additions and 0 deletions.
44 changes: 44 additions & 0 deletions tests/Middleware/ActionCallerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

namespace Yiisoft\Yii\Web\Tests\Middleware;

use Nyholm\Psr7\Response;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Yiisoft\Di\Container;
use Yiisoft\Yii\Web\Middleware\ActionCaller;
use PHPUnit\Framework\TestCase;

class ActionCallerTest extends TestCase
{
/** @var ServerRequestInterface */
private $request;

/** @var RequestHandlerInterface */
private $handler;

protected function setUp()
{
$this->request = $this->createMock(ServerRequestInterface::class);
$this->handler = $this->createMock(RequestHandlerInterface::class);
}

public function testProcess()
{
$container = new Container([self::class => $this]);

$caller = new ActionCaller(self::class, 'process', $container);

$response = $caller->process($this->request, $this->handler);
self::assertEquals(204, $response->getStatusCode());
}

public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{
$this->assertSame($this->request, $request);
$this->assertSame($this->handler, $handler);

return new Response(204);
}
}
29 changes: 29 additions & 0 deletions tests/Middleware/TagRequestTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace Yiisoft\Yii\Web\Tests\Middleware;

use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Yiisoft\Yii\Web\Middleware\TagRequest;
use PHPUnit\Framework\TestCase;

class TagRequestTest extends TestCase
{
public function testProcess()
{
$request = $this->createMock(ServerRequestInterface::class);

$request
->expects($this->once())
->method('withAttribute')
->with(
$this->equalTo('requestTag'),
$this->isType('string')
)
->willReturnSelf();

$handler = $this->createMock(RequestHandlerInterface::class);

(new TagRequest())->process($request, $handler);
}
}
74 changes: 74 additions & 0 deletions tests/Middleware/WebActionsCallerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php

namespace Yiisoft\Yii\Web\Middleware;

use Nyholm\Psr7\Response;
use PHPUnit\Framework\TestCase;
use Psr\Container\ContainerInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Yiisoft\Di\Container;

class WebActionsCallerTest extends TestCase
{
/** @var ServerRequestInterface */
private $request;

/** @var RequestHandlerInterface */
private $handler;

/** @var ContainerInterface */
private $container;

protected function setUp()
{
$this->request = $this->createMock(ServerRequestInterface::class);
$this->handler = $this->createMock(RequestHandlerInterface::class);
$this->container = new Container([self::class => $this]);
}

public function testProcess()
{
$this->request
->method('getAttribute')
->with($this->equalTo('action'))
->willReturn('process');

$response = (new WebActionsCaller(self::class, $this->container))->process($this->request, $this->handler);
$this->assertEquals(204, $response->getStatusCode());
}

public function testExceptionOnNullAction()
{
$this->request
->method('getAttribute')
->with($this->equalTo('action'))
->willReturn(null);

$this->expectException(\RuntimeException::class);
(new WebActionsCaller(self::class, $this->container))->process($this->request, $this->handler);
}

public function testHandlerInvocation()
{
$this->request
->method('getAttribute')
->with($this->equalTo('action'))
->willReturn('notExistant');

$this->handler
->expects($this->once())
->method('handle');

(new WebActionsCaller(self::class, $this->container))->process($this->request, $this->handler);
}

public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{
$this->assertSame($this->request, $request);
$this->assertSame($this->handler, $handler);

return new Response(204);
}
}

0 comments on commit 22fb8e7

Please sign in to comment.