diff options
Diffstat (limited to 'tests/lib/AppFramework/Db')
-rw-r--r-- | tests/lib/AppFramework/Db/EntityTest.php | 13 | ||||
-rw-r--r-- | tests/lib/AppFramework/Db/QBMapperDBTest.php | 5 | ||||
-rw-r--r-- | tests/lib/AppFramework/Db/QBMapperTest.php | 96 | ||||
-rw-r--r-- | tests/lib/AppFramework/Db/TransactionalTest.php | 16 |
4 files changed, 72 insertions, 58 deletions
diff --git a/tests/lib/AppFramework/Db/EntityTest.php b/tests/lib/AppFramework/Db/EntityTest.php index 3c844780b07..eab081e6ac6 100644 --- a/tests/lib/AppFramework/Db/EntityTest.php +++ b/tests/lib/AppFramework/Db/EntityTest.php @@ -36,7 +36,6 @@ use PHPUnit\Framework\Constraint\IsType; * @method void setDatetime(\DateTimeImmutable $datetime) */ class TestEntity extends Entity { - protected $name; protected $email; protected $testId; protected $smallInt; @@ -49,7 +48,9 @@ class TestEntity extends Entity { protected $time; protected $datetime; - public function __construct($name = null) { + public function __construct( + protected $name = null, + ) { $this->addType('testId', Types::INTEGER); $this->addType('smallInt', Types::SMALLINT); $this->addType('bigInt', Types::BIGINT); @@ -63,8 +64,6 @@ class TestEntity extends Entity { $this->addType('trueOrFalse', 'bool'); $this->addType('legacyInt', 'int'); $this->addType('doubleNowFloat', 'double'); - - $this->name = $name; } public function setAnotherBool(bool $anotherBool): void { @@ -211,7 +210,7 @@ class EntityTest extends \Test\TestCase { } - public function dataSetterCasts(): array { + public static function dataSetterCasts(): array { return [ ['Id', '3', 3], ['smallInt', '3', 3], @@ -226,9 +225,7 @@ class EntityTest extends \Test\TestCase { } - /** - * @dataProvider dataSetterCasts - */ + #[\PHPUnit\Framework\Attributes\DataProvider('dataSetterCasts')] public function testSetterCasts(string $field, mixed $in, mixed $out): void { $entity = new TestEntity(); $entity->{'set' . $field}($in); diff --git a/tests/lib/AppFramework/Db/QBMapperDBTest.php b/tests/lib/AppFramework/Db/QBMapperDBTest.php index 72bc2d956d6..614f1099644 100644 --- a/tests/lib/AppFramework/Db/QBMapperDBTest.php +++ b/tests/lib/AppFramework/Db/QBMapperDBTest.php @@ -1,4 +1,5 @@ <?php + /** * SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors * SPDX-License-Identifier: AGPL-3.0-or-later @@ -63,14 +64,14 @@ class QBDBTestMapper extends QBMapper { * @group DB */ class QBMapperDBTest extends TestCase { - /** @var \Doctrine\DBAL\Connection|\OCP\IDBConnection */ + /** @var \Doctrine\DBAL\Connection|IDBConnection */ protected $connection; protected $schemaSetup = false; protected function setUp(): void { parent::setUp(); - $this->connection = \OCP\Server::get(IDBConnection::class); + $this->connection = Server::get(IDBConnection::class); $this->prepareTestingTable(); } diff --git a/tests/lib/AppFramework/Db/QBMapperTest.php b/tests/lib/AppFramework/Db/QBMapperTest.php index 3cf32e56f12..0f18ef3f204 100644 --- a/tests/lib/AppFramework/Db/QBMapperTest.php +++ b/tests/lib/AppFramework/Db/QBMapperTest.php @@ -1,4 +1,5 @@ <?php + /** * SPDX-FileCopyrightText: 2019 Nextcloud GmbH and Nextcloud contributors * SPDX-License-Identifier: AGPL-3.0-or-later @@ -99,7 +100,7 @@ class QBMapperTest extends \Test\TestCase { $this->mapper = new QBTestMapper($this->db); } - + public function testInsertEntityParameterTypeMapping(): void { $datetime = new \DateTimeImmutable(); $entity = new QBTestEntity(); @@ -117,31 +118,40 @@ class QBMapperTest extends \Test\TestCase { $booleanParam = $this->qb->createNamedParameter('boolean_prop', IQueryBuilder::PARAM_BOOL); $datetimeParam = $this->qb->createNamedParameter('datetime_prop', IQueryBuilder::PARAM_DATETIME_IMMUTABLE); + $createNamedParameterCalls = [ + [123, IQueryBuilder::PARAM_INT, null], + [true, IQueryBuilder::PARAM_BOOL, null], + ['string', IQueryBuilder::PARAM_STR, null], + [456, IQueryBuilder::PARAM_INT, null], + [false, IQueryBuilder::PARAM_BOOL, null], + [$datetime, IQueryBuilder::PARAM_DATETIME_IMMUTABLE, null], + ]; $this->qb->expects($this->exactly(6)) ->method('createNamedParameter') - ->withConsecutive( - [$this->equalTo(123), $this->equalTo(IQueryBuilder::PARAM_INT)], - [$this->equalTo(true), $this->equalTo(IQueryBuilder::PARAM_BOOL)], - [$this->equalTo('string'), $this->equalTo(IQueryBuilder::PARAM_STR)], - [$this->equalTo(456), $this->equalTo(IQueryBuilder::PARAM_INT)], - [$this->equalTo(false), $this->equalTo(IQueryBuilder::PARAM_BOOL)], - [$this->equalTo($datetime), $this->equalTo(IQueryBuilder::PARAM_DATETIME_IMMUTABLE)], - ); + ->willReturnCallback(function () use (&$createNamedParameterCalls): void { + $expected = array_shift($createNamedParameterCalls); + $this->assertEquals($expected, func_get_args()); + }); + + $setValueCalls = [ + ['int_prop', $intParam], + ['bool_prop', $boolParam], + ['string_prop', $stringParam], + ['integer_prop', $integerParam], + ['boolean_prop', $booleanParam], + ['datetime_prop', $datetimeParam], + ]; $this->qb->expects($this->exactly(6)) ->method('setValue') - ->withConsecutive( - [$this->equalTo('int_prop'), $this->equalTo($intParam)], - [$this->equalTo('bool_prop'), $this->equalTo($boolParam)], - [$this->equalTo('string_prop'), $this->equalTo($stringParam)], - [$this->equalTo('integer_prop'), $this->equalTo($integerParam)], - [$this->equalTo('boolean_prop'), $this->equalTo($booleanParam)], - [$this->equalTo('datetime_prop'), $this->equalTo($datetimeParam)], - ); + ->willReturnCallback(function () use (&$setValueCalls): void { + $expected = array_shift($setValueCalls); + $this->assertEquals($expected, func_get_args()); + }); $this->mapper->insert($entity); } - + public function testUpdateEntityParameterTypeMapping(): void { $datetime = new \DateTimeImmutable(); $entity = new QBTestEntity(); @@ -163,30 +173,38 @@ class QBMapperTest extends \Test\TestCase { $jsonParam = $this->qb->createNamedParameter('json_prop', IQueryBuilder::PARAM_JSON); $datetimeParam = $this->qb->createNamedParameter('datetime_prop', IQueryBuilder::PARAM_DATETIME_IMMUTABLE); + $createNamedParameterCalls = [ + [123, IQueryBuilder::PARAM_INT, null], + [true, IQueryBuilder::PARAM_BOOL, null], + ['string', IQueryBuilder::PARAM_STR, null], + [456, IQueryBuilder::PARAM_INT, null], + [false, IQueryBuilder::PARAM_BOOL, null], + [['hello' => 'world'], IQueryBuilder::PARAM_JSON, null], + [$datetime, IQueryBuilder::PARAM_DATETIME_IMMUTABLE, null], + [789, IQueryBuilder::PARAM_INT, null], + ]; $this->qb->expects($this->exactly(8)) ->method('createNamedParameter') - ->withConsecutive( - [$this->equalTo(123), $this->equalTo(IQueryBuilder::PARAM_INT)], - [$this->equalTo(true), $this->equalTo(IQueryBuilder::PARAM_BOOL)], - [$this->equalTo('string'), $this->equalTo(IQueryBuilder::PARAM_STR)], - [$this->equalTo(456), $this->equalTo(IQueryBuilder::PARAM_INT)], - [$this->equalTo(false), $this->equalTo(IQueryBuilder::PARAM_BOOL)], - [$this->equalTo(['hello' => 'world']), $this->equalTo(IQueryBuilder::PARAM_JSON)], - [$this->equalTo($datetime), $this->equalTo(IQueryBuilder::PARAM_DATETIME_IMMUTABLE)], - [$this->equalTo(789), $this->equalTo(IQueryBuilder::PARAM_INT)], - ); - + ->willReturnCallback(function () use (&$createNamedParameterCalls): void { + $expected = array_shift($createNamedParameterCalls); + $this->assertEquals($expected, func_get_args()); + }); + + $setCalls = [ + ['int_prop', $intParam], + ['bool_prop', $boolParam], + ['string_prop', $stringParam], + ['integer_prop', $integerParam], + ['boolean_prop', $booleanParam], + ['json_prop', $datetimeParam], + ['datetime_prop', $datetimeParam], + ]; $this->qb->expects($this->exactly(7)) ->method('set') - ->withConsecutive( - [$this->equalTo('int_prop'), $this->equalTo($intParam)], - [$this->equalTo('bool_prop'), $this->equalTo($boolParam)], - [$this->equalTo('string_prop'), $this->equalTo($stringParam)], - [$this->equalTo('integer_prop'), $this->equalTo($integerParam)], - [$this->equalTo('boolean_prop'), $this->equalTo($booleanParam)], - [$this->equalTo('json_prop'), $this->equalTo($jsonParam)], - [$this->equalTo('datetime_prop'), $this->equalTo($datetimeParam)], - ); + ->willReturnCallback(function () use (&$setCalls): void { + $expected = array_shift($setCalls); + $this->assertEquals($expected, func_get_args()); + }); $this->expr->expects($this->once()) ->method('eq') @@ -196,7 +214,7 @@ class QBMapperTest extends \Test\TestCase { $this->mapper->update($entity); } - + public function testGetParameterTypeForProperty(): void { $entity = new QBTestEntity(); diff --git a/tests/lib/AppFramework/Db/TransactionalTest.php b/tests/lib/AppFramework/Db/TransactionalTest.php index a60c4386fea..72a3d9ae59f 100644 --- a/tests/lib/AppFramework/Db/TransactionalTest.php +++ b/tests/lib/AppFramework/Db/TransactionalTest.php @@ -28,14 +28,13 @@ class TransactionalTest extends TestCase { $test = new class($this->db) { use TTransactional; - private IDBConnection $db; - - public function __construct(IDBConnection $db) { - $this->db = $db; + public function __construct( + private IDBConnection $db, + ) { } public function fail(): void { - $this->atomic(function () { + $this->atomic(function (): void { throw new RuntimeException('nope'); }, $this->db); } @@ -55,10 +54,9 @@ class TransactionalTest extends TestCase { $test = new class($this->db) { use TTransactional; - private IDBConnection $db; - - public function __construct(IDBConnection $db) { - $this->db = $db; + public function __construct( + private IDBConnection $db, + ) { } public function succeed(): int { |