public function testCustomType() { $mapper = \Amiss\Sql\Factory::createMapper(); $mapper->addTypeHandler(new TestCustomFieldTypeHandler(), 'foo'); $deps = \Amiss\Test\Factory::managerNoteModelCustom(' /** :amiss = true; */ class TestCustomFieldTypeModel { /** :amiss = {"field": {"primary": true, "type": "autoinc" }}; */ public $testCustomFieldTypeRecordId; /** * :amiss = {"field": {"type": "foo bar"}}; */ public $yep1; } ', (object) ['mapper' => $mapper]); $class = $deps->classes['TestCustomFieldTypeModel']; $r = new $class(); $r->yep1 = 'foo'; $deps->manager->save($r); $r = $deps->manager->getById($class, 1); // this will have passed through the prepareValueForDb first, then // through the handleValueFromDb method $this->assertEquals('value-db-foo', $r->yep1); }
public function testInsertGeneratesGuid() { $d = Test\Factory::managerNoteModelCustom(' /** * :amiss = { * "on": { * "beforeInsert": ["generateGuid"] * }, * "primary": "id" * }; */ class AutoGuid { /** :amiss = {"field": {"type": "autoinc"}}; */ public $id; /** :amiss = {"field": true}; */ public $guid; public function generateGuid() { $this->guid = \\Amiss\\Functions::guid(); } } '); $cls = "{$d->ns}\\AutoGuid"; $o = new $cls(); $d->manager->insert($o); $this->assertNotEmpty($o->guid); $o = $d->manager->get($cls); $this->assertNotEmpty($o->guid); }
function setUp() { $d = $this->deps = Test\Factory::managerNoteModelCustom(' /** :amiss = true; */ class TestParent { /** :amiss = {"field": {"primary": true}}; */ public $id; private $children = "nope"; /** :amiss = {"has": {"type": "many", "of": "{{ns}}TestChild", "to": "parentId"}}; */ public function getChildren() { return $this->children; } public function setChildren($v) { $this->children = $v; } } /** :amiss = true; */ class TestChild { /** :amiss = {"field": {"primary": true}}; */ public $id; /** :amiss = {"field": {"index": true}}; */ public $parentId; private $parent = "nope"; /** :amiss = {"has": {"type": "one", "of": "{{ns}}TestParent", "from": "parentId"}}; */ public function getParent() { return $this->parent; } public function setParent($v) { $this->parent = $v; } } '); $this->deps->manager->insertTable($d->classes['TestParent'], ['id' => 1]); $this->deps->manager->insertTable($d->classes['TestChild'], ['id' => 1, 'parentId' => 1]); $this->deps->manager->insertTable($d->classes['TestChild'], ['id' => 2, 'parentId' => 1]); }
public function testDeleteEvents() { $events = ['beforeDelete', 'afterDelete']; $deps = Test\Factory::managerNoteModelCustom($this->makeClass($events)); $class = $deps->classes['Test']; $class::setManager($deps->manager); $deps->connector->exec("INSERT INTO test(id) VALUES(1);"); $record = $class::getById(1); $record->delete(); $this->assertEquals($events, $record->events); }
public function testObjectToPropertiesWithGetters() { $deps = Test\Factory::managerNoteModelCustom(' /** :amiss = true; */ class Pants { /** :amiss = {"field": true}; */ function getId() { return $this->id; } function setId($v) { $this->id = $v; } } '); $c = $deps->ns . "\\Pants"; $pants = new $c(); $pants->setId(1); $props = $deps->mapper->mapObjectToProperties($pants); $this->assertEquals(['id' => 1], $props); }
/** * @group acceptance * @group manager * @group exists */ public function testExistsKeySingle() { $deps = Test\Factory::managerNoteModelCustom(' /** :amiss = true; */ class Pants { /** :amiss = {"field": {"index": {"key": true}}}; */ public $slug; /** :amiss = {"field": true}; */ public $name; } '); $class = $deps->classes['Pants']; $deps->manager->insertTable($class, ['slug' => 'yes', 'name' => 'Yep!']); $this->assertTrue($deps->manager->exists($class, 'yes', 'slug')); $this->assertFalse($deps->manager->exists($class, 'nup', 'slug')); }
function testSelectDateForcedTime() { $d = Test\Factory::managerNoteModelCustom(' /** :amiss = true; */ class Pants { /** :amiss = {"field": {"primary": true, "type": "autoinc"}}; */ public $id; /** :amiss = {"field": {"type": "date"}}; */ public $date; } '); $d->connector->exec("INSERT INTO pants(`date`) VALUES('2014-01-01');"); $date = new \DateTime('2014-01-01T12:00:00Z'); $result = $d->manager->getList($d->classes['Pants'], '{date}>=:date', ['date' => $date]); $this->assertCount(1, $result); }
function testDecimalToDb() { $d = Test\Factory::managerNoteModelCustom(' /** :amiss = true; */ class Pants { /** :amiss = {"field": {"primary": true, "type": "autoinc"}}; */ public $id; /** :amiss = {"field": {"type": "decimal"}}; */ public $num; } '); $c = $d->ns . '\\Pants'; $obj = new $c(); $obj->num = Decimal::fromString("1.23456"); $d->manager->insert($obj); $out = $d->connector->query("SELECT num FROM pants")->fetchColumn(0); $this->assertEquals("1.23456", $out); }
function testMetaAfterDelete() { $d = Test\Factory::managerNoteModelCustom(' /** * :amiss = {"on": {"afterDelete": ["a"]}}; */ class Pants { /** :amiss = {"field": {"primary": true, "type": "autoinc"}}; */ public $id; function a() { $this->id = 99; } } '); $cls = $d->classes['Pants']; $d->manager->insertTable($cls, ['id' => 94]); $d->manager->on['afterDelete'][] = function ($object) { $object->id -= 5; }; $o = new $cls(); $o->id = 1; $d->manager->insert($o, $d->manager->getMeta($cls)); // delete $o = $d->manager->getById($cls, 1); $this->assertEquals(1, $o->id); $d->manager->delete($o); // It will change in the instance $this->assertEquals(94, $o->id); // It should not exist in the DB $o = $d->manager->getById($cls, 1); $this->assertNull($o); // but the other one should $this->assertTrue($d->manager->exists($cls, 94)); }
protected static function managerNestedSetNote($classes) { $deps = Test\Factory::managerNoteModelCustom($classes); $deps->nsManager = \Amiss\Ext\NestedSet\Manager::createWithDefaults($deps->manager); return $deps; }
/** * @group acceptance * @group manager * @group getById */ public function testGetByIdArgs() { $d = Test\Factory::managerNoteModelCustom(' /** :amiss = {}; */ class Pants { /** :amiss = {"field": {"primary": true}}; */ public $id; function __construct($a, $b) { $this->a = $a; $this->b = $b; } } '); $d->manager->insertTable($d->classes['Pants'], ['id' => 100]); $result = $d->manager->getById($d->classes['Pants'], 100, ['args' => ['ding', 'dong']]); $this->assertEquals(100, $result->id); $this->assertEquals("ding", $result->a); $this->assertEquals("dong", $result->b); }