| | |
| | | <?php |
| | | class BotSessionTest extends PHPUnit_Framework_TestCase { |
| | | function testSessionFolder() { |
| | | $dbFolder = dirname(__FILE__).'/../../database'; |
| | | private static $dataFolder; |
| | | private static $legacyFolder; |
| | | |
| | | private static function tmpdir() { |
| | | $tmpName = tempnam(sys_get_temp_dir(), 'Bot'); |
| | | unlink($tmpName); |
| | | mkdir($tmpName); |
| | | return $tmpName; |
| | | } |
| | | |
| | | private static function rmdir($dir) { |
| | | foreach(glob($dir.'/*', GLOB_NOSORT) as $name) { |
| | | if($name == '.' || $name == '..') continue; |
| | | |
| | | if(is_dir($name)) { |
| | | self::rmdir($name); |
| | | } else { |
| | | unlink($name); |
| | | } |
| | | } |
| | | |
| | | $this->assertTrue(is_writable($dbFolder)); |
| | | $this->assertTrue(count(glob($dbFolder.'/*.sqlite')) == 0); |
| | | rmdir($dir); |
| | | } |
| | | |
| | | /** |
| | | * @depends testSessionFolder |
| | | * Create one-time directories for testing purposes. |
| | | */ |
| | | static function setUpBeforeClass() { |
| | | self::$dataFolder = self::tmpdir(); |
| | | self::$legacyFolder = self::tmpdir(); |
| | | } |
| | | |
| | | function testPullEmpty() { |
| | | $dbFolder = dirname(__FILE__).'/../../database'; |
| | | |
| | | $session = new BotSession('test://user1@test'); |
| | | $session = new BotSession('test://user1@test', self::$dataFolder, self::$legacyFolder); |
| | | $session->setClass('test'); |
| | | |
| | | $this->assertEquals(array(), $session->pull()); |
| | | $this->assertTrue(count(glob($dbFolder.'/*.sqlite')) == 1); |
| | | $this->assertTrue(count(glob(self::$dataFolder.'/*.sqlite')) == 1); |
| | | } |
| | | |
| | | /** |
| | | * @depends testPullEmpty |
| | | * @expectedException Exception |
| | | */ |
| | | function testSetClass() { |
| | | $session = new BotSession('test://user1'); |
| | | $session = new BotSession('test://testException', self::$dataFolder, self::$legacyFolder); |
| | | $session->pull(); |
| | | } |
| | | |
| | | /** |
| | | * @depends testPullEmpty |
| | | */ |
| | | function testLegacyImport() { |
| | | $dbFolder = dirname(__FILE__).'/../../database'; |
| | | $oldDbFolder = $dbFolder = dirname(__FILE__).'/../../db'; |
| | | |
| | | $data = array('test' => true, 'other' => 'yes, sir!'); |
| | | $data_serialized = serialize($data); |
| | | |
| | | $this->assertTrue(mkdir($oldDbFolder)); |
| | | $this->assertTrue(is_writable($oldDbFolder)); |
| | | $this->assertTrue(mkdir($oldDbFolder.'/test')); |
| | | $this->assertTrue(mkdir(self::$legacyFolder.'/test')); |
| | | |
| | | $filename = $oldDbFolder.'/test/testUser.ggdb'; |
| | | $filename = self::$legacyFolder.'/test/legacyUser.ggdb'; |
| | | $this->assertEquals(strlen($data_serialized), file_put_contents($filename, $data_serialized)); |
| | | $this->assertEquals($data_serialized, file_get_contents($filename)); |
| | | |
| | | $session = new BotSession('test://testUser@test'); |
| | | $session = new BotSession('test://legacyUser@test', self::$dataFolder, self::$legacyFolder); |
| | | $session->setClass('test'); |
| | | |
| | | $this->assertTrue(isset($session->test)); |
| | | $this->assertEquals($data, $session->pull()); |
| | | |
| | | $this->assertFalse(file_exists($filename)); |
| | | $this->assertTrue(rmdir($oldDbFolder.'/test')); |
| | | $this->assertTrue(rmdir($oldDbFolder)); |
| | | } |
| | | |
| | | /** |
| | | * @depends testPullEmpty |
| | | */ |
| | | function testManualExample() { |
| | | $session = new BotSession('test://user1@test'); |
| | | $session = new BotSession('test://user1@test', self::$dataFolder, self::$legacyFolder); |
| | | $session->setClass('test'); |
| | | |
| | | // Ustawienie pojedynczej wartości |
| | |
| | | * @depends testManualExample |
| | | */ |
| | | function testManualExample2() { |
| | | $session = new BotSession('test://user1@test'); |
| | | $session = new BotSession('test://user1@test', self::$dataFolder, self::$legacyFolder); |
| | | $session->setClass('test'); |
| | | |
| | | $array = array( |
| | |
| | | $this->assertEquals(array(), $session->pull()); |
| | | } |
| | | |
| | | /** |
| | | * @depends testManualExample2 |
| | | */ |
| | | function testCleanup() { |
| | | $dbFolder = dirname(__FILE__).'/../../database'; |
| | | foreach(glob($dbFolder.'/*.sqlite') as $file) { |
| | | static function tearDownAfterClass() { |
| | | foreach(glob(self::$dataFolder.'/*.sqlite') as $file) { |
| | | unlink($file); |
| | | } |
| | | |
| | | self::rmdir(self::$dataFolder); |
| | | self::rmdir(self::$legacyFolder); |
| | | } |
| | | } |