EntityTest.php 6.8 KB

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