diff options
Diffstat (limited to 'tests/lib/AppFramework/Db/EntityTest.php')
-rw-r--r-- | tests/lib/AppFramework/Db/EntityTest.php | 176 |
1 files changed, 121 insertions, 55 deletions
diff --git a/tests/lib/AppFramework/Db/EntityTest.php b/tests/lib/AppFramework/Db/EntityTest.php index 17234849a2d..eab081e6ac6 100644 --- a/tests/lib/AppFramework/Db/EntityTest.php +++ b/tests/lib/AppFramework/Db/EntityTest.php @@ -1,29 +1,15 @@ <?php /** - * ownCloud - App Framework - * - * @author Bernhard Posselt - * @copyright 2012 Bernhard Posselt dev@bernhard-posselt.com - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE - * License as published by the Free Software Foundation; either - * version 3 of the License, or any later version. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU AFFERO GENERAL PUBLIC LICENSE for more details. - * - * You should have received a copy of the GNU Affero General Public - * License along with this library. If not, see <http://www.gnu.org/licenses/>. - * + * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors + * SPDX-FileCopyrightText: 2016 ownCloud, Inc. + * SPDX-License-Identifier: AGPL-3.0-or-later */ namespace Test\AppFramework\Db; use OCP\AppFramework\Db\Entity; +use OCP\DB\Types; use PHPUnit\Framework\Constraint\IsType; /** @@ -42,21 +28,46 @@ use PHPUnit\Framework\Constraint\IsType; * @method void setTrueOrFalse(bool $trueOrFalse) * @method bool getAnotherBool() * @method bool isAnotherBool() - * @method void setAnotherBool(bool $anotherBool) + * @method string getLongText() + * @method void setLongText(string $longText) + * @method \DateTime getTime() + * @method void setTime(\DateTime $time) + * @method \DateTimeImmutable getDatetime() + * @method void setDatetime(\DateTimeImmutable $datetime) */ class TestEntity extends Entity { - protected $name; protected $email; protected $testId; + protected $smallInt; + protected $bigInt; protected $preName; protected $trueOrFalse; protected $anotherBool; - - public function __construct($name = null) { - $this->addType('testId', 'integer'); + protected $text; + protected $longText; + protected $time; + protected $datetime; + + public function __construct( + protected $name = null, + ) { + $this->addType('testId', Types::INTEGER); + $this->addType('smallInt', Types::SMALLINT); + $this->addType('bigInt', Types::BIGINT); + $this->addType('anotherBool', Types::BOOLEAN); + $this->addType('text', Types::TEXT); + $this->addType('longText', Types::BLOB); + $this->addType('time', Types::TIME); + $this->addType('datetime', Types::DATETIME_IMMUTABLE); + + // Legacy types $this->addType('trueOrFalse', 'bool'); - $this->addType('anotherBool', 'boolean'); - $this->name = $name; + $this->addType('legacyInt', 'int'); + $this->addType('doubleNowFloat', 'double'); + } + + public function setAnotherBool(bool $anotherBool): void { + parent::setAnotherBool($anotherBool); } } @@ -70,7 +81,7 @@ class EntityTest extends \Test\TestCase { } - public function testResetUpdatedFields() { + public function testResetUpdatedFields(): void { $entity = new TestEntity(); $entity->setId(3); $entity->resetUpdatedFields(); @@ -79,19 +90,21 @@ class EntityTest extends \Test\TestCase { } - public function testFromRow() { + public function testFromRow(): void { $row = [ 'pre_name' => 'john', - 'email' => 'john@something.com' + 'email' => 'john@something.com', + 'another_bool' => 1, ]; $this->entity = TestEntity::fromRow($row); $this->assertEquals($row['pre_name'], $this->entity->getPreName()); $this->assertEquals($row['email'], $this->entity->getEmail()); + $this->assertEquals($row['another_bool'], $this->entity->getAnotherBool()); } - public function testGetSetId() { + public function testGetSetId(): void { $id = 3; $this->entity->setId(3); @@ -99,28 +112,28 @@ class EntityTest extends \Test\TestCase { } - public function testColumnToPropertyNoReplacement() { + public function testColumnToPropertyNoReplacement(): void { $column = 'my'; $this->assertEquals('my', $this->entity->columnToProperty($column)); } - public function testColumnToProperty() { + public function testColumnToProperty(): void { $column = 'my_attribute'; $this->assertEquals('myAttribute', $this->entity->columnToProperty($column)); } - public function testPropertyToColumnNoReplacement() { + public function testPropertyToColumnNoReplacement(): void { $property = 'my'; $this->assertEquals('my', $this->entity->propertyToColumn($property)); } - public function testSetterMarksFieldUpdated() { + public function testSetterMarksFieldUpdated(): void { $this->entity->setId(3); $this->assertContains('id', array_keys($this->entity->getUpdatedFields())); @@ -128,7 +141,7 @@ class EntityTest extends \Test\TestCase { - public function testCallShouldOnlyWorkForGetterSetter() { + public function testCallShouldOnlyWorkForGetterSetter(): void { $this->expectException(\BadFunctionCallException::class); $this->entity->something(); @@ -136,21 +149,21 @@ class EntityTest extends \Test\TestCase { - public function testGetterShouldFailIfAttributeNotDefined() { + public function testGetterShouldFailIfAttributeNotDefined(): void { $this->expectException(\BadFunctionCallException::class); $this->entity->getTest(); } - public function testSetterShouldFailIfAttributeNotDefined() { + public function testSetterShouldFailIfAttributeNotDefined(): void { $this->expectException(\BadFunctionCallException::class); $this->entity->setTest(); } - public function testFromRowShouldNotAssignEmptyArray() { + public function testFromRowShouldNotAssignEmptyArray(): void { $row = []; $entity2 = new TestEntity(); @@ -159,7 +172,7 @@ class EntityTest extends \Test\TestCase { } - public function testIdGetsConvertedToInt() { + public function testIdGetsConvertedToInt(): void { $row = ['id' => '4']; $this->entity = TestEntity::fromRow($row); @@ -167,7 +180,7 @@ class EntityTest extends \Test\TestCase { } - public function testSetType() { + public function testSetType(): void { $row = ['testId' => '4']; $this->entity = TestEntity::fromRow($row); @@ -175,7 +188,7 @@ class EntityTest extends \Test\TestCase { } - public function testFromParams() { + public function testFromParams(): void { $params = [ 'testId' => 4, 'email' => 'john@doe' @@ -188,7 +201,7 @@ class EntityTest extends \Test\TestCase { $this->assertTrue($entity instanceof TestEntity); } - public function testSlugify() { + public function testSlugify(): void { $entity = new TestEntity(); $entity->setName('Slugify this!'); $this->assertEquals('slugify-this', $entity->slugify('name')); @@ -197,45 +210,98 @@ class EntityTest extends \Test\TestCase { } - public function testSetterCasts() { + public static function dataSetterCasts(): array { + return [ + ['Id', '3', 3], + ['smallInt', '3', 3], + ['bigInt', '' . PHP_INT_MAX, PHP_INT_MAX], + ['trueOrFalse', 0, false], + ['trueOrFalse', 1, true], + ['anotherBool', 0, false], + ['anotherBool', 1, true], + ['text', 33, '33'], + ['longText', PHP_INT_MAX, '' . PHP_INT_MAX], + ]; + } + + + #[\PHPUnit\Framework\Attributes\DataProvider('dataSetterCasts')] + public function testSetterCasts(string $field, mixed $in, mixed $out): void { $entity = new TestEntity(); - $entity->setId('3'); - $this->assertSame(3, $entity->getId()); + $entity->{'set' . $field}($in); + $this->assertSame($out, $entity->{'get' . $field}()); } - public function testSetterDoesNotCastOnNull() { + public function testSetterDoesNotCastOnNull(): void { $entity = new TestEntity(); $entity->setId(null); $this->assertSame(null, $entity->getId()); } + public function testSetterConvertsResourcesToStringProperly(): void { + $string = 'Definitely a string'; + $stream = fopen('php://memory', 'r+'); + fwrite($stream, $string); + rewind($stream); + + $entity = new TestEntity(); + $entity->setLongText($stream); + fclose($stream); + $this->assertSame($string, $entity->getLongText()); + } + + public function testSetterConvertsDatetime() { + $entity = new TestEntity(); + $entity->setDatetime('2024-08-19 15:26:00'); + $this->assertEquals(new \DateTimeImmutable('2024-08-19 15:26:00'), $entity->getDatetime()); + } + + public function testSetterDoesNotConvertNullOnDatetime() { + $entity = new TestEntity(); + $entity->setDatetime(null); + $this->assertNull($entity->getDatetime()); + } + + public function testSetterConvertsTime() { + $entity = new TestEntity(); + $entity->setTime('15:26:00'); + $this->assertEquals(new \DateTime('15:26:00'), $entity->getTime()); + } - public function testGetFieldTypes() { + public function testGetFieldTypes(): void { $entity = new TestEntity(); $this->assertEquals([ - 'id' => 'integer', - 'testId' => 'integer', - 'trueOrFalse' => 'bool', - 'anotherBool' => 'boolean', + 'id' => Types::INTEGER, + 'testId' => Types::INTEGER, + 'smallInt' => Types::SMALLINT, + 'bigInt' => Types::BIGINT, + 'anotherBool' => Types::BOOLEAN, + 'text' => Types::TEXT, + 'longText' => Types::BLOB, + 'time' => Types::TIME, + 'datetime' => Types::DATETIME_IMMUTABLE, + 'trueOrFalse' => Types::BOOLEAN, + 'legacyInt' => Types::INTEGER, + 'doubleNowFloat' => Types::FLOAT, ], $entity->getFieldTypes()); } - public function testGetItInt() { + public function testGetItInt(): void { $entity = new TestEntity(); $entity->setId(3); - $this->assertEquals('integer', gettype($entity->getId())); + $this->assertEquals(Types::INTEGER, gettype($entity->getId())); } - public function testFieldsNotMarkedUpdatedIfNothingChanges() { + public function testFieldsNotMarkedUpdatedIfNothingChanges(): void { $entity = new TestEntity('hey'); $entity->setName('hey'); $this->assertEquals(0, count($entity->getUpdatedFields())); } - public function testIsGetter() { + public function testIsGetter(): void { $entity = new TestEntity(); $entity->setTrueOrFalse(false); $entity->setAnotherBool(false); @@ -244,7 +310,7 @@ class EntityTest extends \Test\TestCase { } - public function testIsGetterShoudFailForOtherType() { + public function testIsGetterShoudFailForOtherType(): void { $this->expectException(\BadFunctionCallException::class); $entity = new TestEntity(); |