summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/Bootstrap/tests/phpunit/Hooks/SetupAfterCacheTest.php
blob: 7c9d6ea271aea5a21e4fbcc2e9fa96cc7e019ad9 (plain)
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
<?php

namespace Bootstrap\Tests\Hooks;

use Bootstrap\Hooks\SetupAfterCache;

/**
 * @uses \Bootstrap\Hooks\SetupAfterCache
 *
 * @ingroup Test
 *
 * @group extension-bootstrap
 * @group mediawiki-databaseless
 *
 * @license GNU GPL v3+
 * @since 1.0
 *
 * @author mwjames
 */
class SetupAfterCacheTest extends \PHPUnit_Framework_TestCase {

	protected $localBasePath = null;
	protected $localBootstrapVendorPath = null;

	protected function setUp() {
		parent::setUp();
		$this->localBootstrapVendorPath = __DIR__ . '/../../../resources/bootstrap';
	}

	public function testCanConstruct() {

		$configuration = array();

		$this->assertInstanceOf(
			'\Bootstrap\Hooks\SetupAfterCache',
			new SetupAfterCache( $configuration )
		);
	}

	public function testProcessWithAccessibilityOnBootstrapVendorPath() {

		$configuration = array(
			'localBasePath'  => $this->localBootstrapVendorPath,
			'remoteBasePath' => '',
			'IP' => 'someIP',
		);

		$instance = new SetupAfterCache( $configuration );

		$this->assertTrue( $instance->process() );
	}

	public function testProcess_setsDefaultCacheTriggers() {

		$configuration = array(
			'localBasePath'  => $this->localBootstrapVendorPath,
			'remoteBasePath' => '',
			'IP' => 'someIP',
		);

		$this->resetGlobals();

		$instance = new SetupAfterCache( $configuration );

		$this->assertTrue( $instance->process() );

		$this->assertEquals(
			'someIP/LocalSettings.php',
			$GLOBALS[ 'wgResourceModules' ][ 'ext.bootstrap.styles' ][ 'cachetriggers' ][ 'LocalSettings.php' ]
		);

		$this->assertEquals(
			'someIP/composer.lock',
			$GLOBALS[ 'wgResourceModules' ][ 'ext.bootstrap.styles' ][ 'cachetriggers' ][ 'composer.lock' ]
		);
	}

	public function testProcessWithAccessibilityOnAddedLocalResourcePaths() {

		$configuration = array(
			'localBasePath'  => $this->localBootstrapVendorPath,
			'remoteBasePath' => '',
			'IP' => 'someIP',
		);

		$instance = new SetupAfterCache( $configuration );
		$instance->process();

		$this->assertThatPathIsReadable(
			$GLOBALS[ 'wgResourceModules' ][ 'ext.bootstrap.styles' ]['localBasePath']
		);

		$this->assertThatPathIsReadable(
			$GLOBALS[ 'wgResourceModules' ][ 'ext.bootstrap.scripts' ]['localBasePath']
		);
	}

	/**
	 * @dataProvider invalidConfigurationProvider
	 */
	public function testProcessOnInvalidConfigurationThrowsException( $configuration ) {

		$instance = new SetupAfterCache( $configuration );

		$this->setExpectedException( 'InvalidArgumentException' );
		$instance->process();
	}

	public function testProcessOnInvalidLocalPathThrowsException() {

		$configuration = array(
			'localBasePath'  => 'Foo',
			'remoteBasePath' => '',
			'IP' => 'someIP',
		);

		$instance = new SetupAfterCache( $configuration );

		$this->setExpectedException( 'RuntimeException' );
		$instance->process();
	}

	public function invalidConfigurationProvider() {

		$provider = array();

		$provider[] = array(
			array()
		);

		$provider[] = array(
			array(
				'localBasePath' => 'Foo'
			)
		);

		$provider[] = array(
			array(
				'remoteBasePath' => 'Foo'
			)
		);

		return $provider;
	}

	protected function assertThatPathIsReadable( $path ) {
		$this->assertTrue( is_readable( $path ) );
	}

	private function resetGlobals() {
		$GLOBALS[ 'wgResourceModules' ][ 'ext.bootstrap.styles' ] = array (
			'class'         => 'Bootstrap\ResourceLoaderBootstrapModule',
			'styles'        => array (),
			'variables'     => array (),
			'dependencies'  => array (),
			'cachetriggers' => array (
				'LocalSettings.php' => null,
				'composer.lock'     => null,
			),
		);
	}

}