EntityTest.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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 Test\AppFramework\Db;
  23. use OCP\AppFramework\Db\Entity;
  24. /**
  25. * @method integer getId()
  26. * @method void setId(integer $id)
  27. * @method integer getTestId()
  28. * @method void setTestId(integer $id)
  29. * @method string getName()
  30. * @method void setName(string $name)
  31. * @method string getEmail()
  32. * @method void setEmail(string $email)
  33. * @method string getPreName()
  34. * @method void setPreName(string $preName)
  35. */
  36. class TestEntity extends Entity {
  37. protected $name;
  38. protected $email;
  39. protected $testId;
  40. protected $preName;
  41. public function __construct($name=null){
  42. $this->addType('testId', 'integer');
  43. $this->name = $name;
  44. }
  45. };
  46. class EntityTest extends \Test\TestCase {
  47. private $entity;
  48. protected function setUp(){
  49. parent::setUp();
  50. $this->entity = new TestEntity();
  51. }
  52. public function testResetUpdatedFields(){
  53. $entity = new TestEntity();
  54. $entity->setId(3);
  55. $entity->resetUpdatedFields();
  56. $this->assertEquals(array(), $entity->getUpdatedFields());
  57. }
  58. public function testFromRow(){
  59. $row = array(
  60. 'pre_name' => 'john',
  61. 'email' => 'john@something.com'
  62. );
  63. $this->entity = TestEntity::fromRow($row);
  64. $this->assertEquals($row['pre_name'], $this->entity->getPreName());
  65. $this->assertEquals($row['email'], $this->entity->getEmail());
  66. }
  67. public function testGetSetId(){
  68. $id = 3;
  69. $this->entity->setId(3);
  70. $this->assertEquals($id, $this->entity->getId());
  71. }
  72. public function testColumnToPropertyNoReplacement(){
  73. $column = 'my';
  74. $this->assertEquals('my',
  75. $this->entity->columnToProperty($column));
  76. }
  77. public function testColumnToProperty(){
  78. $column = 'my_attribute';
  79. $this->assertEquals('myAttribute',
  80. $this->entity->columnToProperty($column));
  81. }
  82. public function testPropertyToColumnNoReplacement(){
  83. $property = 'my';
  84. $this->assertEquals('my',
  85. $this->entity->propertyToColumn($property));
  86. }
  87. public function testSetterMarksFieldUpdated(){
  88. $this->entity->setId(3);
  89. $this->assertContains('id', $this->entity->getUpdatedFields());
  90. }
  91. /**
  92. * @expectedException \BadFunctionCallException
  93. */
  94. public function testCallShouldOnlyWorkForGetterSetter(){
  95. $this->entity->something();
  96. }
  97. /**
  98. * @expectedException \BadFunctionCallException
  99. */
  100. public function testGetterShouldFailIfAttributeNotDefined(){
  101. $this->entity->getTest();
  102. }
  103. /**
  104. * @expectedException \BadFunctionCallException
  105. */
  106. public function testSetterShouldFailIfAttributeNotDefined(){
  107. $this->entity->setTest();
  108. }
  109. public function testFromRowShouldNotAssignEmptyArray(){
  110. $row = array();
  111. $entity2 = new TestEntity();
  112. $this->entity = TestEntity::fromRow($row);
  113. $this->assertEquals($entity2, $this->entity);
  114. }
  115. public function testIdGetsConvertedToInt(){
  116. $row = array('id' => '4');
  117. $this->entity = TestEntity::fromRow($row);
  118. $this->assertSame(4, $this->entity->getId());
  119. }
  120. public function testSetType(){
  121. $row = array('testId' => '4');
  122. $this->entity = TestEntity::fromRow($row);
  123. $this->assertSame(4, $this->entity->getTestId());
  124. }
  125. public function testFromParams(){
  126. $params = array(
  127. 'testId' => 4,
  128. 'email' => 'john@doe'
  129. );
  130. $entity = TestEntity::fromParams($params);
  131. $this->assertEquals($params['testId'], $entity->getTestId());
  132. $this->assertEquals($params['email'], $entity->getEmail());
  133. $this->assertTrue($entity instanceof TestEntity);
  134. }
  135. public function testSlugify(){
  136. $entity = new TestEntity();
  137. $entity->setName('Slugify this!');
  138. $this->assertEquals('slugify-this', $entity->slugify('name'));
  139. $entity->setName('°!"§$%&/()=?`´ß\}][{³²#\'+~*-_.:,;<>|äöüÄÖÜSlugify this!');
  140. $this->assertEquals('slugify-this', $entity->slugify('name'));
  141. }
  142. public function testSetterCasts() {
  143. $entity = new TestEntity();
  144. $entity->setId('3');
  145. $this->assertSame(3, $entity->getId());
  146. }
  147. public function testSetterDoesNotCastOnNull() {
  148. $entity = new TestEntity();
  149. $entity->setId(null);
  150. $this->assertSame(null, $entity->getId());
  151. }
  152. public function testGetFieldTypes() {
  153. $entity = new TestEntity();
  154. $this->assertEquals(array(
  155. 'id' => 'integer',
  156. 'testId' => 'integer'
  157. ), $entity->getFieldTypes());
  158. }
  159. public function testGetItInt() {
  160. $entity = new TestEntity();
  161. $entity->setId(3);
  162. $this->assertEquals('integer', gettype($entity->getId()));
  163. }
  164. public function testFieldsNotMarkedUpdatedIfNothingChanges() {
  165. $entity = new TestEntity('hey');
  166. $entity->setName('hey');
  167. $this->assertEquals(0, count($entity->getUpdatedFields()));
  168. }
  169. }