DocumentTest.php 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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 DocumentTest extends \lithium\test\Integration {
  12. protected $_database;
  13. protected $_connection = null;
  14. protected $_key = null;
  15. /**
  16. * Creating the test database
  17. */
  18. public function setUp() {
  19. $this->_connection->connection->put($this->_database);
  20. }
  21. /**
  22. * Dropping the test database
  23. */
  24. public function tearDown() {
  25. $this->_connection->connection->delete($this->_database);
  26. }
  27. /**
  28. * Skip the test if no test database connection available.
  29. */
  30. public function skip() {
  31. $connection = 'lithium_couch_test';
  32. $config = Connections::get($connection, array('config' => true));
  33. $isConnected = $config && Connections::get($connection)->isConnected(array(
  34. 'autoConnect' => true
  35. ));
  36. $isAvailable = $config && $isConnected;
  37. $this->skipIf(!$isAvailable, "No {$connection} connection available.");
  38. $this->_key = Companies::key();
  39. $this->_database = $config['database'];
  40. $this->_connection = Connections::get($connection);
  41. }
  42. public function testUpdateWithNewArray() {
  43. $new = Companies::create(array('name' => 'Acme, Inc.', 'active' => true));
  44. $expected = array('name' => 'Acme, Inc.', 'active' => true);
  45. $result = $new->data();
  46. $this->assertEqual($expected, $result);
  47. $new->foo = array('bar');
  48. $expected = array('name' => 'Acme, Inc.', 'active' => true, 'foo' => array('bar'));
  49. $result = $new->data();
  50. $this->assertEqual($expected, $result);
  51. $this->assertTrue($new->save());
  52. $updated = Companies::find((string) $new->_id);
  53. $expected = 'bar';
  54. $result = $updated->foo[0];
  55. $this->assertEqual($expected, $result);
  56. $updated->foo[1] = 'baz';
  57. $this->assertTrue($updated->save());
  58. $updated = Companies::find((string) $updated->_id);
  59. $expected = 'baz';
  60. $result = $updated->foo[1];
  61. $this->assertEqual($expected, $result);
  62. }
  63. }
  64. ?>