Completed
Push — master ( e11051...f173c0 )
by mw
10s
created

testExecuteOnNonCachedParseRequest()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 46
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 46
rs 8.9411
cc 1
eloc 31
nc 1
nop 0
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() {
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
		$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 ) {
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...
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