diff --git a/tests/integration/data/DatabaseTest.php b/tests/integration/data/DatabaseTest.php index b0f48c4de8..97ffca0420 100644 --- a/tests/integration/data/DatabaseTest.php +++ b/tests/integration/data/DatabaseTest.php @@ -21,28 +21,45 @@ class DatabaseTest extends \lithium\test\Integration { protected $_dbConfig; public $images = array( - array( - 'id' => null, - 'gallery_id' => null, + 1 => array( + 'id' => 1, + 'gallery_id' => 1, 'image' => 'someimage.png', 'title' => 'Image1 Title' ), - array( - 'id' => null, - 'gallery_id' => null, + 2 => array( + 'id' => 2, + 'gallery_id' => 1, 'image' => 'anotherImage.jpg', 'title' => 'Our Vacation' ), - array( - 'id' => null, - 'gallery_id' => null, + 3 => array( + 'id' => 3, + 'gallery_id' => 1, 'image' => 'me.bmp', 'title' => 'Me.' + ), + 4 => array( + 'id' => 4, + 'gallery_id' => 2, + 'image' => 'picture.jpg', + 'title' => 'Obi-Wan Kenobi' + ), + 5 => array( + 'id' => 5, + 'gallery_id' => 2, + 'image' => 'unknown.gif', + 'title' => 'John Doe' ) ); public $gallery = array('name' => 'Foo Gallery'); + public $galleries = array( + 1 => array('id' => 1, 'name' => 'Foo Gallery'), + 2 => array('id' => 2, 'name' => 'Bar Gallery') + ); + public function setUp() { $mockBase = LITHIUM_LIBRARY_PATH . '/lithium/tests/mocks/data/source/database/adapter/'; $files = array('galleries' => '_galleries.sql', 'images' => '_images.sql'); @@ -84,20 +101,19 @@ public function skip() { public function testCreateData() { $gallery = Galleries::create($this->gallery); $this->assertTrue($gallery->save()); - $this->gallery = array('id' => $gallery->id) + $this->gallery; + $this->assertTrue($gallery->id); foreach ($this->images as $key => $image) { unset($image['id'], $image['gallery_id']); $img = Images::create($image + array('gallery_id' => $gallery->id)); $this->assertEqual(true, $img->save()); - $this->images[$key]['id'] = $img->id; - $this->images[$key]['gallery_id'] = $gallery->id; + $this->assertEqual($gallery->id, $img->gallery_id); } } public function testManyToOne() { - $this->_createGalleryWithImages(); - $opts = array('conditions' => array('gallery_id' => $this->gallery['id'])); + $this->_createGalleriesWithImages(); + $opts = array('conditions' => array('gallery_id' => 1)); $query = new Query($opts + array( 'type' => 'read', @@ -109,10 +125,11 @@ public function testManyToOne() { $images = $this->db->read($query)->data(); reset($this->images); + $this->assertEqual(3, count($images)); foreach ($images as $key => $image) { $expect = current($this->images) + array( - 'gallery_id' => $this->gallery['id'], - 'gallery' => $this->gallery + 'gallery_id' => 1, + 'gallery' => $this->galleries[1] ); $this->assertEqual($expect, $image); next($this->images); @@ -122,7 +139,7 @@ public function testManyToOne() { reset($this->images); foreach ($images as $key => $image) { - $expect = (array) current($this->images) + array('gallery' => $this->gallery); + $expect = (array) current($this->images) + array('gallery' => $this->galleries[1]); ksort($expect); ksort($image); $this->assertEqual($expect, $image); @@ -131,8 +148,8 @@ public function testManyToOne() { } public function testOneToMany() { - $this->_createGalleryWithImages(); - $opts = array('conditions' => array('Galleries.id' => $this->gallery['id'])); + $this->_createGalleriesWithImages(); + $opts = array('conditions' => array('Galleries.id' => 1)); $query = new Query($opts + array( 'type' => 'read', @@ -143,19 +160,24 @@ public function testOneToMany() { )); $galleries = $this->db->read($query)->data(); + $images = array($this->images[1], $this->images[2], $this->images[3]); + + $this->assertEqual(1, count($galleries)); foreach ($galleries as $key => $gallery) { - $expect = $this->gallery + array('images' => $this->images); + $expect = $this->galleries[1] + array( + 'images' => $images + ); $this->assertEqual($expect, $gallery); } $gallery = Galleries::find('first', $opts + array('with' => 'Images'))->data(); - $expect = $this->gallery + array('images' => $this->images); + $expect = $this->galleries[1] + array('images' => $images); $this->assertEqual($expect, $gallery); } public function testUpdate() { - $this->_createGalleryWithImages(); - $options = array('conditions' => array('gallery_id' => $this->gallery['id'])); + $this->_createGalleriesWithImages(); + $options = array('conditions' => array('gallery_id' => 1)); $uuid = String::uuid(); $image = Images::find('first', $options); $image->title = $uuid; @@ -170,31 +192,51 @@ public function testUpdate() { } public function testFields() { - $this->_createGalleryWithImages(); + $this->_createGalleriesWithImages(); $fields = array('id', 'image'); $image = Images::find('first', array( 'fields' => $fields, 'conditions' => array( - 'gallery_id' => $this->gallery['id'] + 'gallery_id' => 1 ) )); $this->assertEqual($fields, array_keys($image->data())); } public function testOrder() { - $this->_createGalleryWithImages(); + $this->_createGalleriesWithImages(); $images = Images::find('all', array( 'order' => 'id DESC', 'conditions' => array( - 'gallery_id' => $this->gallery['id'] + 'gallery_id' => 1 ) ))->data(); - krsort($this->images); - reset($this->images); + $expected = array($this->images[3], $this->images[2], $this->images[1]); + + $this->assertEqual(3, count($images)); foreach ($images as $image) { - $this->assertEqual(current($this->images), $image); - next($this->images); + $this->assertEqual(current($expected), $image); + next($expected); + } + } + + public function testGroup() { + $this->_createGalleriesWithImages(); + + $galleries = Galleries::find('all', array( + 'fields' => array(array('count(Images.id) AS count')), + 'with' => 'Images', + 'group' => array('Galleries.id'), + 'order' => array('Galleries.id' => 'ASC'), + )); + + $this->assertEqual(2, count($galleries)); + $expected = array(3, 2); + + foreach($galleries as $gallery) { + $this->assertEqual(current($expected), $gallery->count); + next($expected); } } @@ -203,22 +245,19 @@ public function testRemove() { $this->assertTrue(Images::remove()); } - protected function _createGallery() { - $gallery = Galleries::create($this->gallery); - $gallery->save(); - return $gallery; + protected function _createGalleries() { + foreach ($this->galleries as $key => $gallery) { + $entity = Galleries::create($gallery); + $this->assertEqual(true, $entity->save()); + } } - protected function _createGalleryWithImages() { - $gallery = $this->_createGallery(); - + protected function _createGalleriesWithImages() { + $this->_createGalleries(); foreach ($this->images as $key => $image) { - unset($image['id'], $image['gallery_id']); - $img = Images::create($image + array('gallery_id' => $gallery->id)); - $this->assertEqual(true, $img->save()); - $this->images[$key]['id'] = $img->id; - $this->images[$key]['gallery_id'] = $gallery->id; + $entity = Images::create($image); + $this->assertEqual(true, $entity->save()); } } } -?> \ No newline at end of file +?>