forked from SteamDatabase/SteamLinux
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path.test.php
121 lines (101 loc) · 3.43 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
<?php
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' );
return $games;
}
/**
* @depends testWhitespace
*/
public function testJSON( $games )
{
$games = json_decode( $games, true );
$this->assertSame( json_last_error(), JSON_ERROR_NONE, 'JSON Error: ' . ( function_exists( 'json_last_error_msg' ) ? json_last_error_msg() : json_last_error() ) );
$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 )
{
$this->assertEquals( $gamesKeys[ $i ], $gamesSortedKeys[ $i ], 'File must be sorted correctly by appid, problem at "' . $gamesKeys[ $i ] . '"' );
}
}
}
}