public function testSingleField() { $new = Galleries::create(array('name' => 'People')); $key = Galleries::meta('key'); $new->save(); $id = is_object($new->{$key}) ? (string) $new->{$key} : $new->{$key}; $entity = Galleries::first($id); $this->assertInstanceOf('lithium\\data\\Entity', $entity); $expected = array($key => $id, 'name' => 'People', 'active' => true); $result = $entity->data(); $this->assertEqual($expected, array_filter($result)); $entity = Galleries::first(array('conditions' => array($key => $id), 'fields' => array($key))); $this->assertInstanceOf('lithium\\data\\Entity', $entity); $expected = array($key => $id); $result = $entity->data(); $this->assertEqual($expected, $result); $entity = Galleries::find('first', array('conditions' => array($key => $id), 'fields' => array($key, 'name'))); $this->assertInstanceOf('lithium\\data\\Entity', $entity); $entity->name = 'Celebrities'; $result = $entity->save(); $this->assertTrue($result); $entity = Galleries::find('first', array('conditions' => array($key => $id), 'fields' => array($key, 'name'))); $this->assertEqual($entity->name, 'Celebrities'); $new->delete(); }
public function testUpdateWithNewArray() { $new = Galleries::create(array('name' => 'Poneys', 'active' => true)); $expected = array('name' => 'Poneys', 'active' => true); $result = $new->data(); $this->assertEqual($expected, $result); $new->foo = array('bar'); $expected = array('name' => 'Poneys', 'active' => true, 'foo' => array('bar')); $result = $new->data(); $this->assertEqual($expected, $result); $this->assertTrue($new->save()); $updated = Galleries::find((string) $new->_id); $expected = 'bar'; $result = $updated->foo[0]; $this->assertEqual($expected, $result); $updated->foo[1] = 'baz'; $this->assertTrue($updated->save()); $updated = Galleries::find((string) $updated->_id); $expected = 'baz'; $result = $updated->foo[1]; $this->assertEqual($expected, $result); }
public function testGroup() { $field = $this->_db->name('Images.id'); $galleries = Galleries::find('all', array('fields' => array(array("count({$field}) AS count")), 'with' => 'Images', 'group' => array('Galleries.id'), 'order' => array('Galleries.id' => 'ASC'))); $this->assertCount(2, $galleries); $expected = array(3, 2); foreach ($galleries as $gallery) { $this->assertEqual(current($expected), $gallery->count); next($expected); } }
/** * Tests that even when using a subquery the correct * number of records is returned. * * @link https://github.com/UnionOfRAD/lithium/issues/1209 */ public function testSubqueryWithMultipleRecords() { $results = Galleries::find('all', array('fields' => array('name', '(SELECT 23) AS number'))); $this->assertEqual(2, $results->count()); }
/** * Contrary to other data sources, MySQL only support one TIMESTAMP column by table. */ public function testDefaultValues() { $this->_db->dropSchema('galleries'); $schema = new Schema(array('fields' => array('id' => array('type' => 'id'), 'name' => array('type' => 'string', 'length' => 255, 'default' => 'image'), 'active' => array('type' => 'boolean', 'default' => false), 'show' => array('type' => 'boolean', 'default' => true), 'empty' => array('type' => 'text', 'null' => true), 'created' => array('type' => 'timestamp', 'null' => true, 'default' => (object) 'CURRENT_TIMESTAMP')))); $this->_db->createSchema('galleries', $schema); $gallery = Galleries::create(); $this->assertEqual('image', $gallery->name); $this->assertEqual(false, $gallery->active); $this->assertEqual(true, $gallery->show); $this->assertEqual(null, $gallery->empty); $gallery->save(); $result = Galleries::find('first')->data(); $this->assertEqual(1, $result['id']); $this->assertEqual('image', $result['name']); $this->assertEqual(false, $result['active']); $this->assertEqual(true, $result['show']); $this->assertEqual(null, $result['empty']); $this->assertPattern('$\\d{4}-\\d\\d-\\d\\d \\d\\d:\\d\\d:\\d\\d$', $result['created']); $this->assertTrue(time() - strtotime($result['created']) < 24 * 3600); }
public function testUpdateWithNewProperties() { $db = $this->_db; $this->skipIf($db::enabled('schema')); $new = Galleries::create(array('name' => 'Flowers', 'active' => true)); $expected = array('name' => 'Flowers', 'active' => true); $result = $new->data(); $this->assertEqual($expected, $result); $new->foo = 'bar'; $expected = array('name' => 'Flowers', 'active' => true, 'foo' => 'bar'); $result = $new->data(); $this->assertEqual($expected, $result); $this->assertTrue($new->save()); $updated = Galleries::find('first', array('conditions' => Galleries::key($new))); $expected = 'bar'; $result = $updated->foo; $this->assertEqual($expected, $result); }
/** * Tests that a record can be created, saved, and subsequently re-read using a key * auto-generated by the data source. Uses short-hand `find()` syntax which does not support * compound keys. */ public function testGetRecordByGeneratedId() { $key = Galleries::meta('key'); $galleries = Galleries::create(array('name' => 'Test Galleries')); $this->assertTrue($galleries->save()); $id = (string) $galleries->{$key}; $galleriesCopy = Galleries::find($id)->data(); $data = $galleries->data(); foreach ($data as $key => $value) { $this->assertTrue(isset($galleriesCopy[$key])); $this->assertEqual($data[$key], $galleriesCopy[$key]); } }
public function testIterateOverEmptyResultSet() { $data = Galleries::find('all', array('conditions' => array('name' => 'no match'))); $result = next($data); $this->assertNull($result); }