EntityTest.php 6.2 KB

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