Completed
Push — master ( 0356e8...535bcb )
by mw
17:11
created

testExecuteOnUntouchedTemplate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 51
Code Lines 35

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 51
rs 9.4109
cc 1
eloc 35
nc 1
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace SUC\Tests;
4
5
use SUC\ApiSummaryCardContentParser;
6
use SUC\CacheHelper;
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\ApiSummaryCardContentParser
18
 * @group summary-cards
19
 *
20
 * @license GNU GPL v2+
21
 * @since   1.0
22
 *
23
 * @author mwjames
24
 */
25
class ApiSummaryCardContentParserTest extends \PHPUnit_Framework_TestCase {
26
27
	protected function setUp() {
0 ignored issues
show
Coding Style introduced by
setUp uses the super-global variable $GLOBALS which is generally not recommended.

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:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
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
		$cacheHelper = $this->getMockBuilder( '\SUC\CacheHelper' )
36
			->disableOriginalConstructor()
37
			->getMock();
38
39
		$instance = new ApiSummaryCardContentParser(
40
			$this->newApiMain( array() ),
41
			'summarycards'
42
		);
43
44
		$instance->setCacheHelper( $cacheHelper );
45
46
		$this->assertInstanceOf(
47
			'SUC\ApiSummaryCardContentParser',
48
			$instance
49
		);
50
	}
51
52
	public function testExecuteOnEmptyRequest() {
53
54
		$cacheHelper = $this->getMockBuilder( '\SUC\CacheHelper' )
55
			->disableOriginalConstructor()
56
			->getMock();
57
58
		$instance = new ApiSummaryCardContentParser(
59
			$this->newApiMain( array() ),
60
			'summarycards'
61
		);
62
63
		$instance->setCacheHelper( $cacheHelper );
64
		$instance->execute();
65
66
		$this->assertEquals(
67
			array(
68
				'summarycards' => 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
		$cacheHelper = $this->getMockBuilder( CacheHelper::class )
93
			->disableOriginalConstructor()
94
			->getMock();
95
96
		$cacheHelper->expects( $this->any() )
97
			->method( 'getBlobStore' )
98
			->will( $this->returnValue( $blobStore ) );
99
100
		$cacheHelper->expects( $this->atLeastOnce() )
101
			->method( 'newTitleFromText' )
102
			->will( $this->returnValue( Title::newFromText( __METHOD__ ) ) );
103
104
		$params = array(
105
			'text'  => 'Some text',
106
			'title' => 'Foo'
107
		);
108
109
		$instance = new ApiSummaryCardContentParser(
110
			$this->newApiMain( $params ),
111
			'summarycards'
112
		);
113
114
		$instance->setCacheHelper( $cacheHelper );
115
		$instance->execute();
116
117
		$result = $instance->getResult()->getResultData();
118
119
		$this->assertInternalType(
120
			'float',
121
			$result['summarycards']['time']['parse']
122
		);
123
	}
124
125
	public function testExecuteOnUntouchedTemplate() {
126
127
		$container = $this->getMockBuilder( Container::class )
128
			->disableOriginalConstructor()
129
			->getMock();
130
131
		$container->expects( $this->atLeastOnce() )
132
			->method( 'has' )
133
			->will( $this->returnValue( true ) );
134
135
		$blobStore = $this->getMockBuilder( BlobStore::class )
136
			->disableOriginalConstructor()
137
			->getMock();
138
139
		$blobStore->expects( $this->atLeastOnce() )
140
			->method( 'read' )
141
			->will( $this->returnValue( $container ) );
142
143
		$cacheHelper = $this->getMockBuilder( CacheHelper::class )
144
			->disableOriginalConstructor()
145
			->getMock();
146
147
		$cacheHelper->expects( $this->any() )
148
			->method( 'getBlobStore' )
149
			->will( $this->returnValue( $blobStore ) );
150
151
		$cacheHelper->expects( $this->atLeastOnce() )
152
			->method( 'newTitleFromText' )
153
			->will( $this->returnValue( Title::newFromText( __METHOD__ ) ) );
154
155
		$params = array(
156
			'text'  => 'Some text',
157
			'title' => 'Foo',
158
			'template' => 'Bar'
159
		);
160
161
		$instance = new ApiSummaryCardContentParser(
162
			$this->newApiMain( $params ),
163
			'summarycards'
164
		);
165
166
		$instance->setCacheHelper( $cacheHelper );
167
		$instance->execute();
168
169
		$result = $instance->getResult()->getResultData();
170
171
		$this->assertInternalType(
172
			'float',
173
			$result['summarycards']['time']['cached']
174
		);
175
	}
176
177
	private function newApiMain( array $params ) {
178
		return new ApiMain( $this->newRequestContext( $params ), true );
179
	}
180
181
	private function newRequestContext( $request = array() ) {
182
183
		$context = new RequestContext();
184
185
		if ( $request instanceof WebRequest ) {
0 ignored issues
show
Bug introduced by
The class WebRequest does not exist. Is this class maybe located in a folder that is not analyzed, or in a newer version of your dependencies than listed in your composer.lock/composer.json?
Loading history...
186
			$context->setRequest( $request );
187
		} else {
188
			$context->setRequest( new FauxRequest( $request, true ) );
189
		}
190
191
		$user = $this->getMockBuilder( '\User' )
192
			->disableOriginalConstructor()
193
			->getMock();
194
195
		$context->setUser( $user );
196
197
		return $context;
198
	}
199
200
}
201