Inheritance: extends Persistence
Example #1
0
 public function testBasicSerialize()
 {
     $db = new Persistence_SQL($this->db->connection);
     $m = new Model($db, 'job');
     $f = $m->addField('data', ['serialize' => 'serialize']);
     $this->assertEquals(['data' => 'a:1:{s:3:"foo";s:3:"bar";}'], $db->typecastSaveRow($m, ['data' => ['foo' => 'bar']]));
     $f->serialize = 'json';
     $this->assertEquals(['data' => '{"foo":"bar"}'], $db->typecastSaveRow($m, ['data' => ['foo' => 'bar']]));
 }
Example #2
0
 public function testQuery()
 {
     $a = ['invoice' => [['total_net' => 10, 'total_vat' => 1.23], ['total_net' => 20, 'total_vat' => 2.46]]];
     $this->setDB($a);
     $db = new Persistence_SQL($this->db->connection);
     $i = (new Model($db, 'invoice'))->addFields(['total_net', 'total_vat']);
     $i->addExpression('sum_net', $i->action('fx', ['sum', 'total_net']));
     $this->assertEquals('select `id`,`total_net`,`total_vat`,(select sum(`total_net`) from `invoice`) `sum_net` from `invoice`', $i->action('select')->render());
     $i->tryLoad(1);
     $this->assertEquals(10, $i['total_net']);
     $this->assertEquals(30, $i['sum_net']);
     $q = $db->dsql();
     $q->field($i->action('count'), 'total_orders');
     $q->field($i->action('fx', ['sum', 'total_net']), 'total_net');
     $this->assertEquals(['total_orders' => 2, 'total_net' => 30], $q->getRow());
 }
Example #3
0
 public function testPersistenceDelete()
 {
     $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');
     $ids = [];
     foreach ($a['user'] as $id => $row) {
         $ids[] = $p->insert($m, $row);
     }
     $this->assertEquals(false, $m->loaded());
     $m->delete($ids[0]);
     $this->assertEquals(false, $m->loaded());
     $m->load($ids[1]);
     $this->assertEquals('Jones', $m['surname']);
     $m['surname'] = 'Smith';
     $m->save();
     $m->tryLoad($ids[0]);
     $this->assertEquals(false, $m->loaded());
     $m->load($ids[1]);
     $this->assertEquals('Smith', $m['surname']);
 }
Example #4
0
 public function testTypecastTimezone()
 {
     $db = new Persistence_SQL($this->db->connection);
     $m = new Model($db, 'event');
     $dt = $m->addField('dt', ['type' => 'datetime', 'persist_timezone' => 'EEST']);
     $d = $m->addField('d', ['type' => 'date', 'persist_timezone' => 'EEST']);
     $t = $m->addField('t', ['type' => 'time', 'persist_timezone' => 'EEST']);
     date_default_timezone_set('UTC');
     $s = new \DateTime('Monday, 15-Aug-05 22:52:01 UTC');
     $this->assertEquals('2005-08-16 00:52:01', $db->typecastSaveField($dt, $s));
     $this->assertEquals('2005-08-15', $db->typecastSaveField($d, $s));
     $this->assertEquals('22:52:01', $db->typecastSaveField($t, $s));
     $this->assertEquals(new \DateTime('Monday, 15-Aug-05 22:52:01 UTC'), $db->typecastLoadField($dt, '2005-08-16 00:52:01'));
     $this->assertEquals(new \DateTime('Monday, 15-Aug-05'), $db->typecastLoadField($d, '2005-08-15'));
     $this->assertEquals(new \DateTime('22:52:01'), $db->typecastLoadField($t, '22:52:01'));
     date_default_timezone_set('Asia/Tokyo');
     $s = new \DateTime('Monday, 15-Aug-05 22:52:01 UTC');
     $this->assertEquals('2005-08-16 00:52:01', $db->typecastSaveField($dt, $s));
     $this->assertEquals('2005-08-15', $db->typecastSaveField($d, $s));
     $this->assertEquals('22:52:01', $db->typecastSaveField($t, $s));
     $this->assertEquals(new \DateTime('Monday, 15-Aug-05 22:52:01 UTC'), $db->typecastLoadField($dt, '2005-08-16 00:52:01'));
     $this->assertEquals(new \DateTime('Monday, 15-Aug-05'), $db->typecastLoadField($d, '2005-08-15'));
     $this->assertEquals(new \DateTime('22:52:01'), $db->typecastLoadField($t, '22:52:01'));
     date_default_timezone_set('America/Los_Angeles');
     $s = new \DateTime('Monday, 15-Aug-05 22:52:01');
     // uses servers default timezone
     $this->assertEquals('2005-08-16 07:52:01', $db->typecastSaveField($dt, $s));
     $this->assertEquals('2005-08-15', $db->typecastSaveField($d, $s));
     $this->assertEquals('22:52:01', $db->typecastSaveField($t, $s));
     $this->assertEquals(new \DateTime('Monday, 15-Aug-05 22:52:01 America/Los_Angeles'), $db->typecastLoadField($dt, '2005-08-16 07:52:01'));
     $this->assertEquals(new \DateTime('Monday, 15-Aug-05'), $db->typecastLoadField($d, '2005-08-15'));
     $this->assertEquals(new \DateTime('22:52:01'), $db->typecastLoadField($t, '22:52:01'));
 }