From 6d8764c5366e0b1baf66da50230dac623edb2450 Mon Sep 17 00:00:00 2001
From: Jacek Kowalski <Jacek@jacekk.info>
Date: Sun, 06 Jul 2014 22:25:29 +0000
Subject: [PATCH] [core] Umożliwienie ustawienia katalogu z danymi sesji użytkowników i dostosowanie testów jednostkowych.

---
 tests/Core/BotSessionTest.php |   72 ++++++++++++++++++++----------------
 1 files changed, 40 insertions(+), 32 deletions(-)

diff --git a/tests/Core/BotSessionTest.php b/tests/Core/BotSessionTest.php
index 1b2209f..62a79d7 100644
--- a/tests/Core/BotSessionTest.php
+++ b/tests/Core/BotSessionTest.php
@@ -1,68 +1,77 @@
 <?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
@@ -108,7 +117,7 @@
 	 * @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(
@@ -124,13 +133,12 @@
 		$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);
 	}
 }

--
Gitblit v1.9.1