This repository has been archived by the owner on May 27, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 87
/
.test.php
141 lines (118 loc) · 4.07 KB
/
.test.php
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
<?php
require 'vendor/autoload.php';
use Seld\JsonLint\JsonParser;
class SteamLinuxTest extends PHPUnit_Framework_TestCase
{
public function testFileExists( )
{
// Trying to get dataProvider to work with depends in phpunit requires some serious magic
$filePath = __DIR__ . DIRECTORY_SEPARATOR . 'GAMES.json';
$this->assertFileExists( $filePath );
return $filePath;
}
/**
* @depends testFileExists
*/
public function testFileNotEmpty( $filePath )
{
$games = file_get_contents( $filePath );
$this->assertNotEmpty( $games );
return $games;
}
/**
* We're sadistic bastards that only allow tabs
*
* @depends testFileNotEmpty
*/
public function testWhitespace( $games )
{
$this->assertNotRegExp( '/^ +/m', $games, 'Spaces used, we only allow tabs' );
$this->assertNotRegExp( '/^\t+ +/m', $games, 'Tabs mixed with spaces, we only allow tabs' );
$games = trim( $games );
$this->assertNotRegExp( '/\s$/m', $games, 'End of line whitespace found, fix it' );
$this->assertNotRegExp( '/^$/m', $games, 'Empty line found, fix it' );
return $games;
}
/**
* @depends testWhitespace
*/
public function testJSON( $games )
{
try
{
$parser = new JsonParser();
$games = $parser->parse( $games, JsonParser::DETECT_KEY_CONFLICTS + JsonParser::PARSE_TO_ASSOC );
} catch ( Exception $e )
{
$this->assertTrue( 'parsing', $e->getMessage() );
}
$allowedKeys = Array(
'Hidden' => 'is_bool',
'Beta' => 'is_bool',
'Comment' => 'is_string',
'CommentURL' => 'is_string'
);
foreach( $games as $appID => $keys )
{
$this->assertTrue( is_numeric( $appID ), 'Key "' . $appID . '" must be numeric' );
if( $keys === true )
{
// We're golden!
}
else if( is_array( $keys ) )
{
$this->assertNotEmpty( $keys, '"' . $appID . '" can not be an empty array' );
foreach( $keys as $key => $value )
{
$this->assertArrayHasKey( $key, $allowedKeys, 'Invalid key "' . $key . '" in "' . $appID . '"' );
$this->assertTrue( $allowedKeys[ $key ]( $value ), '"' . $key . '" in "' . $appID . '" is not "' . $allowedKeys[ $key ] . '"' );
if( $key === 'Beta' )
{
$this->assertTrue( $value, $key . ' key in "' . $appID . '" can only be set to true' );
}
else if( $key === 'Hidden' )
{
$this->assertTrue( $value, $key . ' key in "' . $appID . '" can only be set to true' );
$this->assertArrayNotHasKey( 'Beta', $keys, 'Beta key can not be used along with Hidden key in "' . $appID . '"' );
$this->assertArrayHasKey( 'Comment', $keys, 'Hidden app "' . $appID . '" must contain a Comment explaining why it was hidden' );
}
else if( $key === 'CommentURL' )
{
$this->assertArrayHasKey( 'Comment', $keys, 'CommentURL key can not be without Comment key in "' . $appID . '"' );
$this->assertStringStartsWith( 'http', $value, 'CommentURL must be an url in "' . $appID . '"' );
}
}
}
else
{
$this->assertTrue( false, 'Key "' . $appID . '" has an invalid value' );
}
}
return $games;
}
/**
* @depends testJSON
*/
public function testSorting( $games )
{
$gamesSorted = $games;
ksort( $gamesSorted );
if( $games !== $gamesSorted )
{
$gamesKeys = array_keys( $games );
$gamesSortedKeys = array_keys( $gamesSorted );
$cachedCount = count( $gamesKeys );
unset( $games, $gamesSorted );
for( $i = 0; $i < $cachedCount; ++$i )
{
$message = '';
if ( $gamesKeys[ $i ] !== $gamesSortedKeys[ $i ] )
{
$where = array_search( $gamesKeys[ $i ], $gamesSortedKeys ) - array_search( $gamesSortedKeys[ $i ], $gamesKeys );
$message = $where > 0 ? $gamesKeys[ $i ] . '" is far too early' : ( $where == 0 ? $gamesKeys[ $i ] . '" is on an adjacent line' : $gamesSortedKeys[ $i ] . '" is far too late' );
}
$this->assertEquals( $gamesKeys[ $i ], $gamesSortedKeys[ $i ], 'File must be sorted correctly by appid, "' . $message );
}
}
}
}