1
0

EntityTest.php 6.3 KB

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