EntityTest.php 7.4 KB

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