1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SUC\Tests; |
4
|
|
|
|
5
|
|
|
use SUC\ApiCacheableTemplateParse; |
6
|
|
|
use SUC\BackendCache; |
7
|
|
|
use Onoi\BlobStore\BlobStore; |
8
|
|
|
use Onoi\BlobStore\Container; |
9
|
|
|
use ApiMain; |
10
|
|
|
use ApiResult; |
11
|
|
|
use FauxRequest; |
12
|
|
|
use RequestContext; |
13
|
|
|
use WebRequest; |
14
|
|
|
use Title; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @covers \SUC\ApiCacheableTemplateParse |
18
|
|
|
* @group summary-cards |
19
|
|
|
* |
20
|
|
|
* @license GNU GPL v2+ |
21
|
|
|
* @since 1.0 |
22
|
|
|
* |
23
|
|
|
* @author mwjames |
24
|
|
|
*/ |
25
|
|
|
class ApiCacheableTemplateParseTest extends \PHPUnit_Framework_TestCase { |
26
|
|
|
|
27
|
|
|
protected function setUp() { |
|
|
|
|
28
|
|
|
if ( version_compare( $GLOBALS['wgVersion'], '1.25', '<' ) ) { |
29
|
|
|
$this->markTestSkipped( "Don't test < 1.25 API due to changes in 1.25." ); |
30
|
|
|
} |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
public function testCanConstruct() { |
34
|
|
|
|
35
|
|
|
$backendCache = $this->getMockBuilder( '\SUC\BackendCache' ) |
36
|
|
|
->disableOriginalConstructor() |
37
|
|
|
->getMock(); |
38
|
|
|
|
39
|
|
|
$instance = new ApiCacheableTemplateParse( |
40
|
|
|
$this->newApiMain( array() ), |
41
|
|
|
'ctparse' |
42
|
|
|
); |
43
|
|
|
|
44
|
|
|
$instance->setBackendCache( $backendCache ); |
45
|
|
|
|
46
|
|
|
$this->assertInstanceOf( |
47
|
|
|
'SUC\ApiCacheableTemplateParse', |
48
|
|
|
$instance |
49
|
|
|
); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public function testExecuteOnEmptyRequest() { |
53
|
|
|
|
54
|
|
|
$backendCache = $this->getMockBuilder( '\SUC\BackendCache' ) |
55
|
|
|
->disableOriginalConstructor() |
56
|
|
|
->getMock(); |
57
|
|
|
|
58
|
|
|
$instance = new ApiCacheableTemplateParse( |
59
|
|
|
$this->newApiMain( array() ), |
60
|
|
|
'ctparse' |
61
|
|
|
); |
62
|
|
|
|
63
|
|
|
$instance->setBackendCache( $backendCache ); |
64
|
|
|
$instance->execute(); |
65
|
|
|
|
66
|
|
|
$this->assertEquals( |
67
|
|
|
array( |
68
|
|
|
'ctparse' => array( |
69
|
|
|
'text' => '', |
70
|
|
|
'time' => false |
71
|
|
|
), |
72
|
|
|
'_type' => 'assoc' |
73
|
|
|
), |
74
|
|
|
$instance->getResult()->getResultData() |
75
|
|
|
); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
public function testExecuteOnNonCachedParseRequest() { |
79
|
|
|
|
80
|
|
|
$container = $this->getMockBuilder( Container::class ) |
81
|
|
|
->disableOriginalConstructor() |
82
|
|
|
->getMock(); |
83
|
|
|
|
84
|
|
|
$blobStore = $this->getMockBuilder( BlobStore::class ) |
85
|
|
|
->disableOriginalConstructor() |
86
|
|
|
->getMock(); |
87
|
|
|
|
88
|
|
|
$blobStore->expects( $this->atLeastOnce() ) |
89
|
|
|
->method( 'read' ) |
90
|
|
|
->will( $this->returnValue( $container ) ); |
91
|
|
|
|
92
|
|
|
$backendCache = $this->getMockBuilder( BackendCache::class ) |
93
|
|
|
->disableOriginalConstructor() |
94
|
|
|
->getMock(); |
95
|
|
|
|
96
|
|
|
$backendCache->expects( $this->any() ) |
97
|
|
|
->method( 'getBlobStore' ) |
98
|
|
|
->will( $this->returnValue( $blobStore ) ); |
99
|
|
|
|
100
|
|
|
$backendCache->expects( $this->atLeastOnce() ) |
101
|
|
|
->method( 'getTargetFrom' ) |
102
|
|
|
->will( $this->returnValue( Title::newFromText( __METHOD__ ) ) ); |
103
|
|
|
|
104
|
|
|
$params = array( |
105
|
|
|
'text' => 'Some text', |
106
|
|
|
'title' => 'Foo' |
107
|
|
|
); |
108
|
|
|
|
109
|
|
|
$instance = new ApiCacheableTemplateParse( |
110
|
|
|
$this->newApiMain( $params ), |
111
|
|
|
'ctparse' |
112
|
|
|
); |
113
|
|
|
|
114
|
|
|
$instance->setBackendCache( $backendCache ); |
115
|
|
|
$instance->execute(); |
116
|
|
|
|
117
|
|
|
$result = $instance->getResult()->getResultData(); |
118
|
|
|
|
119
|
|
|
$this->assertInternalType( |
120
|
|
|
'float', |
121
|
|
|
$result['ctparse']['time']['parse'] |
122
|
|
|
); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
private function newApiMain( array $params ) { |
126
|
|
|
return new ApiMain( $this->newRequestContext( $params ), true ); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
private function newRequestContext( $request = array() ) { |
130
|
|
|
|
131
|
|
|
$context = new RequestContext(); |
132
|
|
|
|
133
|
|
|
if ( $request instanceof WebRequest ) { |
|
|
|
|
134
|
|
|
$context->setRequest( $request ); |
135
|
|
|
} else { |
136
|
|
|
$context->setRequest( new FauxRequest( $request, true ) ); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
$user = $this->getMockBuilder( '\User' ) |
140
|
|
|
->disableOriginalConstructor() |
141
|
|
|
->getMock(); |
142
|
|
|
|
143
|
|
|
$context->setUser( $user ); |
144
|
|
|
|
145
|
|
|
return $context; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
} |
149
|
|
|
|
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: