EntityTest.php 6.7 KB

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