public function testModelInsert() { $a = ['user' => [1 => ['name' => 'John', 'surname' => 'Smith'], 2 => ['name' => 'Sarah', 'surname' => 'Jones']]]; $p = new Persistence_SQL('sqlite::memory:'); $p->connection->expr('drop table if exists user')->execute(); $p->connection->expr('create table user(id integer primary key autoincrement, name varchar(255), surname varchar(255))')->execute(); $m = new Model($p, 'user'); $m->addField('name'); $m->addField('surname'); $ms = []; foreach ($a['user'] as $id => $row) { $ms[] = $m->insert($row); } $this->assertEquals('John', $m->load($ms[0])['name']); $this->assertEquals('Jones', $m->load($ms[1])['surname']); }
public function testAtomicOperations() { $db = new Persistence_SQL($this->db->connection); $a = ['item' => [['name' => 'John'], ['name' => 'Sue'], ['name' => 'Smith']]]; $this->setDB($a); $m = new Model($db, 'item'); $m->addField('name'); $m->load(2); $m->addHook('afterSave', function ($m) { throw new \Exception('Awful thing happened'); }); $m['name'] = 'XXX'; try { $m->save(); } catch (\Exception $e) { } $this->assertEquals('Sue', $this->getDB()['item'][2]['name']); $m->addHook('afterDelete', function ($m) { throw new \Exception('Awful thing happened'); }); try { $m->delete(); } catch (\Exception $e) { } $this->assertEquals('Sue', $this->getDB()['item'][2]['name']); }
public function testUpdateArray() { $a = ['user' => [1 => ['name' => 'John', 'surname' => 'Smith'], 2 => ['name' => 'Sarah', 'surname' => 'Jones']]]; $p = new Persistence_Array($a); $m = new Model($p, 'user'); $m->addField('name'); $m->addField('surname'); $m->load(1); $m['name'] = 'Peter'; $m->save(); $m->load(2); $m['surname'] = 'Smith'; $m->save(); $m['surname'] = 'QQ'; $m->save(); $this->assertEquals(['user' => [1 => ['name' => 'Peter', 'surname' => 'Smith'], 2 => ['name' => 'Sarah', 'surname' => 'QQ']]], $a); $m->unload(); $m->set(['name' => 'Foo', 'surname' => 'Bar']); $m->save(); $this->assertEquals(['user' => [1 => ['name' => 'Peter', 'surname' => 'Smith'], 2 => ['name' => 'Sarah', 'surname' => 'QQ'], 3 => ['name' => 'Foo', 'surname' => 'Bar', 'id' => 3]]], $a); }
public function testDoubleReverseJoin() { $a = ['user' => [10 => ['id' => 10, 'name' => 'John 2', 'contact_id' => 100], 20 => ['id' => 20, 'name' => 'Peter', 'contact_id' => 100], 30 => ['id' => 30, 'name' => 'XX', 'contact_id' => 200], 40 => ['id' => 40, 'name' => 'YYY', 'contact_id' => 300]], 'contact' => [100 => ['id' => 100, 'contact_phone' => '+555', 'country_id' => 1], 200 => ['id' => 200, 'contact_phone' => '+999', 'country_id' => 2], 300 => ['id' => 300, 'contact_phone' => '+777', 'country_id' => 5]], 'country' => [1 => ['id' => 1, 'name' => 'UK'], 2 => ['id' => 2, 'name' => 'US'], 3 => ['id' => 3, 'name' => 'India']]]; $this->setDB($a); $db = new Persistence_SQL($this->db->connection); $m_u = new Model($db, 'user'); $m_u->addField('contact_id'); $m_u->addField('name'); $j = $m_u->join('contact'); $j->addField('contact_phone'); $c = $j->join('country'); $c->addField('country_name', ['actual' => 'name']); $m_u->load(10); $m_u->delete(); $m_u->loadBy('country_name', 'US'); $this->assertEquals(30, $m_u->id); $this->assertEquals(['user' => [20 => ['id' => 20, 'name' => 'Peter', 'contact_id' => 100], 30 => ['id' => 30, 'name' => 'XX', 'contact_id' => 200], 40 => ['id' => 40, 'name' => 'YYY', 'contact_id' => 300]], 'contact' => [200 => ['id' => 200, 'contact_phone' => '+999', 'country_id' => 2], 300 => ['id' => 300, 'contact_phone' => '+777', 'country_id' => 5]], 'country' => [2 => ['id' => 2, 'name' => 'US'], 3 => ['id' => 3, 'name' => 'India']]], $this->getDB()); }
public function testReloading() { $a = ['math' => [['a' => 2, 'b' => 2]]]; $this->setDB($a); $db = new Persistence_SQL($this->db->connection); $m = new Model($db, 'math'); $m->addFields(['a', 'b']); $m->addExpression('sum', '[a] + [b]'); $m->load(1); $this->assertEquals(4, $m['sum']); $m->save(['a' => 3]); $this->assertEquals(5, $m['sum']); $this->assertEquals(9, $m->unload()->save(['a' => 4, 'b' => 5])->get('sum')); $this->setDB($a); $m = new Model($db, ['math', 'reload_after_save' => false]); $m->addFields(['a', 'b']); $m->addExpression('sum', '[a] + [b]'); $m->load(1); $this->assertEquals(4, $m['sum']); $m->save(['a' => 3]); $this->assertEquals(4, $m['sum']); $this->assertEquals(null, $m->unload()->save(['a' => 4, 'b' => 5])->get('sum')); }
/** * @expectedException Exception */ public function testLoadMissing() { $a = ['user' => [2 => ['id' => 2, 'name' => 'Peter', 'contact_id' => 1], 3 => ['id' => 3, 'name' => 'XX', 'contact_id' => 2], 4 => ['id' => 4, 'name' => 'YYY', 'contact_id' => 3]], 'contact' => [2 => ['id' => 2, 'contact_phone' => '+999'], 3 => ['id' => 3, 'contact_phone' => '+777']]]; $db = new Persistence_Array($a); $m_u = new Model($db, 'user'); $m_u->addField('contact_id'); $m_u->addField('name'); $j = $m_u->join('contact'); $j->addField('contact_phone'); $m_u->load(2); }
public function testTypeCustom1() { $a = ['types' => [['date' => '2013-02-20', 'datetime' => '2013-02-20 20:00:12', 'time' => '12:00:50', 'b1' => 'Y', 'b2' => 'N', 'integer' => '2940', 'money' => '8.20', 'float' => '8.202343', 'rot13' => 'uryyb jbeyq']]]; $this->setDB($a); $db = new Persistence_SQL($this->db->connection); date_default_timezone_set('Asia/Seoul'); $m = new Model($db, ['table' => 'types']); $m->addField('date', ['type' => 'date', 'dateTimeClass' => '\\atk4\\data\\tests\\MyDate']); $m->addField('datetime', ['type' => 'datetime', 'dateTimeClass' => '\\atk4\\data\\tests\\MyDateTime']); $m->addField('time', ['type' => 'time', 'dateTimeClass' => '\\atk4\\data\\tests\\MyTime']); $m->addField('b1', ['type' => 'boolean', 'enum' => ['N', 'Y']]); $m->addField('b2', ['type' => 'boolean', 'enum' => ['N', 'Y']]); $m->addField('money', ['type' => 'money']); $m->addField('float', ['type' => 'float']); $m->addField('integer', ['type' => 'integer']); $rot = function ($v) { return str_rot13($v); }; $m->addField('rot13', ['typecast' => [$rot, $rot]]); $m->load(1); $this->assertSame('hello world', $m['rot13']); $this->assertSame('1', $m->id); $this->assertSame('1', $m['id']); $this->assertEquals('2013-02-21 05:00:12', (string) $m['datetime']); $this->assertEquals('2013-02-20', (string) $m['date']); $this->assertEquals('12:00:50', (string) $m['time']); $this->assertEquals(true, $m['b1']); $this->assertEquals(false, $m['b2']); $m->duplicate()->save()->delete(1); $a = ['types' => [2 => ['id' => '2', 'date' => '2013-02-20', 'datetime' => '2013-02-20 20:00:12', 'time' => '12:00:50', 'b1' => 'Y', 'b2' => 'N', 'integer' => '2940', 'money' => '8.20', 'float' => '8.202343', 'rot13' => 'uryyb jbeyq']]]; $this->assertEquals($a, $this->getDB()); }
/** * Model is not associated with any database - persistence should be set. * * @expectedException Exception */ public function testException4() { $m = new Model(); $m->load(1); }
public function testTitle() { $db = new Persistence_SQL($this->db->connection); $a = ['user' => [1 => ['id' => 1, 'name' => 'John', 'surname' => 'Smith', 'category_id' => 2]], 'category' => [1 => ['id' => 1, 'name' => 'General'], 2 => ['id' => 2, 'name' => 'Programmer'], 3 => ['id' => 3, 'name' => 'Sales']]]; $this->setDB($a); $c = new Model($db, 'category'); $c->addField('name'); $m = new Model($db, 'user'); $m->addField('name'); $m->hasOne('category_id', $c)->addTitle(); $m->load(1); $this->assertEquals('John', $m['name']); $this->assertEquals('Programmer', $m['category']); $m->insert(['Peter', 'category' => 'Sales']); $a = ['user' => [1 => ['id' => 1, 'name' => 'John', 'surname' => 'Smith', 'category_id' => 2], 2 => ['id' => 2, 'name' => 'Peter', 'surname' => null, 'category_id' => 3]], 'category' => [1 => ['id' => 1, 'name' => 'General'], 2 => ['id' => 2, 'name' => 'Programmer'], 3 => ['id' => 3, 'name' => 'Sales']]]; $this->assertEquals($a, $this->getDB()); }