diff --git a/.gitignore b/.gitignore index b6b18f6..2d1b867 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ composer.lock /vendor/ *.tmp /test.php +*.ignore diff --git a/src/DataTransformers/JSON.php b/src/DataTransformers/JSON.php index 9b3b0c2..62fc944 100644 --- a/src/DataTransformers/JSON.php +++ b/src/DataTransformers/JSON.php @@ -53,7 +53,9 @@ class JSON extends AbstractDataTransformer implements \ArrayAccess, \Iterator { if (is_array($userValue)) { foreach ($userValue as $key => $value) { - $this[$key] = $value; + if ($value !== null) { + $this[$key] = $value; + } } } } diff --git a/tests/DataTransformers/JSONTest.php b/tests/DataTransformers/JSONTest.php index 67eb2b4..0f071e4 100644 --- a/tests/DataTransformers/JSONTest.php +++ b/tests/DataTransformers/JSONTest.php @@ -6,7 +6,7 @@ use Digraph\DataObject\DataTransformers\JSON; class JSONTest extends TestCase { - public function testJSON() + public function testStorageValues() { $o = 'foo'; $h = new JSON($o); @@ -19,4 +19,36 @@ class JSONTest extends TestCase $unset = $h->getStorageValue(); $this->assertEquals('[]', $unset); } + + public function testNullValues() + { + $o = 'foo'; + $h = new JSON($o); + $pre = $h->getStorageValue(); + $this->assertEquals('[]', $pre); + $h['foo'] = array( + 'bar' => null, + 'baz' => 'buzz' + ); + $post = $h->getStorageValue(); + $this->assertEquals(1, preg_match('/\{["\']foo["\']:{["\']baz["\']:["\']buzz["\']}\}/', $post)); + } + + public function testNullValuesOverwriting() + { + $o = 'foo'; + $h = new JSON($o); + $pre = $h->getStorageValue(); + $this->assertEquals('[]', $pre); + $h['foo'] = array( + 'bar' => 'baz', + 'baz' => 'buzz' + ); + $h['foo'] = array( + 'bar' => null, + 'baz' => 'buzz' + ); + $post = $h->getStorageValue(); + $this->assertEquals(1, preg_match('/\{["\']foo["\']:{["\']baz["\']:["\']buzz["\']}\}/', $post)); + } } diff --git a/tests/FilesTest.php b/tests/FilesTest.php deleted file mode 100644 index baa779e..0000000 --- a/tests/FilesTest.php +++ /dev/null @@ -1,159 +0,0 @@ -assertTrue($obj->files instanceof FilesContainer); - - //try adding a file from an info array - $tmpFile = tempnam(sys_get_temp_dir(), 'SFF'); - file_put_contents($tmpFile, 'test content'); - $newFile = array( - 'name' => 'test', - 'ext' => 'md', - 'size' => 12, - 'type' => 'text/markdown', - 'tmp_name' => $tmpFile - ); - $files = $obj->files; - $files->addFile('test', $newFile); - $this->assertTrue($obj->files['test'] instanceof SingleFile); - $this->assertEquals('test content', file_get_contents($files['test']->fullPath())); - - //test stashing file - //it should refuse to stash this file, because tmp_name isn't an actual - //uploaded file - $this->assertFalse($files['test']->stash()); - $this->assertTrue(isset($files['test']['tmp_name'])); - $this->assertFalse(isset($files['test']['stash_name'])); - $this->assertEquals('test content', file_get_contents($files['test']->fullPath())); - - //test stashing file, skipping uploaded check - $this->assertTrue($files['test']->stash(true)); - $this->assertFalse(isset($files['test']['tmp_name'])); - $this->assertTrue(isset($files['test']['stash_name'])); - $this->assertTrue(is_file($files['test']['stash_name'])); - $this->assertEquals('test content', file_get_contents($files['test']['stash_name'])); - $this->assertEquals('test content', file_get_contents($files['test']->fullPath())); - - //test storing file - $this->assertTrue($files['test']->store()); - $this->assertFalse(isset($files['test']['stash_name'])); - $this->assertTrue(is_file($files['test']->fullPath())); - $this->assertEquals('test content', file_get_contents($files['test']->fullPath())); - } - - public function testSingleFile() - { - $obj = new SFTO(); - $tmpFile = tempnam(sys_get_temp_dir(), 'SFF'); - file_put_contents($tmpFile, 'test content'); - $obj->files->addFile('test', array( - 'name' => 'test', - 'ext' => 'md', - 'size' => 12, - 'type' => 'text/markdown', - 'tmp_name' => $tmpFile - )); - - //test that automatic mtime updates are happening - $this->assertEquals(time(), $obj->files['test']['mtime']); - sleep(1); - $this->assertNotEquals(time(), $obj->files['test']['mtime']); - $obj->files['test']['name'] = 'new-name'; - $this->assertEquals(time(), $obj->files['test']['mtime']); - } - - /** - * @expectedException \PHPUnit_Framework_Error_Warning - */ - public function testWriteProtection() - { - $obj = new SFTO(); - $obj->files->addFile('test', array( - 'name' => 'test', - 'ext' => 'md', - 'size' => 12, - 'type' => 'text/markdown' - )); - $obj->files['test']['ctime'] = time(); - } - - public function testStringification() - { - $obj = new SFTO(); - $obj->files->addFile('test1', array( - 'name' => 'test1', - 'ext' => 'md', - 'size' => 12, - 'type' => 'text/markdown', - 'store_name' => 'imaginaryname' - )); - $obj->files->addFile('test2', array( - 'name' => 'test2', - 'ext' => 'txt', - 'size' => 24, - 'type' => 'text/plain' - )); - $storageString = $obj->files->getStorageString(); - //now make a new object and verify - $new = new FilesContainer($obj, $storageString); - $this->assertEquals('test1', $new['test1']['name']); - $this->assertEquals('md', $new['test1']['ext']); - $this->assertEquals('test2', $new['test2']['name']); - $this->assertEquals('txt', $new['test2']['ext']); - //verify same-ness of fullPath results - $this->assertEquals( - $obj->files['test1']->fullPath(), - $new['test1']->fullPath() - ); - } - - public function testFilesArrayAccess() - { - $obj = new SFTO(); - $obj['files']->addFile('test1', array( - 'name' => 'test1', - 'ext' => 'md', - 'size' => 12, - 'type' => 'text/markdown', - 'store_name' => 'imaginaryname' - )); - $obj['files']['test1']['name'] = 'altered'; - $this->assertEquals( - 'altered', - $obj->files['test1']['name'] - ); - } -} - -class SFTO extends AbstractArrayHarnessObject -{ - static function getMap() - { - $map = parent::getMap(); - $map['files'] = array( - 'name' => 'files', - 'getter' => 'Files', - 'setter' => 'Files', - 'default' => '{}' - ); - return $map; - } - - public function getFileFolder() - { - $tmpDir = tempnam(sys_get_temp_dir(), 'SFD').'.dir'; - if (!is_dir($tmpDir)) { - mkdir($tmpDir); - } - return $tmpDir; - } -} diff --git a/tests/JSON/JSONLayerTest.php b/tests/JSON/JSONLayerTest.php deleted file mode 100644 index 3d40c28..0000000 --- a/tests/JSON/JSONLayerTest.php +++ /dev/null @@ -1,73 +0,0 @@ -assertEquals( - 'foo', - $layer[0] - ); - $this->assertEquals( - 'bar', - $layer[1] - ); - $this->assertEquals( - 'baz', - $layer[2] - ); - } - - public function testNestingAndAltering() - { - $test = array( - 'foo' => 'bar', - 'baz' => array( - 'good' => 'nice', - 'bad' => 'mean' - ) - ); - $layer = new JSONLayer($test); - $this->assertEquals( - 'nice', - $layer['baz']['good'] - ); - $this->assertTrue($layer['baz'] instanceof JSONLayer); - - //test altering both first and second layer - $layer['foo'] = 'rab'; - $this->assertEquals( - 'rab', - $layer['foo'] - ); - - $layer['baz']['bad'] = 'evil'; - $this->assertEquals( - 'evil', - $layer['baz']['bad'] - ); - - //test adding an array value - $layer['bar'] = array( - 'a' => 'b', - 'c' => 'd' - ); - - $this->assertTrue($layer['bar'] instanceof JSONLayer); - $this->assertEquals( - 'b', - $layer['bar']['a'] - ); - $this->assertEquals( - 'd', - $layer['bar']['c'] - ); - } -} diff --git a/tests/SQL/AbstractSQLDataObjectTest.php b/tests/SQL/AbstractSQLDataObjectTest.php deleted file mode 100644 index 6e4e5f4..0000000 --- a/tests/SQL/AbstractSQLDataObjectTest.php +++ /dev/null @@ -1,276 +0,0 @@ -assertInstanceOf( - '\\Digraph\\DataObject\\Tests\\SQL\\HarnessObject', - new HarnessObject(), - "Failed to instantiate a HarnessObject" - ); - } - - public function testCreateAndRead() - { - //test creation - $new = new HarnessObject(array( - 'test_col' => 'test data' - )); - $this->newID = $new->do_id; - $new->create(); - $this->assertEquals( - 1, - $this->getConnection()->getRowCount('HarnessObject'), - "HarnessObject table should have a row in it after create() is called" - ); - - //test reading that object back out - $read = HarnessObject::read($new->do_id); - $this->assertNotNull($read); - foreach (HarnessObject::map() as $key => $value) { - if ($new->$key instanceof DataTransformerInterface) { - $this->assertEquals( - $new->$key->getUserValue(), - $read->$key->getUserValue(), - "Item $key doesn't match after reading back out of DB" - ); - } else { - $this->assertEquals( - $new->$key, - $read->$key, - "Item $key doesn't match after reading back out of DB" - ); - } - } - } - - /** - * @expectedException Digraph\DataObject\Exceptions\IDExistsException - */ - function testCreationIDExists() - { - $new = new HarnessObject(); - $new->create(); - $new->create(); - } - - public function testSearchSortLimit() - { - $d = new HarnessObject(array( - 'search_sort' => 3, - 'search_like' => 'xax' - )); - $c = new HarnessObject(array( - 'search_sort' => 3, - 'search_like' => 'xzx' - )); - $b = new HarnessObject(array( - 'search_sort' => 2, - 'search_like' => 'xbx' - )); - $a = new HarnessObject(array( - 'search_sort' => 1, - 'search_like' => 'xax' - )); - $a->create(); - $b->create(); - $c->create(); - $d->create(); - //check - $sorted = HarnessObject::search( - array(), - array( - 'search_sort' => 'desc', - 'search_like' => 'asc' - ), - array() - ); - //assert proper sorting - $this->assertEquals(3, $sorted[0]->search_sort); - $this->assertEquals('xax', $sorted[0]->search_like); - $this->assertEquals(3, $sorted[1]->search_sort); - $this->assertEquals('xzx', $sorted[1]->search_like); - $this->assertEquals(2, $sorted[2]->search_sort); - $this->assertEquals(1, $sorted[3]->search_sort); - //re-query with limit - $limited = HarnessObject::search( - array(), - array( - 'search_sort' => 'desc', - 'search_like' => 'asc' - ), - array( - 'limit' => 2 - ) - ); - $this->assertEquals(2, count($limited)); - $this->assertEquals(3, $limited[0]->search_sort); - $this->assertEquals('xax', $limited[0]->search_like); - $this->assertEquals(3, $limited[1]->search_sort); - $this->assertEquals('xzx', $limited[1]->search_like); - //re-query with limit, offset - $limited = HarnessObject::search( - array(), - array( - 'search_sort' => 'desc', - 'search_like' => 'asc' - ), - array( - 'limit' => 3, - 'offset' => 1 - ) - ); - $this->assertEquals(3, count($limited)); - $this->assertEquals(3, $limited[0]->search_sort); - $this->assertEquals('xzx', $limited[0]->search_like); - $this->assertEquals(2, $limited[1]->search_sort); - $this->assertEquals(1, $limited[2]->search_sort); - } - - public function testSearchLike() - { - $a = new HarnessObject(array( - 'search_sort' => 1, - 'search_like' => 'xax' - )); - $b = new HarnessObject(array( - 'search_sort' => 2, - 'search_like' => 'xbx' - )); - $c = new HarnessObject(array( - 'search_sort' => 3, - 'search_like' => 'xzx' - )); - $d = new HarnessObject(array( - 'search_sort' => 3, - 'search_like' => 'xax' - )); - $a->create(); - $b->create(); - $c->create(); - $d->create(); - //check - $sorted = HarnessObject::search( - array( - ':search_like LIKE ?' => '%a%' - ), - array(), - array() - ); - $this->assertEquals(2, count($sorted)); - } - - public function testUpdate() - { - //test basic update - $rowCount = $this->getConnection()->getRowCount('HarnessObject'); - $new = new HarnessObject(); - $new->create(); - $read = HarnessObject::read($new->do_id); - $read->test_col = 'updated'; - $read->update(); - $read2 = HarnessObject::read($new->do_id); - $this->assertEquals( - 'updated', - $read2->test_col, - "After update, test_col value should be saved and have value \"updated\" in freshly loaded object" - ); - } - - public function testUpdateJSON() - { - //test basic update - $rowCount = $this->getConnection()->getRowCount('HarnessObject'); - $new = new HarnessObject(array( - 'test_json' => array('array'=>'not updated') - )); - $this->assertEquals( - array('array'=>'not updated'), - $new->test_json->getUserValue(), - "Before update, test_col value should be saved and have value \"not updated\" in freshly created object" - ); - $new->create(); - $read = HarnessObject::read($new->do_id); - $read->test_json = array('array'=>'updated'); - $read->update(); - $read2 = HarnessObject::read($new->do_id); - $this->assertEquals( - array('array'=>'updated'), - $read2->test_json->getUserValue(), - "After update, test_col value should be saved and have value \"updated\" in freshly loaded object" - ); - } - - public function testDelete() - { - $rowCount = $this->getConnection()->getRowCount('HarnessObject'); - $new = new HarnessObject(); - $new->create(); - $new->delete(); - $this->assertEquals( - $rowCount+1, - $this->getConnection()->getRowCount('HarnessObject'), - "HarnessObject table should have one more entry even after a delete()" - ); - $this->assertNull( - HarnessObject::read($new->do_id), - "A deleted object must not be returned by read()" - ); - $read = HarnessObject::read($new->do_id, true); - $this->assertNotNull( - $read, - "A deleted object should be returned by read() if the \$deleted flag is set" - ); - //test permanent deletion - $rowCount = $this->getConnection()->getRowCount('HarnessObject'); - $new = new HarnessObject(); - $new->create(); - $new->delete(true); - $this->assertEquals( - $rowCount, - $this->getConnection()->getRowCount('HarnessObject'), - 'Row count should be the same as at the start after delete(true), as row is actually removed from DB' - ); - } -} - -$_SERVER['REMOTE_ADDR'] = '127.0.0.1'; - -class HarnessObject extends AbstractSQLDataObject -{ - protected static $_table = 'HarnessObject'; - - static function getMap() - { - $map = parent::getMap(); - $map['test_col'] = array( - 'name' => 'test_col_col' - ); - $map['test_json'] = array( - 'name' => 'test_json_col', - 'transform' => 'JSON' - ); - $map['search_sort'] = array( - 'name'=>'search_sort' - ); - $map['search_like'] = array( - 'name'=>'search_like' - ); - return $map; - } - - protected static function buildConn() - { - return new \PDO('sqlite:test.sqlite.tmp'); - } - - public function getStorageFolder() - { - return ''; - } -} diff --git a/tests/SQL/Generic_Tests_DatabaseTestCase.php b/tests/SQL/Generic_Tests_DatabaseTestCase.php deleted file mode 100644 index 4f36ed1..0000000 --- a/tests/SQL/Generic_Tests_DatabaseTestCase.php +++ /dev/null @@ -1,30 +0,0 @@ -createDefaultDBConnection(self::$pdo, 'test.sqlite.tmp'); - } - - public function getDataSet() - { - return $this->createXMLDataSet(dirname(__FILE__).'/_files/db-seed.xml'); - } -} diff --git a/tests/SQL/_files/db-seed.xml b/tests/SQL/_files/db-seed.xml deleted file mode 100644 index c1e4e40..0000000 --- a/tests/SQL/_files/db-seed.xml +++ /dev/null @@ -1,16 +0,0 @@ - - - - do_id - do_cdate - do_cuser - do_mdate - do_muser - -
-