CrudTest.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. class CrudTest extends \lithium\test\Integration {
  12. protected $_connection = null;
  13. protected $_database = null;
  14. protected $_key = null;
  15. public $companyData = array(
  16. array('name' => 'StuffMart', 'active' => true),
  17. array('name' => 'Ma \'n Pa\'s Data Warehousing & Bait Shop', 'active' => false)
  18. );
  19. /**
  20. * Creating the test database
  21. */
  22. public function setUp() {
  23. $this->_connection->connection->put($this->_database);
  24. }
  25. /**
  26. * Dropping the test database
  27. */
  28. public function tearDown() {
  29. $this->_connection->connection->delete($this->_database);
  30. }
  31. /**
  32. * Skip the test if no test database connection available.
  33. */
  34. public function skip() {
  35. $connection = 'lithium_couch_test';
  36. $config = Connections::get($connection, array('config' => true));
  37. $isConnected = $config && Connections::get($connection)->isConnected(array(
  38. 'autoConnect' => true
  39. ));
  40. $isAvailable = $config && $isConnected;
  41. $this->skipIf(!$isAvailable, "No {$connection} connection available.");
  42. $this->_key = Companies::key();
  43. $this->_database = $config['database'];
  44. $this->_connection = Connections::get($connection);
  45. }
  46. /**
  47. * Tests that a single record with a manually specified primary key can be created, persisted
  48. * to an arbitrary data store, re-read and updated.
  49. *
  50. * @return void
  51. */
  52. public function testCreate() {
  53. $this->assertIdentical(0, Companies::count());
  54. $new = Companies::create(array('name' => 'Acme, Inc.', 'active' => true));
  55. $expected = array('name' => 'Acme, Inc.', 'active' => true);
  56. $result = $new->data();
  57. $this->assertEqual($expected, $result);
  58. $this->assertEqual(
  59. array(false, true, true),
  60. array($new->exists(), $new->save(), $new->exists())
  61. );
  62. $this->assertIdentical(1, Companies::count());
  63. }
  64. public function testRead() {
  65. static::_createCompany();
  66. $existing = Companies::first();
  67. foreach (Companies::key($existing) as $val) {
  68. $this->assertTrue($val);
  69. }
  70. $this->assertEqual('Acme, Inc.', $existing->name);
  71. $this->assertTrue($existing->active);
  72. $this->assertTrue($existing->exists());
  73. }
  74. public function testUpdate() {
  75. static::_createCompany();
  76. $existing = Companies::first();
  77. $this->assertEqual($existing->name, 'Acme, Inc.');
  78. $existing->name = 'Big Brother and the Holding Company';
  79. $result = $existing->save();
  80. $this->assertTrue($result);
  81. $existing = Companies::first();
  82. foreach (Companies::key($existing) as $val) {
  83. $this->assertTrue($val);
  84. }
  85. $this->assertTrue($existing->active);
  86. $this->assertEqual('Big Brother and the Holding Company', $existing->name);
  87. }
  88. public function testDelete() {
  89. static::_createCompany();
  90. $existing = Companies::first();
  91. $this->assertTrue($existing->exists());
  92. $this->assertTrue($existing->delete());
  93. $this->assertNull(Companies::first(array('conditions' => Companies::key($existing))));
  94. $this->assertIdentical(0, Companies::count());
  95. }
  96. public function testCrudMulti() {
  97. $large = Companies::create(array('name' => 'BigBoxMart', 'active' => true));
  98. $medium = Companies::create(array('name' => 'Acme, Inc.', 'active' => true));
  99. $small = Companies::create(array('name' => 'Ma & Pa\'s', 'active' => true));
  100. foreach (array('large', 'medium', 'small') as $key) {
  101. $this->assertFalse(${$key}->exists());
  102. $this->assertTrue(${$key}->save());
  103. $this->assertTrue(${$key}->exists());
  104. }
  105. $this->assertEqual(3, Companies::count());
  106. $all = Companies::all();
  107. $this->assertEqual(3, $all->count());
  108. $match = 'BigBoxMart';
  109. $filter = function($entity) use (&$match) { return $entity->name === $match; };
  110. foreach (array('BigBoxMart', 'Acme, Inc.', 'Ma & Pa\'s') as $match) {
  111. $this->assertTrue($all->first($filter)->exists());
  112. }
  113. $this->assertEqual(array(true, true, true), array_values($all->delete()));
  114. $this->assertEqual(0, Companies::count());
  115. }
  116. public function testUpdateWithNewProperties() {
  117. $new = Companies::create(array('name' => 'Acme, Inc.', 'active' => true));
  118. $expected = array('name' => 'Acme, Inc.', 'active' => true);
  119. $result = $new->data();
  120. $this->assertEqual($expected, $result);
  121. $new->foo = 'bar';
  122. $expected = array('name' => 'Acme, Inc.', 'active' => true, 'foo' => 'bar');
  123. $result = $new->data();
  124. $this->assertEqual($expected, $result);
  125. $this->assertTrue($new->save());
  126. $updated = Companies::find((string) $new->_id);
  127. $expected = 'bar';
  128. $result = $updated->foo;
  129. $this->assertEqual($expected, $result);
  130. }
  131. protected static function _createCompany() {
  132. Companies::create(array(
  133. 'name' => 'Acme, Inc.',
  134. 'active' => true
  135. ))->save();
  136. }
  137. }
  138. ?>