EntityTest.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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. public function testCallShouldOnlyWorkForGetterSetter(){
  92. $this->setExpectedException('\BadFunctionCallException');
  93. $this->entity->something();
  94. }
  95. public function testGetterShouldFailIfAttributeNotDefined(){
  96. $this->setExpectedException('\BadFunctionCallException');
  97. $this->entity->getTest();
  98. }
  99. public function testSetterShouldFailIfAttributeNotDefined(){
  100. $this->setExpectedException('\BadFunctionCallException');
  101. $this->entity->setTest();
  102. }
  103. public function testFromRowShouldNotAssignEmptyArray(){
  104. $row = array();
  105. $entity2 = new TestEntity();
  106. $this->entity = TestEntity::fromRow($row);
  107. $this->assertEquals($entity2, $this->entity);
  108. }
  109. public function testIdGetsConvertedToInt(){
  110. $row = array('id' => '4');
  111. $this->entity = TestEntity::fromRow($row);
  112. $this->assertSame(4, $this->entity->getId());
  113. }
  114. public function testSetType(){
  115. $row = array('testId' => '4');
  116. $this->entity = TestEntity::fromRow($row);
  117. $this->assertSame(4, $this->entity->getTestId());
  118. }
  119. public function testFromParams(){
  120. $params = array(
  121. 'testId' => 4,
  122. 'email' => 'john@doe'
  123. );
  124. $entity = TestEntity::fromParams($params);
  125. $this->assertEquals($params['testId'], $entity->getTestId());
  126. $this->assertEquals($params['email'], $entity->getEmail());
  127. $this->assertTrue($entity instanceof TestEntity);
  128. }
  129. public function testSlugify(){
  130. $entity = new TestEntity();
  131. $entity->setName('Slugify this!');
  132. $this->assertEquals('slugify-this', $entity->slugify('name'));
  133. $entity->setName('°!"§$%&/()=?`´ß\}][{³²#\'+~*-_.:,;<>|äöüÄÖÜSlugify this!');
  134. $this->assertEquals('slugify-this', $entity->slugify('name'));
  135. }
  136. public function testSetterCasts() {
  137. $entity = new TestEntity();
  138. $entity->setId('3');
  139. $this->assertSame(3, $entity->getId());
  140. }
  141. public function testSetterDoesNotCastOnNull() {
  142. $entity = new TestEntity();
  143. $entity->setId(null);
  144. $this->assertSame(null, $entity->getId());
  145. }
  146. public function testGetFieldTypes() {
  147. $entity = new TestEntity();
  148. $this->assertEquals(array(
  149. 'id' => 'integer',
  150. 'testId' => 'integer'
  151. ), $entity->getFieldTypes());
  152. }
  153. public function testGetItInt() {
  154. $entity = new TestEntity();
  155. $entity->setId(3);
  156. $this->assertEquals('integer', gettype($entity->getId()));
  157. }
  158. public function testFieldsNotMarkedUpdatedIfNothingChanges() {
  159. $entity = new TestEntity('hey');
  160. $entity->setName('hey');
  161. $this->assertEquals(0, count($entity->getUpdatedFields()));
  162. }
  163. }