1
|
|
|
<?php |
2
|
|
|
namespace FwlibTest\Web; |
3
|
|
|
|
4
|
|
|
use Fwlib\Util\Common\HttpUtil; |
5
|
|
|
use Fwlib\Util\UtilContainer; |
6
|
|
|
use Fwlib\Web\RequestTrait; |
7
|
|
|
use Fwolf\Wrapper\PHPUnit\PHPUnitTestCase; |
8
|
|
|
use PHPUnit_Framework_MockObject_MockObject as MockObject; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* @copyright Copyright 2015 Fwolf |
12
|
|
|
* @license http://www.gnu.org/licenses/lgpl.html LGPL-3.0+ |
13
|
|
|
*/ |
14
|
|
|
class RequestTraitTest extends PHPUnitTestCase |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var string |
18
|
|
|
*/ |
19
|
|
|
protected static $getGet = ''; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var HttpUtil |
23
|
|
|
*/ |
24
|
|
|
protected static $httpUtilBackup = null; |
25
|
|
|
|
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @return MockObject | RequestTrait |
29
|
|
|
*/ |
30
|
|
|
protected function buildMock() |
31
|
|
|
{ |
32
|
|
|
$mock = $this->getMockBuilder(RequestTrait::class) |
33
|
|
|
->disableOriginalConstructor() |
34
|
|
|
->getMockForTrait(); |
35
|
|
|
|
36
|
|
|
/** @noinspection PhpUndefinedFieldInspection */ |
37
|
|
|
{ |
38
|
|
|
$mock->actionParameter = 'a'; |
|
|
|
|
39
|
|
|
$mock->moduleParameter = 'm'; |
|
|
|
|
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
return $mock; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
|
46
|
|
View Code Duplication |
public static function setUpBeforeClass() |
|
|
|
|
47
|
|
|
{ |
48
|
|
|
$utilContainer = UtilContainer::getInstance(); |
49
|
|
|
self::$httpUtilBackup = $utilContainer->getHttp(); |
50
|
|
|
|
51
|
|
|
$testCase = new self; |
52
|
|
|
$httpUtil = $testCase->getMock( |
53
|
|
|
HttpUtil::class, |
54
|
|
|
['getGet'] |
55
|
|
|
); |
56
|
|
|
$httpUtil->expects($testCase->any()) |
57
|
|
|
->method('getGet') |
58
|
|
|
->willReturnCallback(function () { |
59
|
|
|
return RequestTraitTest::$getGet; |
60
|
|
|
}); |
61
|
|
|
|
62
|
|
|
$utilContainer->register('Http', $httpUtil); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
|
66
|
|
|
public static function tearDownAfterClass() |
67
|
|
|
{ |
68
|
|
|
$utilContainer = UtilContainer::getInstance(); |
69
|
|
|
|
70
|
|
|
$utilContainer->register('Http', self::$httpUtilBackup); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
|
74
|
|
|
public function testAccessors() |
75
|
|
|
{ |
76
|
|
|
$request = $this->buildMock(); |
77
|
|
|
|
78
|
|
|
self::$getGet = 'foo'; |
79
|
|
|
$this->assertEquals('foo', $request->getAction()); |
80
|
|
|
|
81
|
|
|
$request->setAction('foo1'); |
82
|
|
|
$this->assertEquals('foo1', $request->getAction()); |
83
|
|
|
|
84
|
|
|
self::$getGet = 'bar'; |
85
|
|
|
$this->assertEquals('bar', $request->getModule()); |
86
|
|
|
|
87
|
|
|
$request->setModule('bar1'); |
88
|
|
|
$this->assertEquals('bar1', $request->getModule()); |
89
|
|
|
|
90
|
|
|
|
91
|
|
|
// Action and module parameter |
92
|
|
|
$request->setActionParameter('aa'); |
93
|
|
|
$this->assertEquals('aa', $request->getActionParameter()); |
94
|
|
|
|
95
|
|
|
$request->setModuleParameter('mm'); |
96
|
|
|
$this->assertEquals('mm', $request->getModuleParameter()); |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
If you access a property on an interface, you most likely code against a concrete implementation of the interface.
Available Fixes
Adding an additional type check:
Changing the type hint: