SourceTest.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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\tests\mocks\data\Companies;
  11. use lithium\tests\mocks\data\Employees;
  12. class SourceTest extends \lithium\test\Integration {
  13. protected $_database = null;
  14. protected $_connection = null;
  15. protected $_classes = array(
  16. 'employees' => 'lithium\tests\mocks\data\Employees',
  17. 'companies' => 'lithium\tests\mocks\data\Companies'
  18. );
  19. public $companiesData = array(
  20. array('name' => 'StuffMart', 'active' => true),
  21. array('name' => 'Ma \'n Pa\'s Data Warehousing & Bait Shop', 'active' => false)
  22. );
  23. /**
  24. * Creating the test database
  25. */
  26. public function setUp() {
  27. $this->_connection->connection->put($this->_database);
  28. }
  29. /**
  30. * Dropping the test database
  31. */
  32. public function tearDown() {
  33. $this->_connection->connection->delete($this->_database);
  34. }
  35. /**
  36. * Skip the test if no test database connection available.
  37. */
  38. public function skip() {
  39. $connection = 'lithium_couch_test';
  40. $config = Connections::get($connection, array('config' => true));
  41. $isConnected = $config && Connections::get($connection)->isConnected(array(
  42. 'autoConnect' => true
  43. ));
  44. $isAvailable = $config && $isConnected;
  45. $this->skipIf(!$isAvailable, "No {$connection} connection available.");
  46. $this->_key = Companies::key();
  47. $this->_database = $config['database'];
  48. $this->_connection = Connections::get($connection);
  49. }
  50. /**
  51. * Tests that a single record with a manually specified primary key can be created, persisted
  52. * to an arbitrary data store, re-read and updated.
  53. */
  54. public function testSingleReadWriteWithKey() {
  55. $key = Companies::meta('key');
  56. $new = Companies::create(array($key => 12345, 'name' => 'Acme, Inc.'));
  57. $result = $new->data();
  58. $expected = array($key => 12345, 'name' => 'Acme, Inc.');
  59. $this->assertEqual($expected[$key], $result[$key]);
  60. $this->assertEqual($expected['name'], $result['name']);
  61. $this->assertFalse($new->exists());
  62. $this->assertTrue($new->save());
  63. $this->assertTrue($new->exists());
  64. $existing = Companies::find(12345);
  65. $result = $existing->data();
  66. $this->assertEqual($expected[$key], $result[$key]);
  67. $this->assertEqual($expected['name'], $result['name']);
  68. $this->assertTrue($existing->exists());
  69. $existing->name = 'Big Brother and the Holding Companies';
  70. $result = $existing->save();
  71. $this->assertTrue($result);
  72. $existing = Companies::find(12345);
  73. $result = $existing->data();
  74. $expected['name'] = 'Big Brother and the Holding Companies';
  75. $this->assertEqual($expected[$key], $result[$key]);
  76. $this->assertEqual($expected['name'], $result['name']);
  77. $this->assertTrue($existing->delete());
  78. }
  79. public function testRewind() {
  80. $key = Companies::meta('key');
  81. $new = Companies::create(array($key => 12345, 'name' => 'Acme, Inc.'));
  82. $result = $new->data();
  83. $this->assertTrue($result !== null);
  84. $this->assertTrue($new->save());
  85. $this->assertTrue($new->exists());
  86. $result = Companies::all(12345);
  87. $this->assertTrue($result !== null);
  88. $result = $result->rewind();
  89. $this->assertTrue($result !== null);
  90. $this->assertTrue(!is_string($result));
  91. }
  92. public function testFindFirstWithFieldsOption() {
  93. return;
  94. $key = Companies::meta('key');
  95. $new = Companies::create(array($key => 1111, 'name' => 'Test find first with fields.'));
  96. $result = $new->data();
  97. $expected = array($key => 1111, 'name' => 'Test find first with fields.');
  98. $this->assertEqual($expected['name'], $result['name']);
  99. $this->assertEqual($expected[$key], $result[$key]);
  100. $this->assertFalse($new->exists());
  101. $this->assertTrue($new->save());
  102. $this->assertTrue($new->exists());
  103. $result = Companies::find('first', array('fields' => array('name')));
  104. $this->assertFalse(is_null($result));
  105. $this->skipIf(is_null($result), 'No result returned to test');
  106. $result = $result->data();
  107. $this->assertEqual($expected['name'], $result['name']);
  108. $this->assertTrue($new->delete());
  109. }
  110. public function testReadWriteMultiple() {
  111. $companies = array();
  112. $key = Companies::meta('key');
  113. foreach ($this->companiesData as $data) {
  114. $companies[] = Companies::create($data);
  115. $this->assertTrue(end($companies)->save());
  116. $this->assertTrue(end($companies)->$key);
  117. }
  118. $this->assertIdentical(2, Companies::count());
  119. $this->assertIdentical(1, Companies::count(array('active' => true)));
  120. $this->assertIdentical(1, Companies::count(array('active' => false)));
  121. $this->assertIdentical(0, Companies::count(array('active' => null)));
  122. $all = Companies::all();
  123. $this->assertIdentical(2, Companies::count());
  124. $expected = count($this->companiesData);
  125. $this->assertEqual($expected, $all->count());
  126. $this->assertEqual($expected, count($all));
  127. $id = (string) $all->first()->{$key};
  128. $this->assertTrue(strlen($id) > 0);
  129. $this->assertTrue($all->data());
  130. foreach ($companies as $companies) {
  131. $this->assertTrue($companies->delete());
  132. }
  133. $this->assertIdentical(0, Companies::count());
  134. }
  135. public function testEntityFields() {
  136. foreach ($this->companiesData as $data) {
  137. Companies::create($data)->save();
  138. }
  139. $all = Companies::all();
  140. $result = $all->first(function($doc) { return $doc->name === 'StuffMart'; });
  141. $this->assertEqual('StuffMart', $result->name);
  142. $result = $result->data();
  143. $this->assertEqual('StuffMart', $result['name']);
  144. $result = $all->next();
  145. $this->assertEqual('Ma \'n Pa\'s Data Warehousing & Bait Shop', $result->name);
  146. $result = $result->data();
  147. $this->assertEqual('Ma \'n Pa\'s Data Warehousing & Bait Shop', $result['name']);
  148. $this->assertNull($all->next());
  149. }
  150. /**
  151. * Tests that a record can be created, saved, and subsequently re-read using a key
  152. * auto-generated by the data source. Uses short-hand `find()` syntax which does not support
  153. * compound keys.
  154. *
  155. * @return void
  156. */
  157. public function testGetRecordByGeneratedId() {
  158. $key = Companies::meta('key');
  159. $companies = Companies::create(array('name' => 'Test Companies'));
  160. $this->assertTrue($companies->save());
  161. $id = (string) $companies->{$key};
  162. $companiesCopy = Companies::find($id)->data();
  163. $data = $companies->data();
  164. foreach ($data as $key => $value) {
  165. $this->assertTrue(isset($companiesCopy[$key]));
  166. $this->assertEqual($data[$key], $companiesCopy[$key]);
  167. }
  168. }
  169. /**
  170. * Tests the default relationship information provided by the backend data source.
  171. *
  172. * @return void
  173. */
  174. public function testDefaultRelationshipInfo() {
  175. $connection = $this->_connection;
  176. $message = "Relationships are not supported by this adapter.";
  177. $this->skipIf(!$connection::enabled('relationships'), $message);
  178. $this->assertEqual(array('Employeess'), array_keys(Companies::relations()));
  179. $this->assertEqual(array('Companies'), array_keys(Employees::relations()));
  180. $this->assertEqual(array('Employeess'), Companies::relations('hasMany'));
  181. $this->assertEqual(array('Companies'), Employees::relations('belongsTo'));
  182. $this->assertFalse(Companies::relations('belongsTo'));
  183. $this->assertFalse(Companies::relations('hasOne'));
  184. $this->assertFalse(Employees::relations('hasMany'));
  185. $this->assertFalse(Employees::relations('hasOne'));
  186. $result = Companies::relations('Employeess');
  187. $this->assertEqual('hasMany', $result->data('type'));
  188. $this->assertEqual($this->_classes['employees'], $result->data('to'));
  189. }
  190. public function testAbstractTypeHandling() {
  191. $key = Companies::meta('key');
  192. foreach ($this->companiesData as $data) {
  193. $companies[] = Companies::create($data);
  194. $this->assertTrue(end($companies)->save());
  195. $this->assertTrue(end($companies)->{$key});
  196. }
  197. foreach (Companies::all() as $companies) {
  198. $this->assertTrue($companies->delete());
  199. }
  200. }
  201. }
  202. ?>