-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathControllerTest.php
89 lines (72 loc) · 2.89 KB
/
ControllerTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\Upward\Test;
use Laminas\Http\PhpEnvironment\Request;
use Laminas\Http\Response;
use Magento\Upward\Controller;
use Magento\Upward\DefinitionIterator;
use Mockery;
use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration;
use PHPUnit\Framework\TestCase;
use function BeBat\Verify\verify;
/**
* @runTestsInSeparateProcesses
*/
class ControllerTest extends TestCase
{
use MockeryPHPUnitIntegration;
/**
* @var DefinitionIterator|Mockery\MockInterface
*/
private $mockIterator;
/**
* @var Request|Mockery\MockInterface
*/
private $mockRequest;
protected function setUp(): void
{
$this->mockRequest = Mockery::mock(Request::class)->makePartial();
$this->mockIterator = Mockery::mock('overload:' . DefinitionIterator::class);
$this->mockIterator->shouldReceive('get')
->once()->with('status')->andReturn(200)->byDefault();
$this->mockIterator->shouldReceive('get')
->once()->with('headers')->andReturn(['content-type' => 'text/plain'])->byDefault();
$this->mockIterator->shouldReceive('get')
->once()->with('body')->andReturn('Response Body')->byDefault();
}
public function testInvokeValid(): void
{
$controller = new Controller($this->mockRequest, 'pwa/upward-config-inline.yml');
$response = $controller();
verify($response->getStatusCode())->is()->sameAs(200);
verify($response->getHeaders())->isNot()->empty();
verify($response->getContent())->is()->sameAs('Response Body');
}
public function testInvokeWithException(): void
{
$this->mockIterator->shouldReceive('get')
->once()->with('status')->andThrow(new \RuntimeException('Exception Message'));
$this->mockIterator->shouldReceive('get')->with('status')->never();
$this->mockIterator->shouldReceive('get')->with('headers')->never();
$controller = new Controller($this->mockRequest, 'pwa/upward-config-inline.yml');
$response = $controller();
verify($response->getStatusCode())->is()->sameAs(500);
verify($response->getHeaders())->is()->empty();
verify($response->getContent())->is()->sameAs('{"error":"Exception Message"}');
}
public function testInvokeWithResponse(): void
{
$response = new Response();
$this->mockIterator->shouldReceive('get')
->once()->with('status')->andReturn($response);
$this->mockIterator->shouldReceive('get')->with('status')->never();
$this->mockIterator->shouldReceive('get')->with('headers')->never();
$controller = new Controller($this->mockRequest, 'pwa/upward-config-inline.yml');
$result = $controller();
verify($result)->is()->sameAs($response);
}
}