DatabaseTest.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. <?php
  2. /**
  3. * Lithium: the most rad php framework
  4. *
  5. * @copyright Copyright 2013, Union of RAD (http://union-of-rad.org)
  6. * @license http://opensource.org/licenses/bsd-license.php The BSD License
  7. */
  8. namespace lithium\tests\integration\data;
  9. use lithium\data\Connections;
  10. use lithium\data\model\Query;
  11. use lithium\data\source\Database;
  12. use lithium\tests\mocks\data\source\Images;
  13. use lithium\tests\mocks\data\source\Galleries;
  14. use lithium\util\String;
  15. class DatabaseTest extends \lithium\test\Integration {
  16. public $db = null;
  17. protected $_dbConfig;
  18. public $images = array(
  19. 1 => array(
  20. 'id' => 1,
  21. 'gallery_id' => 1,
  22. 'image' => 'someimage.png',
  23. 'title' => 'Image1 Title'
  24. ),
  25. 2 => array(
  26. 'id' => 2,
  27. 'gallery_id' => 1,
  28. 'image' => 'anotherImage.jpg',
  29. 'title' => 'Our Vacation'
  30. ),
  31. 3 => array(
  32. 'id' => 3,
  33. 'gallery_id' => 1,
  34. 'image' => 'me.bmp',
  35. 'title' => 'Me.'
  36. ),
  37. 4 => array(
  38. 'id' => 4,
  39. 'gallery_id' => 2,
  40. 'image' => 'picture.jpg',
  41. 'title' => 'Obi-Wan Kenobi'
  42. ),
  43. 5 => array(
  44. 'id' => 5,
  45. 'gallery_id' => 2,
  46. 'image' => 'unknown.gif',
  47. 'title' => 'John Doe'
  48. )
  49. );
  50. public $gallery = array('name' => 'Foo Gallery');
  51. public $galleries = array(
  52. 1 => array('id' => 1, 'name' => 'Foo Gallery'),
  53. 2 => array('id' => 2, 'name' => 'Bar Gallery')
  54. );
  55. public function setUp() {
  56. $mockBase = LITHIUM_LIBRARY_PATH . '/lithium/tests/mocks/data/source/database/adapter/';
  57. $files = array('galleries' => '_galleries.sql', 'images' => '_images.sql');
  58. $files = array_diff_key($files, array_flip($this->db->sources()));
  59. foreach ($files as $file) {
  60. $sqlFile = $mockBase . strtolower($this->_dbConfig['adapter']) . $file;
  61. $this->skipIf(!file_exists($sqlFile), "SQL file $sqlFile does not exist.");
  62. $sql = file_get_contents($sqlFile);
  63. $this->db->read($sql, array('return' => 'resource'));
  64. }
  65. }
  66. public function tearDown() {
  67. $this->db->read('DROP TABLE IF EXISTS `images`;');
  68. $this->db->read('DROP TABLE IF EXISTS `galleries`;');
  69. }
  70. public function skip() {
  71. $connection = 'lithium_mysql_test';
  72. $this->_dbConfig = Connections::get($connection, array(
  73. 'config' => true
  74. ));
  75. $isConnected = $this->_dbConfig && Connections::get($connection)->isConnected(array(
  76. 'autoConnect' => true
  77. ));
  78. $isAvailable = $this->_dbConfig && $isConnected;
  79. $this->skipIf(!$isAvailable, "No {$connection} connection available.");
  80. $this->db = Connections::get($connection);
  81. $this->skipIf(
  82. !($this->db instanceof Database),
  83. "The {$connection} connection is not a relational database."
  84. );
  85. }
  86. public function testConnectWithNoDatabase() {
  87. $config = $this->_dbConfig;
  88. $config['database'] = null;
  89. $connection = 'no_database';
  90. Connections::add($connection, $config);
  91. $this->expectException("/No Database configured/");
  92. Connections::get($connection)->connect();
  93. }
  94. public function testConnectWithWrongHost() {
  95. $config = $this->_dbConfig;
  96. $config['host'] = 'unknown.host.nowhere';
  97. $connection = 'wrong_host';
  98. Connections::add($connection, $config);
  99. $this->expectException('/Unable to connect to host `unknown.host.nowhere`/');
  100. Connections::get($connection)->connect();
  101. }
  102. public function testConnectWithWrongPassword() {
  103. $config = $this->_dbConfig;
  104. $config['login'] = 'wrong_login';
  105. $config['password'] = 'wrong_pass';
  106. $connection = 'wrong_passord';
  107. Connections::add($connection, $config);
  108. $this->expectException('/Host connected, but could not access database/');
  109. Connections::get($connection)->connect();
  110. }
  111. public function testExecuteException() {
  112. $this->expectException("/You have an error(.*?)near '\* FROM table'/");
  113. $this->db->read('SELECT * FROM * FROM table');
  114. }
  115. public function testCreateData() {
  116. $gallery = Galleries::create($this->gallery);
  117. $this->assertTrue($gallery->save());
  118. $this->assertTrue($gallery->id);
  119. foreach ($this->images as $key => $image) {
  120. unset($image['id'], $image['gallery_id']);
  121. $img = Images::create($image + array('gallery_id' => $gallery->id));
  122. $this->assertEqual(true, $img->save());
  123. $this->assertEqual($gallery->id, $img->gallery_id);
  124. }
  125. }
  126. public function testManyToOne() {
  127. $this->_createGalleriesWithImages();
  128. $opts = array('conditions' => array('gallery_id' => 1));
  129. $query = new Query($opts + array(
  130. 'type' => 'read',
  131. 'model' => 'lithium\tests\mocks\data\source\Images',
  132. 'source' => 'images',
  133. 'alias' => 'Images',
  134. 'with' => array('Galleries')
  135. ));
  136. $images = $this->db->read($query)->data();
  137. reset($this->images);
  138. $this->assertEqual(3, count($images));
  139. foreach ($images as $key => $image) {
  140. $expect = current($this->images) + array(
  141. 'gallery_id' => 1,
  142. 'gallery' => $this->galleries[1]
  143. );
  144. $this->assertEqual($expect, $image);
  145. next($this->images);
  146. }
  147. $images = Images::find('all', $opts + array('with' => 'Galleries'))->data();
  148. reset($this->images);
  149. foreach ($images as $key => $image) {
  150. $expect = (array) current($this->images) + array('gallery' => $this->galleries[1]);
  151. ksort($expect);
  152. ksort($image);
  153. $this->assertEqual($expect, $image);
  154. next($this->images);
  155. }
  156. }
  157. public function testOneToMany() {
  158. $this->_createGalleriesWithImages();
  159. $opts = array('conditions' => array('Galleries.id' => 1));
  160. $query = new Query($opts + array(
  161. 'type' => 'read',
  162. 'model' => 'lithium\tests\mocks\data\source\Galleries',
  163. 'source' => 'galleries',
  164. 'alias' => 'Galleries',
  165. 'with' => array('Images')
  166. ));
  167. $galleries = $this->db->read($query)->data();
  168. $images = array(1 => $this->images[1], 2 => $this->images[2], 3 => $this->images[3]);
  169. $this->assertEqual(1, count($galleries));
  170. foreach ($galleries as $key => $gallery) {
  171. $expect = $this->galleries[1] + array(
  172. 'images' => $images
  173. );
  174. $this->assertEqual($expect, $gallery);
  175. }
  176. $gallery = Galleries::find('first', $opts + array('with' => 'Images'))->data();
  177. $expect = $this->galleries[1] + array('images' => $images);
  178. $this->assertEqual($expect, $gallery);
  179. }
  180. public function testUpdate() {
  181. $this->_createGalleriesWithImages();
  182. $options = array('conditions' => array('gallery_id' => 1));
  183. $uuid = String::uuid();
  184. $image = Images::find('first', $options);
  185. $image->title = $uuid;
  186. $firstID = $image->id;
  187. $image->save();
  188. $this->assertEqual($uuid, Images::find('first', $options)->title);
  189. $uuid = String::uuid();
  190. Images::update(array('title' => $uuid), array('id' => $firstID));
  191. $this->assertEqual($uuid, Images::find('first', $options)->title);
  192. $this->images[0]['title'] = $uuid;
  193. }
  194. public function testFields() {
  195. $this->_createGalleriesWithImages();
  196. $fields = array('id', 'image');
  197. $image = Images::find('first', array(
  198. 'fields' => $fields,
  199. 'conditions' => array(
  200. 'gallery_id' => 1
  201. )
  202. ));
  203. $this->assertEqual($fields, array_keys($image->data()));
  204. }
  205. public function testOrder() {
  206. $this->_createGalleriesWithImages();
  207. $images = Images::find('all', array(
  208. 'order' => 'id DESC',
  209. 'conditions' => array(
  210. 'gallery_id' => 1
  211. )
  212. ))->data();
  213. $expected = array($this->images[3], $this->images[2], $this->images[1]);
  214. $this->assertEqual(3, count($images));
  215. foreach ($images as $image) {
  216. $this->assertEqual(current($expected), $image);
  217. next($expected);
  218. }
  219. }
  220. public function testGroup() {
  221. $this->_createGalleriesWithImages();
  222. $galleries = Galleries::find('all', array(
  223. 'fields' => array(array('count(Images.id) AS count')),
  224. 'with' => 'Images',
  225. 'group' => array('Galleries.id'),
  226. 'order' => array('Galleries.id' => 'ASC')
  227. ));
  228. $this->assertEqual(2, count($galleries));
  229. $expected = array(3, 2);
  230. foreach ($galleries as $gallery) {
  231. $this->assertEqual(current($expected), $gallery->count);
  232. next($expected);
  233. }
  234. }
  235. public function testRemove() {
  236. $this->assertTrue(Galleries::remove());
  237. $this->assertTrue(Images::remove());
  238. }
  239. protected function _createGalleries() {
  240. foreach ($this->galleries as $key => $gallery) {
  241. $entity = Galleries::create($gallery);
  242. $this->assertEqual(true, $entity->save());
  243. }
  244. }
  245. protected function _createGalleriesWithImages() {
  246. $this->_createGalleries();
  247. foreach ($this->images as $key => $image) {
  248. $entity = Images::create($image);
  249. $this->assertEqual(true, $entity->save());
  250. }
  251. }
  252. }
  253. ?>