commit | author | age
|
0868e0
|
1 |
<?php |
JK |
2 |
class BotSessionTest extends PHPUnit_Framework_TestCase { |
6d8764
|
3 |
private static $dataFolder; |
JK |
4 |
private static $legacyFolder; |
|
5 |
|
|
6 |
private static function tmpdir() { |
|
7 |
$tmpName = tempnam(sys_get_temp_dir(), 'Bot'); |
|
8 |
unlink($tmpName); |
|
9 |
mkdir($tmpName); |
|
10 |
return $tmpName; |
|
11 |
} |
|
12 |
|
|
13 |
private static function rmdir($dir) { |
|
14 |
foreach(glob($dir.'/*', GLOB_NOSORT) as $name) { |
|
15 |
if($name == '.' || $name == '..') continue; |
|
16 |
|
|
17 |
if(is_dir($name)) { |
|
18 |
self::rmdir($name); |
|
19 |
} else { |
|
20 |
unlink($name); |
|
21 |
} |
|
22 |
} |
0868e0
|
23 |
|
6d8764
|
24 |
rmdir($dir); |
0868e0
|
25 |
} |
JK |
26 |
|
|
27 |
/** |
6d8764
|
28 |
* Create one-time directories for testing purposes. |
0868e0
|
29 |
*/ |
6d8764
|
30 |
static function setUpBeforeClass() { |
JK |
31 |
self::$dataFolder = self::tmpdir(); |
|
32 |
self::$legacyFolder = self::tmpdir(); |
|
33 |
} |
|
34 |
|
0868e0
|
35 |
function testPullEmpty() { |
6d8764
|
36 |
$session = new BotSession('test://user1@test', self::$dataFolder, self::$legacyFolder); |
0868e0
|
37 |
$session->setClass('test'); |
JK |
38 |
|
|
39 |
$this->assertEquals(array(), $session->pull()); |
6d8764
|
40 |
$this->assertTrue(count(glob(self::$dataFolder.'/*.sqlite')) == 1); |
0868e0
|
41 |
} |
JK |
42 |
|
|
43 |
/** |
|
44 |
* @expectedException Exception |
|
45 |
*/ |
|
46 |
function testSetClass() { |
6d8764
|
47 |
$session = new BotSession('test://testException', self::$dataFolder, self::$legacyFolder); |
0868e0
|
48 |
$session->pull(); |
JK |
49 |
} |
|
50 |
|
|
51 |
function testLegacyImport() { |
|
52 |
$data = array('test' => true, 'other' => 'yes, sir!'); |
|
53 |
$data_serialized = serialize($data); |
|
54 |
|
6d8764
|
55 |
$this->assertTrue(mkdir(self::$legacyFolder.'/test')); |
0868e0
|
56 |
|
6d8764
|
57 |
$filename = self::$legacyFolder.'/test/legacyUser.ggdb'; |
0868e0
|
58 |
$this->assertEquals(strlen($data_serialized), file_put_contents($filename, $data_serialized)); |
JK |
59 |
$this->assertEquals($data_serialized, file_get_contents($filename)); |
|
60 |
|
6d8764
|
61 |
$session = new BotSession('test://legacyUser@test', self::$dataFolder, self::$legacyFolder); |
0868e0
|
62 |
$session->setClass('test'); |
JK |
63 |
|
|
64 |
$this->assertTrue(isset($session->test)); |
|
65 |
$this->assertEquals($data, $session->pull()); |
|
66 |
|
|
67 |
$this->assertFalse(file_exists($filename)); |
|
68 |
} |
|
69 |
|
|
70 |
/** |
|
71 |
* @depends testPullEmpty |
|
72 |
*/ |
|
73 |
function testManualExample() { |
6d8764
|
74 |
$session = new BotSession('test://user1@test', self::$dataFolder, self::$legacyFolder); |
0868e0
|
75 |
$session->setClass('test'); |
JK |
76 |
|
|
77 |
// Ustawienie pojedynczej wartości |
|
78 |
$session->zmienna = 'To jest test'; |
|
79 |
$this->assertTrue(isset($session->zmienna)); |
|
80 |
$this->assertEquals('To jest test', $session->zmienna); |
|
81 |
|
|
82 |
// Usunięcie pojedynczej wartości |
|
83 |
unset($session->zmienna); |
|
84 |
$this->assertFalse(isset($session->zmienna)); |
|
85 |
$this->assertEquals(NULL, $session->zmienna); |
|
86 |
|
|
87 |
// Ustawienie pojedynczej wartości ponownie |
|
88 |
$session->zmienna = 'To jest test'; |
|
89 |
$this->assertTrue(isset($session->zmienna)); |
|
90 |
$this->assertEquals('To jest test', $session->zmienna); |
|
91 |
|
|
92 |
// Usunięcie wszystkich danych |
|
93 |
$session->truncate(); |
|
94 |
$this->assertFalse(isset($session->zmienna)); |
|
95 |
$this->assertEquals(NULL, $session->zmienna); |
|
96 |
$this->assertEquals(array(), $session->pull()); |
|
97 |
|
|
98 |
// Dopisanie (nadpisanie) danych |
|
99 |
$array = array( |
|
100 |
'zmienna' => 'To jest test2', |
|
101 |
'zmienna2' => new DateTime('2012-01-10') |
|
102 |
); |
|
103 |
$session->push($array); |
|
104 |
|
|
105 |
$this->assertEquals('To jest test2', $session->zmienna); |
|
106 |
$this->assertEquals($array, $session->pull()); |
|
107 |
|
|
108 |
// push() nie usuwa istniejących danych |
|
109 |
$session->zmienna3 = '333'; |
|
110 |
$session->push($array); |
|
111 |
$this->assertNotEquals($array, $session->pull()); |
|
112 |
|
|
113 |
unset($this->session); |
|
114 |
} |
|
115 |
|
|
116 |
/** |
|
117 |
* @depends testManualExample |
|
118 |
*/ |
|
119 |
function testManualExample2() { |
6d8764
|
120 |
$session = new BotSession('test://user1@test', self::$dataFolder, self::$legacyFolder); |
0868e0
|
121 |
$session->setClass('test'); |
JK |
122 |
|
|
123 |
$array = array( |
|
124 |
'zmienna' => 'To jest test2', |
|
125 |
'zmienna2' => new DateTime('2012-01-10'), |
|
126 |
'zmienna3' => '333' |
|
127 |
); |
|
128 |
|
|
129 |
$this->assertEquals($array, $session->pull()); |
|
130 |
|
|
131 |
|
|
132 |
$session->setClass('test2'); |
|
133 |
$this->assertEquals(array(), $session->pull()); |
|
134 |
} |
|
135 |
|
6d8764
|
136 |
static function tearDownAfterClass() { |
JK |
137 |
foreach(glob(self::$dataFolder.'/*.sqlite') as $file) { |
0868e0
|
138 |
unlink($file); |
JK |
139 |
} |
6d8764
|
140 |
|
JK |
141 |
self::rmdir(self::$dataFolder); |
|
142 |
self::rmdir(self::$legacyFolder); |
0868e0
|
143 |
} |
JK |
144 |
} |