EntityTest.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. <?php
  2. /**
  3. * ownCloud - App Framework
  4. *
  5. * @author Bernhard Posselt
  6. * @copyright 2012 Bernhard Posselt dev@bernhard-posselt.com
  7. *
  8. * This library is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
  10. * License as published by the Free Software Foundation; either
  11. * version 3 of the License, or any later version.
  12. *
  13. * This library is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public
  19. * License along with this library. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. namespace OCP\AppFramework\Db;
  23. /**
  24. * @method integer getId()
  25. * @method void setId(integer $id)
  26. * @method integer getTestId()
  27. * @method void setTestId(integer $id)
  28. * @method string getName()
  29. * @method void setName(string $name)
  30. * @method string getEmail()
  31. * @method void setEmail(string $email)
  32. * @method string getPreName()
  33. * @method void setPreName(string $preName)
  34. */
  35. class TestEntity extends Entity {
  36. protected $name;
  37. protected $email;
  38. protected $testId;
  39. protected $preName;
  40. public function __construct($name=null){
  41. $this->addType('testId', 'integer');
  42. $this->name = $name;
  43. }
  44. };
  45. class EntityTest extends \Test\TestCase {
  46. private $entity;
  47. protected function setUp(){
  48. parent::setUp();
  49. $this->entity = new TestEntity();
  50. }
  51. public function testResetUpdatedFields(){
  52. $entity = new TestEntity();
  53. $entity->setId(3);
  54. $entity->resetUpdatedFields();
  55. $this->assertEquals(array(), $entity->getUpdatedFields());
  56. }
  57. public function testFromRow(){
  58. $row = array(
  59. 'pre_name' => 'john',
  60. 'email' => 'john@something.com'
  61. );
  62. $this->entity = TestEntity::fromRow($row);
  63. $this->assertEquals($row['pre_name'], $this->entity->getPreName());
  64. $this->assertEquals($row['email'], $this->entity->getEmail());
  65. }
  66. public function testGetSetId(){
  67. $id = 3;
  68. $this->entity->setId(3);
  69. $this->assertEquals($id, $this->entity->getId());
  70. }
  71. public function testColumnToPropertyNoReplacement(){
  72. $column = 'my';
  73. $this->assertEquals('my',
  74. $this->entity->columnToProperty($column));
  75. }
  76. public function testColumnToProperty(){
  77. $column = 'my_attribute';
  78. $this->assertEquals('myAttribute',
  79. $this->entity->columnToProperty($column));
  80. }
  81. public function testPropertyToColumnNoReplacement(){
  82. $property = 'my';
  83. $this->assertEquals('my',
  84. $this->entity->propertyToColumn($property));
  85. }
  86. public function testSetterMarksFieldUpdated(){
  87. $this->entity->setId(3);
  88. $this->assertContains('id', $this->entity->getUpdatedFields());
  89. }
  90. public function testCallShouldOnlyWorkForGetterSetter(){
  91. $this->setExpectedException('\BadFunctionCallException');
  92. $this->entity->something();
  93. }
  94. public function testGetterShouldFailIfAttributeNotDefined(){
  95. $this->setExpectedException('\BadFunctionCallException');
  96. $this->entity->getTest();
  97. }
  98. public function testSetterShouldFailIfAttributeNotDefined(){
  99. $this->setExpectedException('\BadFunctionCallException');
  100. $this->entity->setTest();
  101. }
  102. public function testFromRowShouldNotAssignEmptyArray(){
  103. $row = array();
  104. $entity2 = new TestEntity();
  105. $this->entity = TestEntity::fromRow($row);
  106. $this->assertEquals($entity2, $this->entity);
  107. }
  108. public function testIdGetsConvertedToInt(){
  109. $row = array('id' => '4');
  110. $this->entity = TestEntity::fromRow($row);
  111. $this->assertSame(4, $this->entity->getId());
  112. }
  113. public function testSetType(){
  114. $row = array('testId' => '4');
  115. $this->entity = TestEntity::fromRow($row);
  116. $this->assertSame(4, $this->entity->getTestId());
  117. }
  118. public function testFromParams(){
  119. $params = array(
  120. 'testId' => 4,
  121. 'email' => 'john@doe'
  122. );
  123. $entity = TestEntity::fromParams($params);
  124. $this->assertEquals($params['testId'], $entity->getTestId());
  125. $this->assertEquals($params['email'], $entity->getEmail());
  126. $this->assertTrue($entity instanceof TestEntity);
  127. }
  128. public function testSlugify(){
  129. $entity = new TestEntity();
  130. $entity->setName('Slugify this!');
  131. $this->assertEquals('slugify-this', $entity->slugify('name'));
  132. $entity->setName('°!"§$%&/()=?`´ß\}][{³²#\'+~*-_.:,;<>|äöüÄÖÜSlugify this!');
  133. $this->assertEquals('slugify-this', $entity->slugify('name'));
  134. }
  135. public function testSetterCasts() {
  136. $entity = new TestEntity();
  137. $entity->setId('3');
  138. $this->assertSame(3, $entity->getId());
  139. }
  140. public function testSetterDoesNotCastOnNull() {
  141. $entity = new TestEntity();
  142. $entity->setId(null);
  143. $this->assertSame(null, $entity->getId());
  144. }
  145. public function testGetFieldTypes() {
  146. $entity = new TestEntity();
  147. $this->assertEquals(array(
  148. 'id' => 'integer',
  149. 'testId' => 'integer'
  150. ), $entity->getFieldTypes());
  151. }
  152. public function testGetItInt() {
  153. $entity = new TestEntity();
  154. $entity->setId(3);
  155. $this->assertEquals('integer', gettype($entity->getId()));
  156. }
  157. public function testFieldsNotMarkedUpdatedIfNothingChanges() {
  158. $entity = new TestEntity('hey');
  159. $entity->setName('hey');
  160. $this->assertEquals(0, count($entity->getUpdatedFields()));
  161. }
  162. }