/bluebox/libraries/doctrine/tests/Ticket/1436TestCase.php

https://github.com/robertleeplummerjr/bluebox · PHP · 132 lines · 85 code · 16 blank · 31 comment · 0 complexity · 0f076146cbd2e358e35cb76fa981d412 MD5 · raw file

  1. <?php
  2. /*
  3. * $Id$
  4. *
  5. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  6. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  7. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  8. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  9. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  10. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  11. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  12. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  13. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  14. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  15. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  16. *
  17. * This software consists of voluntary contributions made by many individuals
  18. * and is licensed under the LGPL. For more information, see
  19. * <http://www.phpdoctrine.org>.
  20. */
  21. /**
  22. * Doctrine_Ticket_1436_TestCase
  23. *
  24. * @package Doctrine
  25. * @author Konsta Vesterinen <kvesteri@cc.hut.fi>
  26. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  27. * @category Object Relational Mapping
  28. * @link www.phpdoctrine.org
  29. * @since 1.0
  30. * @version $Revision$
  31. */
  32. class Doctrine_Ticket_1436_TestCase extends Doctrine_UnitTestCase
  33. {
  34. public function prepareTables()
  35. {
  36. parent::prepareTables();
  37. }
  38. public function prepareData()
  39. {
  40. $user = new User();
  41. $user->name = 'John';
  42. $user->save();
  43. # Create existing groups
  44. $group = new Group();
  45. $group->name = 'Group One';
  46. $group->save();
  47. $this->group_one = $group['id'];
  48. $group = new Group();
  49. $group->name = 'Group Two';
  50. $group->save();
  51. $this->group_two = $group['id'];
  52. $group = new Group();
  53. $group->name = 'Group Three';
  54. $group->save();
  55. $this->group_three = $group['id'];
  56. }
  57. public function testSynchronizeAddMNLinks()
  58. {
  59. $user = Doctrine_Query::create()->from('User u')->fetchOne();
  60. $userArray = array(
  61. 'Group' => array(
  62. $this->group_one,
  63. $this->group_two
  64. )
  65. );
  66. $user->synchronizeWithArray($userArray);
  67. try {
  68. $user->save();
  69. } catch (Exception $e ) {
  70. $this->fail("Failed saving with " . $e->getMessage());
  71. }
  72. }
  73. public function testSynchronizeAddMNLinksAfterSave()
  74. {
  75. $user = Doctrine_Query::create()->from('User u, u.Group g')->fetchOne();
  76. $this->assertEqual($user->Group[0]->name, 'Group One');
  77. $this->assertEqual($user->Group[1]->name, 'Group Two');
  78. $this->assertTrue(!isset($user->Group[2]));
  79. }
  80. public function testSynchronizeChangeMNLinks()
  81. {
  82. $user = Doctrine_Query::create()->from('User u, u.Group g')->fetchOne();
  83. $userArray = array(
  84. 'Group' => array(
  85. $this->group_two,
  86. $this->group_three
  87. )
  88. );
  89. $user->synchronizeWithArray($userArray);
  90. $this->assertTrue(!isset($user->Groups));
  91. try {
  92. $user->save();
  93. } catch (Exception $e ) {
  94. $this->fail("Failed saving with " . $e->getMessage());
  95. }
  96. $user->refresh();
  97. $user->loadReference('Group');
  98. $this->assertEqual($user->Group[0]->name, 'Group Two');
  99. $this->assertEqual($user->Group[1]->name, 'Group Three');
  100. $this->assertTrue(!isset($user->Group[2]));
  101. }
  102. public function testFromArray()
  103. {
  104. $user = new User();
  105. $userArray = array('Group' => array($this->group_two, $this->group_three));
  106. $user->fromArray($userArray);
  107. $this->assertEqual($user->Group[0]->name, 'Group Two');
  108. $this->assertEqual($user->Group[1]->name, 'Group Three');
  109. }
  110. public function testSynchronizeMNRecordsDontDeleteAfterUnlink()
  111. {
  112. $group = Doctrine::getTable('Group')->find($this->group_one);
  113. $this->assertTrue(!empty($group));
  114. $this->assertEqual($group->name, 'Group One');
  115. }
  116. }