This repository was archived by the owner on Jun 29, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add WebActionsCaller, ActionCaller and TagRequest tests (#137)
- Loading branch information
Showing
3 changed files
with
147 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |