AutoUpdateTest::setUpBeforeClass()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
nc 2
nop 0
dl 0
loc 8
rs 9.4285
c 2
b 0
f 0
1
<?php
2
3
namespace Tests;
4
5
use AutoUpdate;
6
7
/**
8
 * Class AutoUpdateTest
9
 *
10
 * @package Tests
11
 */
12
class AutoUpdateTest extends \PHPUnit_Framework_TestCase {
13
14
	/**
15
	 * @var string this is used to cache the contents of the log when running tests and restore it after
16
	 */
17
	private static $logContents;
18
19
    /**
20
     * @inheritdoc
21
     */
22
	public static function setUpBeforeClass() {
23
		parent::setUpBeforeClass();
24
25
		//cache anything in the StableUpdate log to restore after
26
		if( file_exists( __DIR__ . '/../../Includes/StableUpdate.log' ) ) {
27
			self::$logContents = file_get_contents( __DIR__ . '/../../Includes/StableUpdate.log' );
28
		}
29
	}
30
31
    /**
32
     * @inheritdoc
33
     */
34
	public static function tearDownAfterClass() {
35
		parent::tearDownAfterClass();
36
37
		//restore the StableUpdate log to previous contents
38
		if( isset( self::$logContents ) ) {
39
			file_put_contents( __DIR__ . '/../../Includes/StableUpdate.log', self::$logContents );
40
		}
41
	}
42
43
    /**
44
     * @param $http
45
     * @return AutoUpdate
46
     */
47
    private function getUpdater($http)
48
    {
49
		return new AutoUpdate( $http );
50
	}
51
52
    /**
53
     * @param array $data
54
     * @return \PHPUnit_Framework_MockObject_MockObject
55
     */
56
    private function getMockHttp($data = array())
57
    {
58
		$mock = $this->getMockBuilder( 'HTTP' )
59
			->disableOriginalConstructor()
60
			->getMock();
61
		$mock->expects( $this->any() )
62
			->method( 'get' )
63
			->will( $this->returnValue( json_encode( $data ) ) );
64
		return $mock;
65
	}
66
67
    /**
68
     * @return array
69
     */
70
    public function provideCheckforupdate()
71
    {
72
		return array(
73
			array(
74
				true,
75
				array( 'message' => 'API rate limit exceeded' ),
76
				'/Cant check for updates right now, next window in/'
77
			),
78
			array(
79
				false,
80
				array( 'commit' => array( 'sha' => 'testshahash' ) ),
81
				'/Peachy Updated!  Changes will go into effect on the next run./',
82
			),
83
			array(
84
				true,
85
				array( 'commit' => array( 'sha' => 'testshahash' ) ),
86
				'/Peachy is up to date/',
87
				serialize( array( 'commit' => array( 'sha' => 'testshahash' ) ) )
88
			),
89
			array(
90
				false,
91
				array( 'commit' => array( 'sha' => 'testshahash' ) ),
92
				'/Peachy Updated!  Changes will go into effect on the next run./',
93
				serialize( array( 'commit' => array( 'sha' => 'differenthash!' ) ) )
94
			),
95
		);
96
	}
97
98
    /**
99
     * @dataProvider provideCheckforupdate
100
     * @covers       AutoUpdate::Checkforupdate
101
     * @covers       AutoUpdate::cacheLastGithubETag
102
     * @covers       AutoUpdate::getTimeToNextLimitWindow
103
     * @covers       AutoUpdate::__construct
104
     * @covers       AutoUpdate::updatePeachy
105
     * @covers       AutoUpdate::getLocalPath
106
     * @covers       AutoUpdate::copyOverGitFiles
107
     * @covers       AutoUpdate::rrmdir
108
     * @param $expected
109
     * @param $data
110
     * @param string $outputRegex
111
     * @param null $updatelog
112
     * @param bool $experimental
113
     * @param bool $wasexperimental
114
     */
115
	public function testCheckforupdate( $expected, $data, $outputRegex = '/.*?/', $updatelog = null, $experimental = false, $wasexperimental = false ) {
116
		$updater = $this->getUpdater( $this->getMockHttp( $data ) );
117
		if( $updatelog === null ) {
118
			if( file_exists( __DIR__ . '/../../Includes/' . ( $experimental ? 'Update.log' : 'StableUpdate.log' ) ) ) {
119
				unlink( __DIR__ . '/../../Includes/' . ( $experimental ? 'Update.log' : 'StableUpdate.log' ) );
120
			}
121
		} else {
122
			file_put_contents( __DIR__ . '/../../Includes/' . ( $experimental ? 'Update.log' : 'StableUpdate.log' ), $updatelog );
123
		}
124
125
		if( $wasexperimental ) {
126
			file_put_contents( __DIR__ . '/../../Includes/updateversion', serialize( 'master' ) );
127
		} else file_put_contents( __DIR__ . '/../../Includes/updateversion', serialize( 'stable' ) );
128
129
		$this->expectOutputRegex( $outputRegex );
130
		$result = $updater->Checkforupdate();
131
		if( !$result ) $updater->updatePeachy();
132
		$this->assertEquals( $expected, $result );
133
	}
134
135
} 
136