From e49019743c7010dc0bead96abde7e59b1e095eb4 Mon Sep 17 00:00:00 2001 From: Jason Date: Tue, 11 Mar 2014 11:00:26 +0000 Subject: [PATCH] Unit tests and some docs --- .gitignore | 2 + phpunit.xml.dist | 25 ++++++++++++ src/Flintstone/Flintstone.php | 9 +++++ tests/FeatureTest.php | 76 +++++++++++++++++++++++++++++++++++ tests/TestFixture.php | 35 ++++++++++++++++ 5 files changed, 147 insertions(+) create mode 100644 .gitignore create mode 100644 phpunit.xml.dist create mode 100644 tests/FeatureTest.php create mode 100644 tests/TestFixture.php diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..88e99d5 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +vendor +composer.lock \ No newline at end of file diff --git a/phpunit.xml.dist b/phpunit.xml.dist new file mode 100644 index 0000000..d4c66c2 --- /dev/null +++ b/phpunit.xml.dist @@ -0,0 +1,25 @@ + + + + + + src/Flintstone + + + + + + tests + + + \ No newline at end of file diff --git a/src/Flintstone/Flintstone.php b/src/Flintstone/Flintstone.php index b4c26e9..68bb731 100644 --- a/src/Flintstone/Flintstone.php +++ b/src/Flintstone/Flintstone.php @@ -36,4 +36,13 @@ public static function load($database, $options = array()) { return self::$instance[$database]; } + + /** + * Unload a database + * @param string $database the database name + * @return void + */ + public static function unload($database) { + unset(self::$instance[$database]); + } } \ No newline at end of file diff --git a/tests/FeatureTest.php b/tests/FeatureTest.php new file mode 100644 index 0000000..d55d48d --- /dev/null +++ b/tests/FeatureTest.php @@ -0,0 +1,76 @@ +assertTrue($this->db->set('a', '1')); + $this->assertTrue($this->db->set('b', 2)); + $this->assertTrue($this->db->set('c', array(3, 4, 5))); + } + + /** + * Test 'set' operations + * @expectedException Flintstone\FlintstoneException + */ + public function testSetException() { + $this->db->set('d', false); + } + + /** + * Test 'replace' operations + */ + public function testReplace() { + $this->assertTrue($this->db->set('a', '1')); + $this->assertTrue($this->db->replace('a', '2')); + $this->assertSame($this->db->get('a'), '2'); + } + + /** + * Test 'get' operations + */ + public function testGet() { + $this->assertFalse($this->db->get('a')); + $this->assertTrue($this->db->set('a', '1')); + $this->assertSame($this->db->get('a'), '1'); + } + + /** + * Test 'delete' operations + */ + public function testDelete() { + $this->assertFalse($this->db->delete('a')); + $this->assertTrue($this->db->set('a', '1')); + $this->assertTrue($this->db->delete('a')); + $this->assertFalse($this->db->get('a')); + } + + /** + * Test 'flush' operations + */ + public function testFlush() { + $this->assertTrue($this->db->set('a', '1')); + $this->assertTrue($this->db->set('b', 2)); + $this->assertTrue($this->db->flush()); + $this->assertFalse($this->db->get('a')); + $this->assertFalse($this->db->get('b')); + } + + /** + * Test 'getKeys' operations + */ + public function testGetKeys() { + $this->assertTrue($this->db->set('a', '1')); + $this->assertTrue($this->db->set('b', 2)); + $this->assertTrue($this->db->set('c', array(3, 4, 5))); + $keys = $this->db->getKeys(); + $this->assertEquals(3, count($keys)); + $this->assertContains('a', $keys); + } +} \ No newline at end of file diff --git a/tests/TestFixture.php b/tests/TestFixture.php new file mode 100644 index 0000000..fda0f20 --- /dev/null +++ b/tests/TestFixture.php @@ -0,0 +1,35 @@ +db = Flintstone::load('test', array('dir' => __DIR__)); + } + + /** + * Unload the test database and remove + */ + public function tearDown() { + Flintstone::unload('test'); + $file = __DIR__ . '/test.dat'; + unlink($file); + } +} \ No newline at end of file