cleaned up tests, fixed null value in arrays bug

This commit is contained in:
Joby Elliott 2019-03-06 12:48:22 -07:00
parent 0a2bbe44a1
commit 171e32fc18
8 changed files with 37 additions and 556 deletions

1
.gitignore vendored
View file

@ -2,3 +2,4 @@ composer.lock
/vendor/
*.tmp
/test.php
*.ignore

View file

@ -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;
}
}
}
}

View file

@ -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));
}
}

View file

@ -1,159 +0,0 @@
<?php
namespace Digraph\DataObject\Tests;
use Digraph\DataObject\Files\SingleFile;
use Digraph\DataObject\Files\FilesContainer;
class FilesTest extends \PHPUnit_Framework_TestCase
{
public function testContainer()
{
//test proper instantiation and getter/setter in AbstractDataObject
$obj = new SFTO();
$this->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;
}
}

View file

@ -1,73 +0,0 @@
<?php
namespace Digraph\DataObject\Tests;
use PHPUnit\Framework\TestCase;
use Digraph\DataObject\JSON\JSONLayer;
class JSONLayerTest extends TestCase
{
public function testJSONLayer()
{
$layer = new JSONLayer(array(
'foo','bar','baz'
));
$this->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']
);
}
}

View file

@ -1,276 +0,0 @@
<?php
namespace Digraph\DataObject\Tests\SQL;
use \Digraph\DataObject\SQL\AbstractSQLDataObject;
use \Digraph\DataObject\DataTransformers\DataTransformerInterface;
class AbstractSQLDataObjectTest extends Generic_Tests_DatabaseTestCase
{
public function testInstantiation()
{
$this->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 '';
}
}

View file

@ -1,30 +0,0 @@
<?php
namespace Digraph\DataObject\Tests\SQL;
use \PDO;
use Digraph\DataObject\SQL\AbstractSQLDataObject;
abstract class Generic_Tests_DatabaseTestCase extends \PHPUnit_Extensions_Database_TestCase
{
private static $pdo = null;
/**
* Note that the tests copy test.sqlite to test.sqlite.tmp to avoid
* mucking up the repository
* @return
*/
public function getConnection()
{
if (self::$pdo === null) {
self::$pdo = new PDO('sqlite:test.sqlite.tmp');
copy('test.sqlite', 'test.sqlite.tmp');
}
return $this->createDefaultDBConnection(self::$pdo, 'test.sqlite.tmp');
}
public function getDataSet()
{
return $this->createXMLDataSet(dirname(__FILE__).'/_files/db-seed.xml');
}
}

View file

@ -1,16 +0,0 @@
<?xml version="1.0" ?>
<dataset>
<table name="HarnessObject">
<column>do_id</column>
<column>do_cdate</column>
<column>do_cuser</column>
<column>do_mdate</column>
<column>do_muser</column>
<!-- <row>
<value>1</value>
<value>Hello buddy!</value>
<value>joe</value>
<value>2010-04-24 17:15:23</value>
</row> -->
</table>
</dataset>