EntityTest.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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 void setAnotherBool(bool $anotherBool)
  27. * @method string getLongText()
  28. * @method void setLongText(string $longText)
  29. */
  30. class TestEntity extends Entity {
  31. protected $name;
  32. protected $email;
  33. protected $testId;
  34. protected $preName;
  35. protected $trueOrFalse;
  36. protected $anotherBool;
  37. protected $longText;
  38. public function __construct($name = null) {
  39. $this->addType('testId', 'integer');
  40. $this->addType('trueOrFalse', 'bool');
  41. $this->addType('anotherBool', 'boolean');
  42. $this->addType('longText', 'blob');
  43. $this->name = $name;
  44. }
  45. }
  46. class EntityTest extends \Test\TestCase {
  47. private $entity;
  48. protected function setUp(): void {
  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([], $entity->getUpdatedFields());
  57. }
  58. public function testFromRow() {
  59. $row = [
  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', array_keys($this->entity->getUpdatedFields()));
  90. }
  91. public function testCallShouldOnlyWorkForGetterSetter() {
  92. $this->expectException(\BadFunctionCallException::class);
  93. $this->entity->something();
  94. }
  95. public function testGetterShouldFailIfAttributeNotDefined() {
  96. $this->expectException(\BadFunctionCallException::class);
  97. $this->entity->getTest();
  98. }
  99. public function testSetterShouldFailIfAttributeNotDefined() {
  100. $this->expectException(\BadFunctionCallException::class);
  101. $this->entity->setTest();
  102. }
  103. public function testFromRowShouldNotAssignEmptyArray() {
  104. $row = [];
  105. $entity2 = new TestEntity();
  106. $this->entity = TestEntity::fromRow($row);
  107. $this->assertEquals($entity2, $this->entity);
  108. }
  109. public function testIdGetsConvertedToInt() {
  110. $row = ['id' => '4'];
  111. $this->entity = TestEntity::fromRow($row);
  112. $this->assertSame(4, $this->entity->getId());
  113. }
  114. public function testSetType() {
  115. $row = ['testId' => '4'];
  116. $this->entity = TestEntity::fromRow($row);
  117. $this->assertSame(4, $this->entity->getTestId());
  118. }
  119. public function testFromParams() {
  120. $params = [
  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 testSetterConvertsResourcesToStringProperly() {
  147. $string = 'Definitely a string';
  148. $stream = fopen('php://memory', 'r+');
  149. fwrite($stream, $string);
  150. rewind($stream);
  151. $entity = new TestEntity();
  152. $entity->setLongText($stream);
  153. fclose($stream);
  154. $this->assertSame($string, $entity->getLongText());
  155. }
  156. public function testGetFieldTypes() {
  157. $entity = new TestEntity();
  158. $this->assertEquals([
  159. 'id' => 'integer',
  160. 'testId' => 'integer',
  161. 'trueOrFalse' => 'bool',
  162. 'anotherBool' => 'boolean',
  163. 'longText' => 'blob',
  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. }