From cd71945449dac3875b93a75af1c6d3a4c514fb11 Mon Sep 17 00:00:00 2001 From: Sergei Tigrov Date: Wed, 3 Jul 2024 17:55:19 +0700 Subject: [PATCH] Remove `$db` property from constructor (#368) --- README.md | 50 +- composer-require-checker.json | 8 + composer.json | 14 +- src/AbstractActiveRecord.php | 23 +- src/ActiveQuery.php | 28 +- src/ActiveQueryInterface.php | 7 +- src/ActiveRecordFactory.php | 87 --- src/ActiveRecordInterface.php | 6 + src/ConnectionProvider.php | 60 ++ src/ConnectionProviderMiddleware.php | 25 + src/Trait/CustomConnectionTrait.php | 38 ++ tests/ActiveQueryFindTest.php | 144 ++--- tests/ActiveQueryTest.php | 569 +++++++++--------- tests/ActiveRecordFactoryTest.php | 98 --- tests/ActiveRecordTest.php | 226 +++---- tests/BatchQueryResultTest.php | 20 +- tests/ConnectionProviderTest.php | 72 +++ tests/Driver/Mssql/ActiveQueryFindTest.php | 17 +- tests/Driver/Mssql/ActiveQueryTest.php | 17 +- .../Driver/Mssql/ActiveRecordFactoryTest.php | 28 - tests/Driver/Mssql/ActiveRecordTest.php | 27 +- tests/Driver/Mssql/BatchQueryResultTest.php | 17 +- tests/Driver/Mssql/ConnectionProviderTest.php | 16 + tests/Driver/Mssql/MagicActiveRecordTest.php | 27 +- tests/Driver/Mysql/ActiveQueryFindTest.php | 17 +- tests/Driver/Mysql/ActiveQueryTest.php | 17 +- .../Driver/Mysql/ActiveRecordFactoryTest.php | 28 - tests/Driver/Mysql/ActiveRecordTest.php | 31 +- tests/Driver/Mysql/BatchQueryResultTest.php | 17 +- tests/Driver/Mysql/ConnectionProviderTest.php | 16 + tests/Driver/Mysql/MagicActiveRecordTest.php | 25 +- tests/Driver/Oracle/ActiveQueryFindTest.php | 39 +- tests/Driver/Oracle/ActiveQueryTest.php | 57 +- .../Driver/Oracle/ActiveRecordFactoryTest.php | 28 - tests/Driver/Oracle/ActiveRecordTest.php | 47 +- tests/Driver/Oracle/BatchQueryResultTest.php | 21 +- .../Driver/Oracle/ConnectionProviderTest.php | 16 + tests/Driver/Oracle/MagicActiveRecordTest.php | 47 +- tests/Driver/Pgsql/ActiveQueryFindTest.php | 23 +- tests/Driver/Pgsql/ActiveQueryTest.php | 17 +- .../Driver/Pgsql/ActiveRecordFactoryTest.php | 28 - tests/Driver/Pgsql/ActiveRecordTest.php | 87 ++- tests/Driver/Pgsql/BatchQueryResultTest.php | 17 +- tests/Driver/Pgsql/ConnectionProviderTest.php | 16 + tests/Driver/Pgsql/MagicActiveRecordTest.php | 73 +-- tests/Driver/Sqlite/ActiveQueryFindTest.php | 17 +- tests/Driver/Sqlite/ActiveQueryTest.php | 17 +- .../Driver/Sqlite/ActiveRecordFactoryTest.php | 28 - tests/Driver/Sqlite/ActiveRecordTest.php | 25 +- tests/Driver/Sqlite/BatchQueryResultTest.php | 17 +- .../Driver/Sqlite/ConnectionProviderTest.php | 16 + tests/Driver/Sqlite/MagicActiveRecordTest.php | 25 +- tests/MagicActiveRecordTest.php | 206 +++---- tests/Stubs/ActiveRecord/Animal.php | 7 +- .../Stubs/ActiveRecord/ArrayAndJsonTypes.php | 3 - tests/Stubs/ActiveRecord/Customer.php | 2 +- .../ActiveRecord/CustomerWithConstructor.php | 53 -- .../CustomerWithCustomConnection.php | 12 + tests/Stubs/ActiveRecord/OrderItem.php | 2 +- tests/Stubs/MagicActiveRecord/Animal.php | 5 +- .../CustomerWithConstructor.php | 38 -- tests/Stubs/MagicActiveRecord/OrderItem.php | 2 +- tests/TestCase.php | 26 +- 63 files changed, 1226 insertions(+), 1566 deletions(-) create mode 100644 composer-require-checker.json delete mode 100644 src/ActiveRecordFactory.php create mode 100644 src/ConnectionProvider.php create mode 100644 src/ConnectionProviderMiddleware.php create mode 100644 src/Trait/CustomConnectionTrait.php delete mode 100644 tests/ActiveRecordFactoryTest.php create mode 100644 tests/ConnectionProviderTest.php delete mode 100644 tests/Driver/Mssql/ActiveRecordFactoryTest.php create mode 100644 tests/Driver/Mssql/ConnectionProviderTest.php delete mode 100644 tests/Driver/Mysql/ActiveRecordFactoryTest.php create mode 100644 tests/Driver/Mysql/ConnectionProviderTest.php delete mode 100644 tests/Driver/Oracle/ActiveRecordFactoryTest.php create mode 100644 tests/Driver/Oracle/ConnectionProviderTest.php delete mode 100644 tests/Driver/Pgsql/ActiveRecordFactoryTest.php create mode 100644 tests/Driver/Pgsql/ConnectionProviderTest.php delete mode 100644 tests/Driver/Sqlite/ActiveRecordFactoryTest.php create mode 100644 tests/Driver/Sqlite/ConnectionProviderTest.php delete mode 100644 tests/Stubs/ActiveRecord/CustomerWithConstructor.php create mode 100644 tests/Stubs/ActiveRecord/CustomerWithCustomConnection.php delete mode 100644 tests/Stubs/MagicActiveRecord/CustomerWithConstructor.php diff --git a/README.md b/README.md index a64a415d1..208ac78de 100644 --- a/README.md +++ b/README.md @@ -106,18 +106,22 @@ final class User extends ActiveRecord For more information, follow [Create Active Record Model](docs/create-model.md). -## Usage in controler with DI container autowiring +## Usage in controller with DI container autowiring ```php use App\Entity\User; use Psr\Http\Message\ResponseInterface; +use Yiisoft\ActiveRecord\ConnectionProvider; +use Yiisoft\Db\Connection\ConnectionInterface; final class Register { public function register( - User $user + ConnectionInterface $db, ): ResponseInterface { - /** Connected AR by di autowired. */ + ConnectionProvider::set($db); + + $user = new User(); $user->setAttribute('username', 'yiiliveext'); $user->setAttribute('email', 'yiiliveext@mail.ru'); $user->save(); @@ -125,7 +129,36 @@ final class Register } ``` -## Usage in controler with Active Record factory +## Usage with middleware + +Add the middleware to the action, for example: + +```php +use Yiisoft\ActiveRecord\ConnectionProviderMiddleware; +use Yiisoft\Router\Route; + +Route::methods([Method::GET, Method::POST], '/user/register') + ->middleware(ConnectionProviderMiddleware::class) + ->action([Register::class, 'register']) + ->name('user/register'); +``` + +Or, if you use `yiisoft/config` and `yiisoft/middleware-dispatcher` packages, add the middleware to the configuration, +for example in `config/common/params.php` file: + +```php +use Yiisoft\ActiveRecord\ConnectionProviderMiddleware; + +return [ + 'middlewares' => [ + ConnectionProviderMiddleware::class, + ], +]; +``` + +_For more information about how to configure middleware, follow [Middleware Documentation](https://github.com/yiisoft/docs/blob/master/guide/en/structure/middleware.md)_ + +Now you can use the Active Record in the action: ```php use App\Entity\User; @@ -134,12 +167,9 @@ use Yiisoft\ActiveRecord\ActiveRecordFactory; final class Register { - public function register( - ActiveRecordFactory $arFactory - ): ResponseInterface { - /** Connected AR by factory di. */ - $user = $arFactory->createAR(User::class); - + public function register(): ResponseInterface + { + $user = new User(); $user->setAttribute('username', 'yiiliveext'); $user->setAttribute('email', 'yiiliveext@mail.ru'); $user->save(); diff --git a/composer-require-checker.json b/composer-require-checker.json new file mode 100644 index 000000000..c17694e98 --- /dev/null +++ b/composer-require-checker.json @@ -0,0 +1,8 @@ +{ + "symbol-whitelist": [ + "Psr\\Http\\Message\\ResponseInterface", + "Psr\\Http\\Message\\ServerRequestInterface", + "Psr\\Http\\Server\\MiddlewareInterface", + "Psr\\Http\\Server\\RequestHandlerInterface" + ] +} diff --git a/composer.json b/composer.json index 5e4c3b2db..dfd088270 100644 --- a/composer.json +++ b/composer.json @@ -32,8 +32,7 @@ "php": "^8.1", "ext-json": "*", "yiisoft/arrays": "^3.0", - "yiisoft/db": "dev-master", - "yiisoft/factory": "^1.0" + "yiisoft/db": "dev-master" }, "require-dev": { "maglnet/composer-require-checker": "^4.2", @@ -46,7 +45,16 @@ "yiisoft/cache": "^3.0", "yiisoft/db-sqlite": "dev-master", "yiisoft/di": "^1.0", - "yiisoft/json": "^1.0" + "yiisoft/json": "^1.0", + "yiisoft/middleware-dispatcher": "^5.2" + }, + "suggest": { + "yiisoft/db-sqlite": "For SQLite database support", + "yiisoft/db-mysql": "For MySQL database support", + "yiisoft/db-pgsql": "For PostgreSQL database support", + "yiisoft/db-mssql": "For MSSQL database support", + "yiisoft/db-oracle": "For Oracle database support", + "yiisoft/middleware-dispatcher": "For middleware support" }, "autoload": { "psr-4": { diff --git a/src/AbstractActiveRecord.php b/src/AbstractActiveRecord.php index 7bc696e02..d6e1aabd5 100644 --- a/src/AbstractActiveRecord.php +++ b/src/AbstractActiveRecord.php @@ -48,11 +48,6 @@ abstract class AbstractActiveRecord implements ActiveRecordInterface /** @psalm-var string[][] */ private array $relationsDependencies = []; - public function __construct( - private ConnectionInterface $db - ) { - } - /** * Returns the public and protected property values of an Active Record object. * @@ -89,7 +84,7 @@ public function delete(): int public function deleteAll(array $condition = [], array $params = []): int { - $command = $this->db->createCommand(); + $command = $this->db()->createCommand(); $command->delete($this->getTableName(), $condition, $params); return $command->execute(); @@ -328,7 +323,7 @@ public function insert(array $attributes = null): bool */ public function instantiateQuery(string|ActiveRecordInterface|Closure $arClass): ActiveQueryInterface { - return new ActiveQuery($arClass, $this->db); + return new ActiveQuery($arClass); } /** @@ -436,7 +431,7 @@ public function link(string $name, ActiveRecordInterface $arClass, array $extraC $viaClass->insert(); } elseif (is_string($viaTable)) { - $this->db->createCommand()->insert($viaTable, $columns)->execute(); + $this->db()->createCommand()->insert($viaTable, $columns)->execute(); } } else { $link = $relation->getLink(); @@ -718,7 +713,7 @@ public function update(array $attributeNames = null): int public function updateAll(array $attributes, array|string $condition = [], array $params = []): int { - $command = $this->db->createCommand(); + $command = $this->db()->createCommand(); $command->update($this->getTableName(), $attributes, $condition, $params); @@ -787,7 +782,7 @@ public function updateAllCounters(array $counters, array|string $condition = '', $n++; } - $command = $this->db->createCommand(); + $command = $this->db()->createCommand(); $command->update($this->getTableName(), $counters, $condition, $params); return $command->execute(); @@ -883,7 +878,7 @@ public function unlink(string $name, ActiveRecordInterface $arClass, bool $delet $viaClass->updateAll($nulls, $columns); } } elseif (is_string($viaTable)) { - $command = $this->db->createCommand(); + $command = $this->db()->createCommand(); if ($delete) { $command->delete($viaTable, $columns)->execute(); } else { @@ -996,7 +991,7 @@ public function unlinkAll(string $name, bool $delete = false): void $viaClass->updateAll($nulls, $condition); } } elseif (is_string($viaTable)) { - $command = $this->db->createCommand(); + $command = $this->db()->createCommand(); if ($delete) { $command->delete($viaTable, $condition)->execute(); } else { @@ -1252,8 +1247,8 @@ public function getTableName(): string return '{{%' . DbStringHelper::pascalCaseToId(DbStringHelper::baseName(static::class)) . '}}'; } - protected function db(): ConnectionInterface + public function db(): ConnectionInterface { - return $this->db; + return ConnectionProvider::get(); } } diff --git a/src/ActiveQuery.php b/src/ActiveQuery.php index 7b678f019..c022fe98a 100644 --- a/src/ActiveQuery.php +++ b/src/ActiveQuery.php @@ -8,7 +8,6 @@ use ReflectionException; use Throwable; use Yiisoft\Db\Command\CommandInterface; -use Yiisoft\Db\Connection\ConnectionInterface; use Yiisoft\Db\Exception\Exception; use Yiisoft\Db\Exception\InvalidArgumentException; use Yiisoft\Db\Exception\InvalidConfigException; @@ -21,7 +20,6 @@ use Yiisoft\Db\QueryBuilder\QueryBuilderInterface; use Yiisoft\Definitions\Exception\CircularReferenceException; use Yiisoft\Definitions\Exception\NotInstantiableException; -use Yiisoft\Factory\NotFoundException; use function array_column; use function array_combine; @@ -118,10 +116,9 @@ class ActiveQuery extends Query implements ActiveQueryInterface * @psalm-param ARClass $arClass */ final public function __construct( - protected string|ActiveRecordInterface|Closure $arClass, - protected ConnectionInterface $db + protected string|ActiveRecordInterface|Closure $arClass ) { - parent::__construct($db); + parent::__construct($this->getARInstance()->db()); } /** @@ -163,7 +160,6 @@ public function each(int $batchSize = 100): BatchQueryResultInterface * @throws CircularReferenceException * @throws Exception * @throws InvalidConfigException - * @throws NotFoundException * @throws NotInstantiableException * @throws Throwable * @throws \Yiisoft\Definitions\Exception\InvalidConfigException @@ -287,7 +283,6 @@ public function populate(array $rows, Closure|string|null $indexBy = null): arra * @throws CircularReferenceException * @throws Exception * @throws InvalidConfigException - * @throws NotFoundException * @throws NotInstantiableException * * @return array The distinctive models. @@ -473,7 +468,6 @@ public function resetJoinWith(): void /** * @throws CircularReferenceException * @throws InvalidConfigException - * @throws NotFoundException * @throws NotInstantiableException * @throws \Yiisoft\Definitions\Exception\InvalidConfigException */ @@ -552,7 +546,6 @@ public function innerJoinWith(array|string $with, array|bool $eagerLoading = tru * * @throws CircularReferenceException * @throws InvalidConfigException - * @throws NotFoundException * @throws NotInstantiableException * @throws \Yiisoft\Definitions\Exception\InvalidConfigException */ @@ -635,7 +628,6 @@ private function getJoinType(array|string $joinType, string $name): string * * @throws CircularReferenceException * @throws InvalidConfigException - * @throws NotFoundException * @throws NotInstantiableException */ private function getTableNameAndAlias(): array @@ -672,9 +664,8 @@ private function getTableNameAndAlias(): array * @param string $joinType The join type. * * @throws CircularReferenceException - * @throws NotFoundException * @throws NotInstantiableException - * @throws \Yiisoft\Definitions\Exception\InvalidConfigException + * @throws InvalidConfigException */ private function joinWithRelation(ActiveQueryInterface $parent, ActiveQueryInterface $child, string $joinType): void { @@ -803,7 +794,7 @@ public function orOnCondition(array|string $condition, array $params = []): self public function viaTable(string $tableName, array $link, callable $callable = null): self { $arClass = $this->primaryModel ?? $this->arClass; - $arClassInstance = new self($arClass, $this->db); + $arClassInstance = new self($arClass); /** @psalm-suppress UndefinedMethod */ $relation = $arClassInstance->from([$tableName])->link($link)->multiple(true)->asArray(); @@ -839,7 +830,6 @@ public function alias(string $alias): self /** * @throws CircularReferenceException * @throws InvalidArgumentException - * @throws NotFoundException * @throws NotInstantiableException * @throws \Yiisoft\Definitions\Exception\InvalidConfigException */ @@ -854,7 +844,6 @@ public function getTablesUsedInFrom(): array /** * @throws CircularReferenceException - * @throws NotFoundException * @throws NotInstantiableException * @throws \Yiisoft\Definitions\Exception\InvalidConfigException */ @@ -920,7 +909,6 @@ public function findAll(mixed $condition): array * @throws CircularReferenceException * @throws Exception * @throws InvalidArgumentException - * @throws NotFoundException * @throws NotInstantiableException */ protected function findByCondition(mixed $condition): static @@ -982,7 +970,7 @@ public function getARClassName(): string } if ($this->arClass instanceof Closure) { - return ($this->arClass)($this->db)::class; + return ($this->arClass)()::class; } return $this->arClass; @@ -995,18 +983,18 @@ public function getARInstance(): ActiveRecordInterface } if ($this->arClass instanceof Closure) { - return ($this->arClass)($this->db); + return ($this->arClass)(); } /** @psalm-var class-string $class */ $class = $this->arClass; - return new $class($this->db); + return new $class(); } private function createInstance(): static { - return (new static($this->arClass, $this->db)) + return (new static($this->arClass)) ->where($this->getWhere()) ->limit($this->getLimit()) ->offset($this->getOffset()) diff --git a/src/ActiveQueryInterface.php b/src/ActiveQueryInterface.php index 8fe0ed423..3d37e02ea 100644 --- a/src/ActiveQueryInterface.php +++ b/src/ActiveQueryInterface.php @@ -7,14 +7,12 @@ use Closure; use ReflectionException; use Throwable; -use Yiisoft\Db\Connection\ConnectionInterface; use Yiisoft\Db\Exception\Exception; use Yiisoft\Db\Exception\InvalidArgumentException; use Yiisoft\Db\Exception\InvalidConfigException; use Yiisoft\Db\Query\QueryInterface; use Yiisoft\Definitions\Exception\CircularReferenceException; use Yiisoft\Definitions\Exception\NotInstantiableException; -use Yiisoft\Factory\NotFoundException; /** * Defines the common interface to be implemented by active record query classes. @@ -24,7 +22,7 @@ * * A class implementing this interface should also use {@see ActiveQueryTrait} and {@see ActiveRelationTrait}. * - * @psalm-type ARClass = class-string|ActiveRecordInterface|Closure(ConnectionInterface):ActiveRecordInterface + * @psalm-type ARClass = class-string|ActiveRecordInterface|Closure():ActiveRecordInterface */ interface ActiveQueryInterface extends QueryInterface { @@ -275,7 +273,6 @@ public function viaTable(string $tableName, array $link, callable $callable = nu * @param string $alias The table alias. * * @throws CircularReferenceException - * @throws NotFoundException * @throws NotInstantiableException * @throws \Yiisoft\Definitions\Exception\InvalidConfigException */ @@ -288,7 +285,6 @@ public function alias(string $alias): self; * * @throws CircularReferenceException * @throws InvalidArgumentException - * @throws NotFoundException * @throws NotInstantiableException * @throws \Yiisoft\Definitions\Exception\InvalidConfigException */ @@ -592,7 +588,6 @@ public function getLink(): array; /** * @throws CircularReferenceException * @throws InvalidConfigException - * @throws NotFoundException * @throws NotInstantiableException * @return ActiveRecordInterface The model instance associated with this query. */ diff --git a/src/ActiveRecordFactory.php b/src/ActiveRecordFactory.php deleted file mode 100644 index 795e3b585..000000000 --- a/src/ActiveRecordFactory.php +++ /dev/null @@ -1,87 +0,0 @@ - $arClass - * @psalm-return T - */ - public function createAR( - string $arClass, - ConnectionInterface $db = null - ): ActiveRecordInterface { - $params = []; - $params['class'] = $arClass; - - if ($db !== null) { - $params['__construct()']['db'] = $db; - } - - return $this->factory->create($params); - } - - /** - * Allows you to create an active query instance through the factory. - * - * @param ActiveRecordInterface|Closure|string $arClass the active record class, active record instance or closure - * returning active record instance. - * @param string $queryClass custom query active query class. - * @param ConnectionInterface|null $db the database connection used for creating active query instances. - * - * @throws CircularReferenceException - * @throws InvalidConfigException - * @throws NotFoundException - * @throws NotInstantiableException - * - * @psalm-param ARClass $arClass - */ - public function createQueryTo( - string|ActiveRecordInterface|Closure $arClass, - string $queryClass = ActiveQuery::class, - ConnectionInterface $db = null - ): ActiveQueryInterface { - $params = [ - 'class' => $queryClass, - '__construct()' => [ - 'arClass' => $arClass, - ], - ]; - - if ($db !== null) { - $params['__construct()']['db'] = $db; - } - - return $this->factory->create($params); - } -} diff --git a/src/ActiveRecordInterface.php b/src/ActiveRecordInterface.php index 21d8a2327..a54f65ef8 100644 --- a/src/ActiveRecordInterface.php +++ b/src/ActiveRecordInterface.php @@ -5,6 +5,7 @@ namespace Yiisoft\ActiveRecord; use Throwable; +use Yiisoft\Db\Connection\ConnectionInterface; use Yiisoft\Db\Exception\Exception; use Yiisoft\Db\Exception\InvalidArgumentException; use Yiisoft\Db\Exception\InvalidConfigException; @@ -24,6 +25,11 @@ interface ActiveRecordInterface */ public function attributes(): array; + /** + * Returns the database connection used by the Active Record instance. + */ + public function db(): ConnectionInterface; + /** * Deletes the table row corresponding to this active record. * diff --git a/src/ConnectionProvider.php b/src/ConnectionProvider.php new file mode 100644 index 000000000..909e67b46 --- /dev/null +++ b/src/ConnectionProvider.php @@ -0,0 +1,60 @@ +db); + + return $handler->handle($request); + } +} diff --git a/src/Trait/CustomConnectionTrait.php b/src/Trait/CustomConnectionTrait.php new file mode 100644 index 000000000..713500c15 --- /dev/null +++ b/src/Trait/CustomConnectionTrait.php @@ -0,0 +1,38 @@ +connectionName = $connectionName; + return $new; + } + + public function db(): ConnectionInterface + { + if (!empty($this->connectionName)) { + return ConnectionProvider::get($this->connectionName); + } + + return parent::db(); + } +} diff --git a/tests/ActiveQueryFindTest.php b/tests/ActiveQueryFindTest.php index 519cb4993..47314ced9 100644 --- a/tests/ActiveQueryFindTest.php +++ b/tests/ActiveQueryFindTest.php @@ -17,23 +17,23 @@ abstract class ActiveQueryFindTest extends TestCase { public function testFindAll(): void { - $this->checkFixture($this->db, 'customer', true); + $this->checkFixture($this->db(), 'customer', true); - $customerQuery = new ActiveQuery(Customer::class, $this->db); + $customerQuery = new ActiveQuery(Customer::class); $this->assertCount(1, $customerQuery->findAll(3)); - $customerQuery = new ActiveQuery(Customer::class, $this->db); + $customerQuery = new ActiveQuery(Customer::class); $this->assertCount(1, $customerQuery->findAll(['id' => 1])); - $customerQuery = new ActiveQuery(Customer::class, $this->db); + $customerQuery = new ActiveQuery(Customer::class); $this->assertCount(3, $customerQuery->findAll(['id' => [1, 2, 3]])); } public function testFindScalar(): void { - $this->checkFixture($this->db, 'customer'); + $this->checkFixture($this->db(), 'customer'); - $customerQuery = new ActiveQuery(Customer::class, $this->db); + $customerQuery = new ActiveQuery(Customer::class); /** query scalar */ $customerName = $customerQuery->where(['[[id]]' => 2])->select('[[name]]')->scalar(); @@ -43,9 +43,9 @@ public function testFindScalar(): void public function testFindExists(): void { - $this->checkFixture($this->db, 'customer'); + $this->checkFixture($this->db(), 'customer'); - $customerQuery = new ActiveQuery(Customer::class, $this->db); + $customerQuery = new ActiveQuery(Customer::class); $this->assertTrue($customerQuery->where(['[[id]]' => 2])->exists()); $this->assertTrue($customerQuery->where(['[[id]]' => 2])->select('[[name]]')->exists()); @@ -56,9 +56,9 @@ public function testFindExists(): void public function testFindColumn(): void { - $this->checkFixture($this->db, 'customer'); + $this->checkFixture($this->db(), 'customer'); - $customerQuery = new ActiveQuery(Customer::class, $this->db); + $customerQuery = new ActiveQuery(Customer::class); $this->assertEquals( ['user1', 'user2', 'user3'], @@ -73,9 +73,9 @@ public function testFindColumn(): void public function testFindBySql(): void { - $this->checkFixture($this->db, 'customer'); + $this->checkFixture($this->db(), 'customer'); - $customerQuery = new ActiveQuery(Customer::class, $this->db); + $customerQuery = new ActiveQuery(Customer::class); /** find onePopulate */ $customers = $customerQuery->findBySql('SELECT * FROM {{customer}} ORDER BY [[id]] DESC')->onePopulate(); @@ -96,9 +96,9 @@ public function testFindBySql(): void public function testFindLazyViaTable(): void { - $this->checkFixture($this->db, 'order'); + $this->checkFixture($this->db(), 'order'); - $orderQuery = new ActiveQuery(Order::class, $this->db); + $orderQuery = new ActiveQuery(Order::class); $orders = $orderQuery->findOne(2); $this->assertCount(0, $orders->getBooks()); @@ -110,9 +110,9 @@ public function testFindLazyViaTable(): void public function testFindEagerViaTable(): void { - $this->checkFixture($this->db, 'order'); + $this->checkFixture($this->db(), 'order'); - $orderQuery = new ActiveQuery(Order::class, $this->db); + $orderQuery = new ActiveQuery(Order::class); $orders = $orderQuery->with('books')->orderBy('id')->all(); $this->assertCount(3, $orders); @@ -132,7 +132,7 @@ public function testFindEagerViaTable(): void $this->assertEquals(2, $order->getBooks()[0]->getAttribute('id')); /** https://github.com/yiisoft/yii2/issues/1402 */ - $orderQuery = new ActiveQuery(Order::class, $this->db); + $orderQuery = new ActiveQuery(Order::class); $orders = $orderQuery->with('books')->orderBy('id')->asArray()->all(); $this->assertCount(3, $orders); $this->assertIsArray($orders[0]['orderItems'][0]); @@ -151,9 +151,9 @@ public function testFindEagerViaTable(): void */ public function testFindCompositeRelationWithJoin(): void { - $this->checkFixture($this->db, 'order_item'); + $this->checkFixture($this->db(), 'order_item'); - $orderItemQuery = new ActiveQuery(OrderItem::class, $this->db); + $orderItemQuery = new ActiveQuery(OrderItem::class); /** @var $orderItems OrderItem */ $orderItems = $orderItemQuery->findOne([1, 1]); @@ -167,9 +167,9 @@ public function testFindCompositeRelationWithJoin(): void public function testFindSimpleRelationWithJoin(): void { - $this->checkFixture($this->db, 'order'); + $this->checkFixture($this->db(), 'order'); - $orderQuery = new ActiveQuery(Order::class, $this->db); + $orderQuery = new ActiveQuery(Order::class); $orders = $orderQuery->findOne(1); $customerNoJoin = $orders->getCustomer(); @@ -186,10 +186,10 @@ public function testFindSimpleRelationWithJoin(): void public function testFindOneByColumnName(): void { - $this->checkFixture($this->db, 'customer'); + $this->checkFixture($this->db(), 'customer'); - $customer = new ActiveQuery(Customer::class, $this->db); - $customerQuery = new CustomerQuery(Customer::class, $this->db); + $customer = new ActiveQuery(Customer::class); + $customerQuery = new CustomerQuery(Customer::class); $arClass = $customer->findOne(['id' => 1]); $this->assertEquals(1, $arClass->getId()); @@ -204,9 +204,9 @@ public function testFindOneByColumnName(): void public function testFind(): void { - $this->checkFixture($this->db, 'customer'); + $this->checkFixture($this->db(), 'customer'); - $customerQuery = new ActiveQuery(Customer::class, $this->db); + $customerQuery = new ActiveQuery(Customer::class); $this->assertInstanceOf(ActiveQueryInterface::class, $customerQuery); /** find one */ @@ -214,7 +214,7 @@ public function testFind(): void $this->assertInstanceOf(Customer::class, $customer); /** find all */ - $customerQuery = new ActiveQuery(Customer::class, $this->db); + $customerQuery = new ActiveQuery(Customer::class); $customers = $customerQuery->allPopulate(); $this->assertCount(3, $customers); $this->assertInstanceOf(Customer::class, $customers[0]); @@ -222,7 +222,7 @@ public function testFind(): void $this->assertInstanceOf(Customer::class, $customers[2]); /** find by a single primary key */ - $customerQuery = new ActiveQuery(Customer::class, $this->db); + $customerQuery = new ActiveQuery(Customer::class); $customer = $customerQuery->findOne(2); $this->assertInstanceOf(Customer::class, $customer); $this->assertEquals('user2', $customer->getName()); @@ -230,7 +230,7 @@ public function testFind(): void $customer = $customerQuery->findOne(5); $this->assertNull($customer); - $customerQuery = new ActiveQuery(Customer::class, $this->db); + $customerQuery = new ActiveQuery(Customer::class); $customer = $customerQuery->findOne(['id' => [5, 6, 1]]); $this->assertInstanceOf(Customer::class, $customer); @@ -238,7 +238,7 @@ public function testFind(): void $this->assertNotNull($customer); /** find by column values */ - $customerQuery = new ActiveQuery(Customer::class, $this->db); + $customerQuery = new ActiveQuery(Customer::class); $customer = $customerQuery->findOne(['id' => 2, 'name' => 'user2']); $this->assertInstanceOf(Customer::class, $customer); $this->assertEquals('user2', $customer->getName()); @@ -253,23 +253,23 @@ public function testFind(): void $this->assertNull($customer); /** find by attributes */ - $customerQuery = new ActiveQuery(Customer::class, $this->db); + $customerQuery = new ActiveQuery(Customer::class); $customer = $customerQuery->where(['name' => 'user2'])->onePopulate(); $this->assertInstanceOf(Customer::class, $customer); $this->assertEquals(2, $customer->getId()); /** scope */ - $customerQuery = new CustomerQuery(Customer::class, $this->db); + $customerQuery = new CustomerQuery(Customer::class); $this->assertCount(2, $customerQuery->active()->allPopulate()); $this->assertEquals(2, $customerQuery->active()->count()); } public function testFindAsArray(): void { - $this->checkFixture($this->db, 'customer'); + $this->checkFixture($this->db(), 'customer'); /** asArray */ - $customerQuery = new ActiveQuery(Customer::class, $this->db); + $customerQuery = new ActiveQuery(Customer::class); $customer = $customerQuery->where(['id' => 2])->asArray()->one(); $this->assertEquals([ 'id' => 2, @@ -282,7 +282,7 @@ public function testFindAsArray(): void ], $customer); /** find all asArray */ - $customerQuery = new ActiveQuery(Customer::class, $this->db); + $customerQuery = new ActiveQuery(Customer::class); $customers = $customerQuery->asArray()->all(); $this->assertCount(3, $customers); $this->assertArrayHasKey('id', $customers[0]); @@ -304,9 +304,9 @@ public function testFindAsArray(): void public function testFindIndexBy(): void { - $this->checkFixture($this->db, 'customer'); + $this->checkFixture($this->db(), 'customer'); - $customerQuery = new ActiveQuery(Customer::class, $this->db); + $customerQuery = new ActiveQuery(Customer::class); $customers = $customerQuery->indexBy('name')->orderBy('id')->all(); @@ -316,7 +316,7 @@ public function testFindIndexBy(): void $this->assertInstanceOf(Customer::class, $customers['user3']); /** indexBy callable */ - $customer = new ActiveQuery(Customer::class, $this->db); + $customer = new ActiveQuery(Customer::class); $customers = $customer ->indexBy(fn (Customer $customer) => $customer->getId() . '-' . $customer->getName()) @@ -331,9 +331,9 @@ public function testFindIndexBy(): void public function testFindIndexByAsArray(): void { - $this->checkFixture($this->db, 'customer'); + $this->checkFixture($this->db(), 'customer'); - $customerQuery = new ActiveQuery(Customer::class, $this->db); + $customerQuery = new ActiveQuery(Customer::class); $customers = $customerQuery->asArray()->indexBy('name')->all(); $this->assertCount(3, $customers); $this->assertArrayHasKey('id', $customers['user1']); @@ -353,7 +353,7 @@ public function testFindIndexByAsArray(): void $this->assertArrayHasKey('status', $customers['user3']); /** indexBy callable + asArray */ - $customerQuery = new ActiveQuery(Customer::class, $this->db); + $customerQuery = new ActiveQuery(Customer::class); $customers = $customerQuery->indexBy(fn ($customer) => $customer['id'] . '-' . $customer['name'])->asArray()->all(); $this->assertCount(3, $customers); $this->assertArrayHasKey('id', $customers['1-user1']); @@ -375,16 +375,16 @@ public function testFindIndexByAsArray(): void public function testFindCount(): void { - $this->checkFixture($this->db, 'customer'); + $this->checkFixture($this->db(), 'customer'); - $customerQuery = new ActiveQuery(Customer::class, $this->db); + $customerQuery = new ActiveQuery(Customer::class); $this->assertEquals(3, $customerQuery->count()); $this->assertEquals(1, $customerQuery->where(['id' => 1])->count()); $this->assertEquals(2, $customerQuery->where(['id' => [1, 2]])->count()); $this->assertEquals(2, $customerQuery->where(['id' => [1, 2]])->offset(1)->count()); $this->assertEquals(2, $customerQuery->where(['id' => [1, 2]])->offset(2)->count()); - $customerQuery = new ActiveQuery(Customer::class, $this->db); + $customerQuery = new ActiveQuery(Customer::class); $this->assertEquals(3, $customerQuery->limit(1)->count()); $this->assertEquals(3, $customerQuery->limit(2)->count()); $this->assertEquals(3, $customerQuery->limit(10)->count()); @@ -393,20 +393,20 @@ public function testFindCount(): void public function testFindLimit(): void { - $this->checkFixture($this->db, 'customer'); + $this->checkFixture($this->db(), 'customer'); /** one */ - $customerQuery = new ActiveQuery(Customer::class, $this->db); + $customerQuery = new ActiveQuery(Customer::class); $customer = $customerQuery->orderBy('id')->onePopulate(); $this->assertEquals('user1', $customer->getName()); /** all */ - $customerQuery = new ActiveQuery(Customer::class, $this->db); + $customerQuery = new ActiveQuery(Customer::class); $customers = $customerQuery->allPopulate(); $this->assertCount(3, $customers); /** limit */ - $customerQuery = new ActiveQuery(Customer::class, $this->db); + $customerQuery = new ActiveQuery(Customer::class); $customers = $customerQuery->orderBy('id')->limit(1)->allPopulate(); $this->assertCount(1, $customers); $this->assertEquals('user1', $customers[0]->getName()); @@ -428,7 +428,7 @@ public function testFindLimit(): void $this->assertCount(0, $customers); /** offset */ - $customerQuery = new ActiveQuery(Customer::class, $this->db); + $customerQuery = new ActiveQuery(Customer::class); $customer = $customerQuery->orderBy('id')->offset(0)->onePopulate(); $this->assertEquals('user1', $customer->getName()); @@ -444,9 +444,9 @@ public function testFindLimit(): void public function testFindComplexCondition(): void { - $this->checkFixture($this->db, 'customer'); + $this->checkFixture($this->db(), 'customer'); - $customerQuery = new ActiveQuery(Customer::class, $this->db); + $customerQuery = new ActiveQuery(Customer::class); $this->assertEquals( 2, @@ -481,9 +481,9 @@ public function testFindComplexCondition(): void public function testFindNullValues(): void { - $this->checkFixture($this->db, 'customer'); + $this->checkFixture($this->db(), 'customer'); - $customerQuery = new ActiveQuery(Customer::class, $this->db); + $customerQuery = new ActiveQuery(Customer::class); $customer = $customerQuery->findOne(2); $customer->setName(null); @@ -496,9 +496,9 @@ public function testFindNullValues(): void public function testFindEager(): void { - $this->checkFixture($this->db, 'customer'); + $this->checkFixture($this->db(), 'customer'); - $customerQuery = new ActiveQuery(Customer::class, $this->db); + $customerQuery = new ActiveQuery(Customer::class); $customers = $customerQuery->with('orders')->indexBy('id')->all(); ksort($customers); @@ -519,7 +519,7 @@ public function testFindEager(): void $this->assertCount(1, $customer->getRelatedRecords()); /** multiple with() calls */ - $orderQuery = new ActiveQuery(Order::class, $this->db); + $orderQuery = new ActiveQuery(Order::class); $orders = $orderQuery->with('customer', 'items')->all(); $this->assertCount(3, $orders); $this->assertTrue($orders[0]->isRelationPopulated('customer')); @@ -533,9 +533,9 @@ public function testFindEager(): void public function testFindEagerViaRelation(): void { - $this->checkFixture($this->db, 'order'); + $this->checkFixture($this->db(), 'order'); - $orderQuery = new ActiveQuery(Order::class, $this->db); + $orderQuery = new ActiveQuery(Order::class); $orders = $orderQuery->with('items')->orderBy('id')->all(); $this->assertCount(3, $orders); @@ -549,9 +549,9 @@ public function testFindEagerViaRelation(): void public function testFindNestedRelation(): void { - $this->checkFixture($this->db, 'customer'); + $this->checkFixture($this->db(), 'customer'); - $customerQuery = new ActiveQuery(Customer::class, $this->db); + $customerQuery = new ActiveQuery(Customer::class); $customers = $customerQuery->with('orders', 'orders.items')->indexBy('id')->all(); ksort($customers); @@ -585,9 +585,9 @@ public function testFindNestedRelation(): void */ public function testFindEagerViaRelationPreserveOrder(): void { - $this->checkFixture($this->db, 'order'); + $this->checkFixture($this->db(), 'order'); - $orderQuery = new ActiveQuery(Order::class, $this->db); + $orderQuery = new ActiveQuery(Order::class); $orders = $orderQuery->with('itemsInOrder1')->orderBy('created_at')->all(); $this->assertCount(3, $orders); @@ -615,10 +615,10 @@ public function testFindEagerViaRelationPreserveOrder(): void public function testFindEagerViaRelationPreserveOrderB(): void { - $this->checkFixture($this->db, 'order'); + $this->checkFixture($this->db(), 'order'); /** different order in via table. */ - $orderQuery = new ActiveQuery(Order::class, $this->db); + $orderQuery = new ActiveQuery(Order::class); $orders = $orderQuery->with('itemsInOrder2')->orderBy('created_at')->all(); $this->assertCount(3, $orders); @@ -646,9 +646,9 @@ public function testFindEagerViaRelationPreserveOrderB(): void public function testFindEmptyInCondition(): void { - $this->checkFixture($this->db, 'customer'); + $this->checkFixture($this->db(), 'customer'); - $customerQuery = new ActiveQuery(Customer::class, $this->db); + $customerQuery = new ActiveQuery(Customer::class); $customers = $customerQuery->where(['id' => [1]])->all(); $this->assertCount(1, $customers); @@ -664,9 +664,9 @@ public function testFindEmptyInCondition(): void public function testFindEagerIndexBy(): void { - $this->checkFixture($this->db, 'order'); + $this->checkFixture($this->db(), 'order'); - $orderQuery = new ActiveQuery(Order::class, $this->db); + $orderQuery = new ActiveQuery(Order::class); $order = $orderQuery->with('itemsIndexed')->where(['id' => 1])->onePopulate(); $this->assertTrue($order->isRelationPopulated('itemsIndexed')); @@ -687,9 +687,9 @@ public function testFindEagerIndexBy(): void public function testFindLazy(): void { - $this->checkFixture($this->db, 'customer'); + $this->checkFixture($this->db(), 'customer'); - $customerQuery = new ActiveQuery(Customer::class, $this->db); + $customerQuery = new ActiveQuery(Customer::class); $customer = $customerQuery->findOne(2); $this->assertFalse($customer->isRelationPopulated('orders')); @@ -713,9 +713,9 @@ public function testFindLazy(): void public function testFindLazyVia(): void { - $this->checkFixture($this->db, 'order'); + $this->checkFixture($this->db(), 'order'); - $orderQuery = new ActiveQuery(Order::class, $this->db); + $orderQuery = new ActiveQuery(Order::class); $order = $orderQuery->findOne(1); $this->assertEquals(1, $order->getId()); diff --git a/tests/ActiveQueryTest.php b/tests/ActiveQueryTest.php index a9fb46a96..a714c8716 100644 --- a/tests/ActiveQueryTest.php +++ b/tests/ActiveQueryTest.php @@ -23,7 +23,6 @@ use Yiisoft\ActiveRecord\Tests\Support\Assert; use Yiisoft\ActiveRecord\Tests\Support\DbHelper; use Yiisoft\Db\Command\AbstractCommand; -use Yiisoft\Db\Connection\ConnectionInterface; use Yiisoft\Db\Exception\Exception; use Yiisoft\Db\Exception\InvalidArgumentException; use Yiisoft\Db\Exception\InvalidCallException; @@ -39,9 +38,9 @@ abstract class ActiveQueryTest extends TestCase { public function testOptions(): void { - $this->checkFixture($this->db, 'customer', true); + $this->checkFixture($this->db(), 'customer', true); - $customerQuery = new ActiveQuery(Customer::class, $this->db); + $customerQuery = new ActiveQuery(Customer::class); $query = $customerQuery->on(['a' => 'b'])->joinWith('profile'); $this->assertEquals($query->getARClass(), Customer::class); @@ -53,25 +52,25 @@ public function testOptions(): void public function testPrepare(): void { - $this->checkFixture($this->db, 'customer'); + $this->checkFixture($this->db(), 'customer'); - $query = new ActiveQuery(Customer::class, $this->db); - $this->assertInstanceOf(QueryInterface::class, $query->prepare($this->db->getQueryBuilder())); + $query = new ActiveQuery(Customer::class); + $this->assertInstanceOf(QueryInterface::class, $query->prepare($this->db()->getQueryBuilder())); } public function testPopulateEmptyRows(): void { - $this->checkFixture($this->db, 'customer'); + $this->checkFixture($this->db(), 'customer'); - $query = new ActiveQuery(Customer::class, $this->db); + $query = new ActiveQuery(Customer::class); $this->assertEquals([], $query->populate([])); } public function testPopulateFilledRows(): void { - $this->checkFixture($this->db, 'customer'); + $this->checkFixture($this->db(), 'customer'); - $query = new ActiveQuery(Customer::class, $this->db); + $query = new ActiveQuery(Customer::class); $rows = $query->all(); $result = $query->populate($rows); $this->assertEquals($rows, $result); @@ -79,9 +78,9 @@ public function testPopulateFilledRows(): void public function testAllPopulate(): void { - $this->checkFixture($this->db, 'customer'); + $this->checkFixture($this->db(), 'customer'); - $query = new ActiveQuery(Customer::class, $this->db); + $query = new ActiveQuery(Customer::class); foreach ($query->allPopulate() as $customer) { $this->assertInstanceOf(Customer::class, $customer); @@ -92,51 +91,51 @@ public function testAllPopulate(): void public function testOnePopulate(): void { - $this->checkFixture($this->db, 'customer'); + $this->checkFixture($this->db(), 'customer'); - $query = new ActiveQuery(Customer::class, $this->db); + $query = new ActiveQuery(Customer::class); $this->assertInstanceOf(Customer::class, $query->onePopulate()); } public function testCreateCommand(): void { - $this->checkFixture($this->db, 'customer'); + $this->checkFixture($this->db(), 'customer'); - $query = new ActiveQuery(Customer::class, $this->db); + $query = new ActiveQuery(Customer::class); $this->assertInstanceOf(AbstractCommand::class, $query->createCommand()); } public function testQueryScalar(): void { - $this->checkFixture($this->db, 'customer'); + $this->checkFixture($this->db(), 'customer'); - $query = new ActiveQuery(Customer::class, $this->db); + $query = new ActiveQuery(Customer::class); $this->assertEquals('user1', Assert::invokeMethod($query, 'queryScalar', ['name'])); } public function testGetJoinWith(): void { - $this->checkFixture($this->db, 'customer'); + $this->checkFixture($this->db(), 'customer'); - $query = new ActiveQuery(Customer::class, $this->db); + $query = new ActiveQuery(Customer::class); $query->joinWith('profile'); $this->assertEquals([[['profile'], true, 'LEFT JOIN']], $query->getJoinWith()); } public function testInnerJoinWith(): void { - $this->checkFixture($this->db, 'customer'); + $this->checkFixture($this->db(), 'customer'); - $query = new ActiveQuery(Customer::class, $this->db); + $query = new ActiveQuery(Customer::class); $query->innerJoinWith('profile'); $this->assertEquals([[['profile'], true, 'INNER JOIN']], $query->getJoinWith()); } public function testBuildJoinWithRemoveDuplicateJoinByTableName(): void { - $this->checkFixture($this->db, 'customer'); + $this->checkFixture($this->db(), 'customer'); - $query = new ActiveQuery(Customer::class, $this->db); + $query = new ActiveQuery(Customer::class); $query->innerJoinWith('orders')->joinWith('orders.orderItems'); Assert::invokeMethod($query, 'buildJoinWith'); $this->assertEquals([ @@ -155,29 +154,29 @@ public function testBuildJoinWithRemoveDuplicateJoinByTableName(): void public function testGetQueryTableNameFromNotSet(): void { - $this->checkFixture($this->db, 'customer'); + $this->checkFixture($this->db(), 'customer'); - $query = new ActiveQuery(Customer::class, $this->db); + $query = new ActiveQuery(Customer::class); $this->assertEquals(['customer', 'customer'], Assert::invokeMethod($query, 'getTableNameAndAlias')); } public function testGetQueryTableNameFromSet(): void { - $this->checkFixture($this->db, 'customer'); + $this->checkFixture($this->db(), 'customer'); - $query = new ActiveQuery(Customer::class, $this->db); + $query = new ActiveQuery(Customer::class); $query->from(['alias' => 'customer']); $this->assertEquals(['customer', 'alias'], Assert::invokeMethod($query, 'getTableNameAndAlias')); } public function testOnCondition(): void { - $this->checkFixture($this->db, 'customer'); + $this->checkFixture($this->db(), 'customer'); $on = ['active' => true]; $params = ['a' => 'b']; - $query = new ActiveQuery(Customer::class, $this->db); + $query = new ActiveQuery(Customer::class); $query->onCondition($on, $params); $this->assertEquals($on, $query->getOn()); $this->assertEquals($params, $query->getParams()); @@ -185,11 +184,11 @@ public function testOnCondition(): void public function testAndOnConditionOnNotSet(): void { - $this->checkFixture($this->db, 'customer'); + $this->checkFixture($this->db(), 'customer'); $on = ['active' => true]; $params = ['a' => 'b']; - $query = new ActiveQuery(Customer::class, $this->db); + $query = new ActiveQuery(Customer::class); $query->andOnCondition($on, $params); $this->assertEquals($on, $query->getOn()); $this->assertEquals($params, $query->getParams()); @@ -197,13 +196,13 @@ public function testAndOnConditionOnNotSet(): void public function testAndOnConditionOnSet(): void { - $this->checkFixture($this->db, 'customer'); + $this->checkFixture($this->db(), 'customer'); $onOld = ['active' => true]; $on = ['active' => true]; $params = ['a' => 'b']; - $query = new ActiveQuery(Customer::class, $this->db); + $query = new ActiveQuery(Customer::class); $query->on($onOld)->andOnCondition($on, $params); @@ -213,12 +212,12 @@ public function testAndOnConditionOnSet(): void public function testOrOnConditionOnNotSet(): void { - $this->checkFixture($this->db, 'customer'); + $this->checkFixture($this->db(), 'customer'); $on = ['active' => true]; $params = ['a' => 'b']; - $query = new ActiveQuery(Customer::class, $this->db); + $query = new ActiveQuery(Customer::class); $query->orOnCondition($on, $params); @@ -228,13 +227,13 @@ public function testOrOnConditionOnNotSet(): void public function testOrOnConditionOnSet(): void { - $this->checkFixture($this->db, 'customer'); + $this->checkFixture($this->db(), 'customer'); $onOld = ['active' => true]; $on = ['active' => true]; $params = ['a' => 'b']; - $query = new ActiveQuery(Customer::class, $this->db); + $query = new ActiveQuery(Customer::class); $query->on($onOld)->orOnCondition($on, $params); @@ -244,11 +243,11 @@ public function testOrOnConditionOnSet(): void public function testViaTable(): void { - $this->checkFixture($this->db, 'customer'); + $this->checkFixture($this->db(), 'customer'); - $order = new Order($this->db); + $order = new Order(); - $query = new ActiveQuery(Customer::class, $this->db); + $query = new ActiveQuery(Customer::class); $query->primaryModel($order)->viaTable(Profile::class, ['id' => 'item_id']); @@ -258,9 +257,9 @@ public function testViaTable(): void public function testAliasNotSet(): void { - $this->checkFixture($this->db, 'customer'); + $this->checkFixture($this->db(), 'customer'); - $query = new ActiveQuery(Customer::class, $this->db); + $query = new ActiveQuery(Customer::class); $query->alias('alias'); @@ -270,11 +269,11 @@ public function testAliasNotSet(): void public function testAliasYetSet(): void { - $this->checkFixture($this->db, 'customer'); + $this->checkFixture($this->db(), 'customer'); $aliasOld = ['old']; - $query = new ActiveQuery(Customer::class, $this->db); + $query = new ActiveQuery(Customer::class); $query->from($aliasOld)->alias('alias'); @@ -284,9 +283,9 @@ public function testAliasYetSet(): void public function testGetTableNamesNotFilledFrom(): void { - $this->checkFixture($this->db, 'profile'); + $this->checkFixture($this->db(), 'profile'); - $query = new ActiveQuery(Profile::class, $this->db); + $query = new ActiveQuery(Profile::class); $tableName = Profile::TABLE_NAME; $this->assertEquals( @@ -299,9 +298,9 @@ public function testGetTableNamesNotFilledFrom(): void public function testGetTableNamesWontFillFrom(): void { - $this->checkFixture($this->db, 'profile'); + $this->checkFixture($this->db(), 'profile'); - $query = new ActiveQuery(Profile::class, $this->db); + $query = new ActiveQuery(Profile::class); $this->assertSame([], $query->getFrom()); @@ -318,10 +317,10 @@ public function testGetTableNamesWontFillFrom(): void */ public function testDeeplyNestedTableRelationWith(): void { - $this->checkFixture($this->db, 'category', true); + $this->checkFixture($this->db(), 'category', true); /** @var $category Category */ - $categoriesQuery = new ActiveQuery(Category::class, $this->db); + $categoriesQuery = new ActiveQuery(Category::class); $categories = $categoriesQuery->with('orders')->indexBy('id')->all(); $category = $categories[1]; @@ -347,9 +346,9 @@ public function testDeeplyNestedTableRelationWith(): void public function testGetSql(): void { - $this->checkFixture($this->db, 'customer'); + $this->checkFixture($this->db(), 'customer'); - $query = new ActiveQuery(Customer::class, $this->db); + $query = new ActiveQuery(Customer::class); $query->sql('SELECT * FROM {{customer}} ORDER BY [[id]] DESC'); @@ -358,12 +357,12 @@ public function testGetSql(): void public function testCustomColumns(): void { - $this->checkFixture($this->db, 'customer'); + $this->checkFixture($this->db(), 'customer'); - $customerQuery = new ActiveQuery(Customer::class, $this->db); + $customerQuery = new ActiveQuery(Customer::class); /** find custom column */ - if ($this->db->getDriverName() === 'oci') { + if ($this->db()->getDriverName() === 'oci') { $customers = $customerQuery ->select(['{{customer}}.*', '([[status]]*2) AS [[status2]]']) ->where(['name' => 'user3'])->onePopulate(); @@ -379,9 +378,9 @@ public function testCustomColumns(): void public function testCallFind(): void { - $this->checkFixture($this->db, 'customer'); + $this->checkFixture($this->db(), 'customer'); - $customerQuery = new ActiveQuery(Customer::class, $this->db); + $customerQuery = new ActiveQuery(Customer::class); /** find count, sum, average, min, max, scalar */ $this->assertEquals(3, $customerQuery->count()); @@ -398,9 +397,9 @@ public function testCallFind(): void */ public function testCountWithFindBySql(): void { - $this->checkFixture($this->db, 'customer'); + $this->checkFixture($this->db(), 'customer'); - $customerQuery = new ActiveQuery(Customer::class, $this->db); + $customerQuery = new ActiveQuery(Customer::class); $query = $customerQuery->findBySql('SELECT * FROM {{customer}}'); $this->assertEquals(3, $query->count()); @@ -411,9 +410,9 @@ public function testCountWithFindBySql(): void public function testDeeplyNestedTableRelation(): void { - $this->checkFixture($this->db, 'customer'); + $this->checkFixture($this->db(), 'customer'); - $customerQuery = new ActiveQuery(Customer::class, $this->db); + $customerQuery = new ActiveQuery(Customer::class); $customers = $customerQuery->findOne(1); $this->assertNotNull($customerQuery); @@ -435,9 +434,9 @@ public function testDeeplyNestedTableRelation(): void */ public function testDeeplyNestedTableRelation2(): void { - $this->checkFixture($this->db, 'category'); + $this->checkFixture($this->db(), 'category'); - $categoryQuery = new ActiveQuery(Category::class, $this->db); + $categoryQuery = new ActiveQuery(Category::class); $categories = $categoryQuery->where(['id' => 1])->onePopulate(); $this->assertNotNull($categories); @@ -462,10 +461,10 @@ public function testDeeplyNestedTableRelation2(): void public function testJoinWith(): void { - $this->checkFixture($this->db, 'order'); + $this->checkFixture($this->db(), 'order'); /** left join and eager loading */ - $orderQuery = new ActiveQuery(Order::class, $this->db); + $orderQuery = new ActiveQuery(Order::class); $orders = $orderQuery->joinWith('customer')->orderBy('customer.id DESC, order.id')->all(); $this->assertCount(3, $orders); $this->assertEquals(2, $orders[0]->getId()); @@ -476,7 +475,7 @@ public function testJoinWith(): void $this->assertTrue($orders[2]->isRelationPopulated('customer')); /** inner join filtering and eager loading */ - $orderQuery = new ActiveQuery(Order::class, $this->db); + $orderQuery = new ActiveQuery(Order::class); $orders = $orderQuery->innerJoinWith( [ 'customer' => function ($query) { @@ -491,7 +490,7 @@ public function testJoinWith(): void $this->assertTrue($orders[1]->isRelationPopulated('customer')); /** inner join filtering, eager loading, conditions on both primary and relation */ - $orderQuery = new ActiveQuery(Order::class, $this->db); + $orderQuery = new ActiveQuery(Order::class); $orders = $orderQuery->innerJoinWith( [ 'customer' => function ($query) { @@ -504,7 +503,7 @@ public function testJoinWith(): void $this->assertTrue($orders[0]->isRelationPopulated('customer')); /** inner join filtering without eager loading */ - $orderQuery = new ActiveQuery(Order::class, $this->db); + $orderQuery = new ActiveQuery(Order::class); $orders = $orderQuery->innerJoinWith( [ 'customer' => static function ($query) { @@ -520,7 +519,7 @@ public function testJoinWith(): void $this->assertFalse($orders[1]->isRelationPopulated('customer')); /** inner join filtering without eager loading, conditions on both primary and relation */ - $orderQuery = new ActiveQuery(Order::class, $this->db); + $orderQuery = new ActiveQuery(Order::class); $orders = $orderQuery->innerJoinWith( [ 'customer' => static function ($query) { @@ -534,7 +533,7 @@ public function testJoinWith(): void $this->assertFalse($orders[0]->isRelationPopulated('customer')); /** join with via-relation */ - $orderQuery = new ActiveQuery(Order::class, $this->db); + $orderQuery = new ActiveQuery(Order::class); $orders = $orderQuery->innerJoinWith('books')->orderBy('order.id')->all(); $this->assertCount(2, $orders); $this->assertCount(2, $orders[0]->getBooks()); @@ -545,7 +544,7 @@ public function testJoinWith(): void $this->assertTrue($orders[1]->isRelationPopulated('books')); /** join with sub-relation */ - $orderQuery = new ActiveQuery(Order::class, $this->db); + $orderQuery = new ActiveQuery(Order::class); $orders = $orderQuery->innerJoinWith( [ 'items' => function ($q) { @@ -564,7 +563,7 @@ public function testJoinWith(): void $this->assertTrue($orders[0]->getItems()[0]->isRelationPopulated('category')); /** join with table alias */ - $orderQuery = new ActiveQuery(Order::class, $this->db); + $orderQuery = new ActiveQuery(Order::class); $orders = $orderQuery->joinWith( [ 'customer' => function ($q) { @@ -581,7 +580,7 @@ public function testJoinWith(): void $this->assertTrue($orders[2]->isRelationPopulated('customer')); /** join with table alias */ - $orderQuery = new ActiveQuery(Order::class, $this->db); + $orderQuery = new ActiveQuery(Order::class); $orders = $orderQuery->joinWith('customer as c')->orderBy('c.id DESC, order.id')->all(); $this->assertCount(3, $orders); $this->assertEquals(2, $orders[0]->getId()); @@ -592,7 +591,7 @@ public function testJoinWith(): void $this->assertTrue($orders[2]->isRelationPopulated('customer')); /** join with table alias sub-relation */ - $orderQuery = new ActiveQuery(Order::class, $this->db); + $orderQuery = new ActiveQuery(Order::class); $orders = $orderQuery->innerJoinWith( [ 'items as t' => function ($q) { @@ -611,7 +610,7 @@ public function testJoinWith(): void $this->assertTrue($orders[0]->getItems()[0]->isRelationPopulated('category')); /** join with ON condition */ - $orderQuery = new ActiveQuery(Order::class, $this->db); + $orderQuery = new ActiveQuery(Order::class); $orders = $orderQuery->joinWith('books2')->orderBy('order.id')->all(); $this->assertCount(3, $orders); $this->assertCount(2, $orders[0]->getBooks2()); @@ -625,20 +624,20 @@ public function testJoinWith(): void $this->assertTrue($orders[2]->isRelationPopulated('books2')); /** lazy loading with ON condition */ - $orderQuery = new ActiveQuery(Order::class, $this->db); + $orderQuery = new ActiveQuery(Order::class); $order = $orderQuery->findOne(1); $this->assertCount(2, $order->getBooks2()); - $orderQuery = new ActiveQuery(Order::class, $this->db); + $orderQuery = new ActiveQuery(Order::class); $order = $orderQuery->findOne(2); $this->assertCount(0, $order->getBooks2()); - $order = new ActiveQuery(Order::class, $this->db); + $order = new ActiveQuery(Order::class); $order = $order->findOne(3); $this->assertCount(1, $order->getBooks2()); /** eager loading with ON condition */ - $orderQuery = new ActiveQuery(Order::class, $this->db); + $orderQuery = new ActiveQuery(Order::class); $orders = $orderQuery->with('books2')->all(); $this->assertCount(3, $orders); $this->assertCount(2, $orders[0]->getBooks2()); @@ -652,7 +651,7 @@ public function testJoinWith(): void $this->assertTrue($orders[2]->isRelationPopulated('books2')); /** join with count and query */ - $orderQuery = new ActiveQuery(Order::class, $this->db); + $orderQuery = new ActiveQuery(Order::class); $query = $orderQuery->joinWith('customer'); $count = $query->count(); $this->assertEquals(3, $count); @@ -661,7 +660,7 @@ public function testJoinWith(): void $this->assertCount(3, $orders); /** {@see https://github.com/yiisoft/yii2/issues/2880} */ - $orderQuery = new ActiveQuery(Order::class, $this->db); + $orderQuery = new ActiveQuery(Order::class); $query = $orderQuery->findOne(1); $customer = $query->getCustomerQuery()->joinWith( [ @@ -672,7 +671,7 @@ public function testJoinWith(): void )->onePopulate(); $this->assertEquals(1, $customer->getId()); - $orderQuery = new ActiveQuery(Order::class, $this->db); + $orderQuery = new ActiveQuery(Order::class); $order = $orderQuery->joinWith( [ 'items' => static function ($q) { @@ -682,7 +681,7 @@ public function testJoinWith(): void )->orderBy('order.id')->one(); /** join with sub-relation called inside Closure */ - $orderQuery = new ActiveQuery(Order::class, $this->db); + $orderQuery = new ActiveQuery(Order::class); $orders = $orderQuery->joinWith( [ 'items' => static function ($q) { @@ -708,17 +707,17 @@ public function testJoinWith(): void */ public function testJoinWithAndScope(): void { - $this->checkFixture($this->db, 'customer'); + $this->checkFixture($this->db(), 'customer'); /** hasOne inner join */ - $customer = new CustomerQuery(Customer::class, $this->db); + $customer = new CustomerQuery(Customer::class); $customers = $customer->active()->innerJoinWith('profile')->orderBy('customer.id')->all(); $this->assertCount(1, $customers); $this->assertEquals(1, $customers[0]->getId()); $this->assertTrue($customers[0]->isRelationPopulated('profile')); /** hasOne outer join */ - $customer = new CustomerQuery(Customer::class, $this->db); + $customer = new CustomerQuery(Customer::class); $customers = $customer->active()->joinWith('profile')->orderBy('customer.id')->all(); $this->assertCount(2, $customers); $this->assertEquals(1, $customers[0]->getId()); @@ -729,7 +728,7 @@ public function testJoinWithAndScope(): void $this->assertTrue($customers[1]->isRelationPopulated('profile')); /** hasMany */ - $customer = new CustomerQuery(Customer::class, $this->db); + $customer = new CustomerQuery(Customer::class); $customers = $customer->active()->joinWith( [ 'orders' => static function ($q) { @@ -753,11 +752,11 @@ public function testJoinWithAndScope(): void */ public function testJoinWithVia(): void { - $this->checkFixture($this->db, 'order'); + $this->checkFixture($this->db(), 'order'); - $orderQuery = new ActiveQuery(Order::class, $this->db); + $orderQuery = new ActiveQuery(Order::class); - $this->db->getQueryBuilder()->setSeparator("\n"); + $this->db()->getQueryBuilder()->setSeparator("\n"); $rows = $orderQuery->joinWith('itemsInOrder1')->joinWith( [ @@ -790,10 +789,10 @@ public static function aliasMethodProvider(): array public function testJoinWithAlias(string $aliasMethod): void { $orders = []; - $this->checkFixture($this->db, 'order'); + $this->checkFixture($this->db(), 'order'); /** left join and eager loading */ - $orderQuery = new ActiveQuery(Order::class, $this->db); + $orderQuery = new ActiveQuery(Order::class); $query = $orderQuery->joinWith(['customer c']); if ($aliasMethod === 'explicit') { @@ -815,7 +814,7 @@ public function testJoinWithAlias(string $aliasMethod): void $this->assertTrue($orders[2]->isRelationPopulated('customer')); /** inner join filtering and eager loading */ - $orderQuery = new ActiveQuery(Order::class, $this->db); + $orderQuery = new ActiveQuery(Order::class); $query = $orderQuery->innerJoinWith(['customer c']); if ($aliasMethod === 'explicit') { @@ -835,7 +834,7 @@ public function testJoinWithAlias(string $aliasMethod): void $this->assertTrue($orders[1]->isRelationPopulated('customer')); /** inner join filtering without eager loading */ - $orderQuery = new ActiveQuery(Order::class, $this->db); + $orderQuery = new ActiveQuery(Order::class); $query = $orderQuery->innerJoinWith(['customer c'], false); if ($aliasMethod === 'explicit') { @@ -855,7 +854,7 @@ public function testJoinWithAlias(string $aliasMethod): void $this->assertFalse($orders[1]->isRelationPopulated('customer')); /** join with via-relation */ - $orderQuery = new ActiveQuery(Order::class, $this->db); + $orderQuery = new ActiveQuery(Order::class); $query = $orderQuery->innerJoinWith(['books b']); if ($aliasMethod === 'explicit') { @@ -881,7 +880,7 @@ public function testJoinWithAlias(string $aliasMethod): void $this->assertTrue($orders[1]->isRelationPopulated('books')); /** joining sub relations */ - $orderQuery = new ActiveQuery(Order::class, $this->db); + $orderQuery = new ActiveQuery(Order::class); $query = $orderQuery->innerJoinWith( [ 'items i' => static function ($q) use ($aliasMethod) { @@ -926,7 +925,7 @@ public function testJoinWithAlias(string $aliasMethod): void if ($aliasMethod === 'explicit' || $aliasMethod === 'querysyntax') { $relationName = 'books' . ucfirst($aliasMethod); - $orderQuery = new ActiveQuery(Order::class, $this->db); + $orderQuery = new ActiveQuery(Order::class); $orders = $orderQuery->joinWith(["$relationName b"])->orderBy('order.id')->all(); $this->assertCount(3, $orders); @@ -945,7 +944,7 @@ public function testJoinWithAlias(string $aliasMethod): void if ($aliasMethod === 'explicit' || $aliasMethod === 'querysyntax') { $relationName = 'books' . ucfirst($aliasMethod) . 'A'; - $orderQuery = new ActiveQuery(Order::class, $this->db); + $orderQuery = new ActiveQuery(Order::class); $orders = $orderQuery->joinWith([(string)$relationName])->orderBy('order.id')->all(); $this->assertCount(3, $orders); @@ -961,7 +960,7 @@ public function testJoinWithAlias(string $aliasMethod): void } /** join with count and query */ - $orderQuery = new ActiveQuery(Order::class, $this->db); + $orderQuery = new ActiveQuery(Order::class); $query = $orderQuery->joinWith(['customer c']); if ($aliasMethod === 'explicit') { @@ -978,7 +977,7 @@ public function testJoinWithAlias(string $aliasMethod): void $this->assertCount(3, $orders); /** relational query */ - $orderQuery = new ActiveQuery(Order::class, $this->db); + $orderQuery = new ActiveQuery(Order::class); $order = $orderQuery->findOne(1); $customerQuery = $order->getCustomerQuery()->innerJoinWith(['orders o'], false); @@ -995,7 +994,7 @@ public function testJoinWithAlias(string $aliasMethod): void $this->assertNotNull($customer); /** join with sub-relation called inside Closure */ - $orderQuery = new ActiveQuery(Order::class, $this->db); + $orderQuery = new ActiveQuery(Order::class); $orders = $orderQuery->joinWith( [ 'items' => static function ($q) use ($aliasMethod) { @@ -1027,13 +1026,13 @@ public function testJoinWithAlias(string $aliasMethod): void */ public function testJoinWithSameTable(): void { - $this->checkFixture($this->db, 'order'); + $this->checkFixture($this->db(), 'order'); /** * join with the same table but different aliases alias is defined in the relation definition without eager * loading */ - $query = new ActiveQuery(Order::class, $this->db); + $query = new ActiveQuery(Order::class); $query->joinWith('bookItems', false)->joinWith('movieItems', false)->where(['movies.name' => 'Toy Story']); $orders = $query->all(); $this->assertCount( @@ -1046,7 +1045,7 @@ public function testJoinWithSameTable(): void $this->assertFalse($orders[0]->isRelationPopulated('movieItems')); /** with eager loading */ - $query = new ActiveQuery(Order::class, $this->db); + $query = new ActiveQuery(Order::class); $query->joinWith('bookItems', true)->joinWith('movieItems', true)->where(['movies.name' => 'Toy Story']); $orders = $query->all(); $this->assertCount( @@ -1064,7 +1063,7 @@ public function testJoinWithSameTable(): void * join with the same table but different aliases alias is defined in the call to joinWith() without eager * loading */ - $query = new ActiveQuery(Order::class, $this->db); + $query = new ActiveQuery(Order::class); $query ->joinWith( [ @@ -1091,7 +1090,7 @@ public function testJoinWithSameTable(): void $this->assertFalse($orders[0]->isRelationPopulated('itemsIndexed')); /** with eager loading, only for one relation as it would be overwritten otherwise. */ - $query = new ActiveQuery(Order::class, $this->db); + $query = new ActiveQuery(Order::class); $query ->joinWith( [ @@ -1116,7 +1115,7 @@ public function testJoinWithSameTable(): void $this->assertTrue($orders[0]->isRelationPopulated('itemsIndexed')); /** with eager loading, and the other relation */ - $query = new ActiveQuery(Order::class, $this->db); + $query = new ActiveQuery(Order::class); $query ->joinWith( [ @@ -1147,10 +1146,10 @@ public function testJoinWithSameTable(): void */ public function testJoinWithDuplicateSimple(): void { - $this->checkFixture($this->db, 'order'); + $this->checkFixture($this->db(), 'order'); /** left join and eager loading */ - $orderQuery = new ActiveQuery(Order::class, $this->db); + $orderQuery = new ActiveQuery(Order::class); $orders = $orderQuery ->innerJoinWith('customer') @@ -1172,10 +1171,10 @@ public function testJoinWithDuplicateSimple(): void */ public function testJoinWithDuplicateCallbackFiltering(): void { - $this->checkFixture($this->db, 'order'); + $this->checkFixture($this->db(), 'order'); /** inner join filtering and eager loading */ - $orderQuery = new ActiveQuery(Order::class, $this->db); + $orderQuery = new ActiveQuery(Order::class); $orders = $orderQuery ->innerJoinWith('customer') @@ -1197,10 +1196,10 @@ public function testJoinWithDuplicateCallbackFiltering(): void */ public function testJoinWithDuplicateCallbackFilteringConditionsOnPrimary(): void { - $this->checkFixture($this->db, 'order'); + $this->checkFixture($this->db(), 'order'); /** inner join filtering, eager loading, conditions on both primary and relation */ - $orderQuery = new ActiveQuery(Order::class, $this->db); + $orderQuery = new ActiveQuery(Order::class); $orders = $orderQuery ->innerJoinWith('customer') @@ -1220,10 +1219,10 @@ public function testJoinWithDuplicateCallbackFilteringConditionsOnPrimary(): voi */ public function testJoinWithDuplicateWithSubRelation(): void { - $this->checkFixture($this->db, 'order'); + $this->checkFixture($this->db(), 'order'); /** join with sub-relation */ - $orderQuery = new ActiveQuery(Order::class, $this->db); + $orderQuery = new ActiveQuery(Order::class); $orders = $orderQuery ->innerJoinWith('items') @@ -1246,10 +1245,10 @@ public function testJoinWithDuplicateWithSubRelation(): void */ public function testJoinWithDuplicateTableAlias1(): void { - $this->checkFixture($this->db, 'order'); + $this->checkFixture($this->db(), 'order'); /** join with table alias */ - $orderQuery = new ActiveQuery(Order::class, $this->db); + $orderQuery = new ActiveQuery(Order::class); $orders = $orderQuery ->innerJoinWith('customer') @@ -1273,10 +1272,10 @@ public function testJoinWithDuplicateTableAlias1(): void */ public function testJoinWithDuplicateTableAlias2(): void { - $this->checkFixture($this->db, 'order'); + $this->checkFixture($this->db(), 'order'); /** join with table alias */ - $orderQuery = new ActiveQuery(Order::class, $this->db); + $orderQuery = new ActiveQuery(Order::class); $orders = $orderQuery ->innerJoinWith('customer') @@ -1298,10 +1297,10 @@ public function testJoinWithDuplicateTableAlias2(): void */ public function testJoinWithDuplicateTableAliasSubRelation(): void { - $this->checkFixture($this->db, 'order'); + $this->checkFixture($this->db(), 'order'); /** join with table alias sub-relation */ - $orderQuery = new ActiveQuery(Order::class, $this->db); + $orderQuery = new ActiveQuery(Order::class); $orders = $orderQuery ->innerJoinWith([ @@ -1328,10 +1327,10 @@ public function testJoinWithDuplicateTableAliasSubRelation(): void */ public function testJoinWithDuplicateSubRelationCalledInsideClosure(): void { - $this->checkFixture($this->db, 'order'); + $this->checkFixture($this->db(), 'order'); /** join with sub-relation called inside Closure */ - $orderQuery = new ActiveQuery(Order::class, $this->db); + $orderQuery = new ActiveQuery(Order::class); $orders = $orderQuery ->innerJoinWith('items') @@ -1358,11 +1357,11 @@ public function testJoinWithDuplicateSubRelationCalledInsideClosure(): void public function testAlias(): void { - $this->checkFixture($this->db, 'order'); + $this->checkFixture($this->db(), 'order'); - $order = new Order($this->db); + $order = new Order(); - $query = new ActiveQuery(Order::class, $this->db); + $query = new ActiveQuery(Order::class); $this->assertSame([], $query->getFrom()); $query->alias('o'); @@ -1377,20 +1376,20 @@ public function testAlias(): void public function testInverseOf(): void { - $this->checkFixture($this->db, 'customer'); + $this->checkFixture($this->db(), 'customer'); /** eager loading: find one and all */ - $customerQuery = new ActiveQuery(Customer::class, $this->db); + $customerQuery = new ActiveQuery(Customer::class); $customer = $customerQuery->with('orders2')->where(['id' => 1])->onePopulate(); $this->assertSame($customer->getOrders2()[0]->getCustomer2(), $customer); - //$customerQuery = new ActiveQuery(Customer::class, $this->db); + //$customerQuery = new ActiveQuery(Customer::class); $customers = $customerQuery->with('orders2')->where(['id' => [1, 3]])->all(); $this->assertEmpty($customers[1]->getOrders2()); $this->assertSame($customers[0]->getOrders2()[0]->getCustomer2(), $customers[0]); /** lazy loading */ - $customerQuery = new ActiveQuery(Customer::class, $this->db); + $customerQuery = new ActiveQuery(Customer::class); $customer = $customerQuery->findOne(2); $orders = $customer->getOrders2(); $this->assertCount(2, $orders); @@ -1398,7 +1397,7 @@ public function testInverseOf(): void $this->assertSame($customer->getOrders2()[1]->getCustomer2(), $customer); /** ad-hoc lazy loading */ - $customerQuery = new ActiveQuery(Customer::class, $this->db); + $customerQuery = new ActiveQuery(Customer::class); $customer = $customerQuery->findOne(2); $orders = $customer->getOrders2Query()->all(); $this->assertCount(2, $orders); @@ -1414,42 +1413,42 @@ public function testInverseOf(): void ); /** the other way around */ - $customerQuery = new ActiveQuery(Customer::class, $this->db); + $customerQuery = new ActiveQuery(Customer::class); $customer = $customerQuery->with('orders2')->where(['id' => 1])->asArray()->onePopulate(); $this->assertSame($customer['orders2'][0]['customer2']['id'], $customer['id']); - $customerQuery = new ActiveQuery(Customer::class, $this->db); + $customerQuery = new ActiveQuery(Customer::class); $customers = $customerQuery->with('orders2')->where(['id' => [1, 3]])->asArray()->all(); $this->assertSame($customer['orders2'][0]['customer2']['id'], $customers[0]['id']); $this->assertEmpty($customers[1]['orders2']); - $orderQuery = new ActiveQuery(Order::class, $this->db); + $orderQuery = new ActiveQuery(Order::class); $orders = $orderQuery->with('customer2')->where(['id' => 1])->all(); $this->assertSame($orders[0]->getCustomer2()->getOrders2(), [$orders[0]]); - $orderQuery = new ActiveQuery(Order::class, $this->db); + $orderQuery = new ActiveQuery(Order::class); $order = $orderQuery->with('customer2')->where(['id' => 1])->onePopulate(); $this->assertSame($order->getCustomer2()->getOrders2(), [$order]); - $orderQuery = new ActiveQuery(Order::class, $this->db); + $orderQuery = new ActiveQuery(Order::class); $orders = $orderQuery->with('customer2')->where(['id' => 1])->asArray()->all(); $this->assertSame($orders[0]['customer2']['orders2'][0]['id'], $orders[0]['id']); - $orderQuery = new ActiveQuery(Order::class, $this->db); + $orderQuery = new ActiveQuery(Order::class); $order = $orderQuery->with('customer2')->where(['id' => 1])->asArray()->onePopulate(); $this->assertSame($order['customer2']['orders2'][0]['id'], $orders[0]['id']); - $orderQuery = new ActiveQuery(Order::class, $this->db); + $orderQuery = new ActiveQuery(Order::class); $orders = $orderQuery->with('customer2')->where(['id' => [1, 3]])->all(); $this->assertSame($orders[0]->getCustomer2()->getOrders2(), [$orders[0]]); $this->assertSame($orders[1]->getCustomer2()->getOrders2(), [$orders[1]]); - $orderQuery = new ActiveQuery(Order::class, $this->db); + $orderQuery = new ActiveQuery(Order::class); $orders = $orderQuery->with('customer2')->where(['id' => [2, 3]])->orderBy('id')->all(); $this->assertSame($orders[0]->getCustomer2()->getOrders2(), $orders); $this->assertSame($orders[1]->getCustomer2()->getOrders2(), $orders); - $orderQuery = new ActiveQuery(Order::class, $this->db); + $orderQuery = new ActiveQuery(Order::class); $orders = $orderQuery->with('customer2')->where(['id' => [2, 3]])->orderBy('id')->asArray()->all(); $this->assertSame($orders[0]['customer2']['orders2'][0]['id'], $orders[0]['id']); $this->assertSame($orders[0]['customer2']['orders2'][1]['id'], $orders[1]['id']); @@ -1459,17 +1458,17 @@ public function testInverseOf(): void public function testUnlinkAllViaTable(): void { - $this->checkFixture($this->db, 'order', true); + $this->checkFixture($this->db(), 'order', true); /** via table with delete. */ - $orderQuery = new ActiveQuery(Order::class, $this->db); + $orderQuery = new ActiveQuery(Order::class); $order = $orderQuery->findOne(1); $this->assertCount(2, $order->getBooksViaTable()); - $orderItemQuery = new ActiveQuery(OrderItem::class, $this->db); + $orderItemQuery = new ActiveQuery(OrderItem::class); $orderItemCount = $orderItemQuery->count(); - $itemQuery = new ActiveQuery(Item::class, $this->db); + $itemQuery = new ActiveQuery(Item::class); $this->assertEquals(5, $itemQuery->count()); $order->unlinkAll('booksViaTable', true); @@ -1480,7 +1479,7 @@ public function testUnlinkAllViaTable(): void /** via table without delete */ $this->assertCount(2, $order->getBooksWithNullFKViaTable()); - $orderItemsWithNullFKQuery = new ActiveQuery(OrderItemWithNullFK::class, $this->db); + $orderItemsWithNullFKQuery = new ActiveQuery(OrderItemWithNullFK::class); $orderItemCount = $orderItemsWithNullFKQuery->count(); $this->assertEquals(5, $itemQuery->count()); @@ -1490,17 +1489,17 @@ public function testUnlinkAllViaTable(): void ['AND', ['item_id' => [1, 2]], ['order_id' => null]] )->count()); - $orderItemsWithNullFKQuery = new ActiveQuery(OrderItemWithNullFK::class, $this->db); + $orderItemsWithNullFKQuery = new ActiveQuery(OrderItemWithNullFK::class); $this->assertEquals($orderItemCount, $orderItemsWithNullFKQuery->count()); $this->assertEquals(5, $itemQuery->count()); } public function testIssues(): void { - $this->checkFixture($this->db, 'category', true); + $this->checkFixture($this->db(), 'category', true); /** {@see https://github.com/yiisoft/yii2/issues/4938} */ - $categoryQuery = new ActiveQuery(Category::class, $this->db); + $categoryQuery = new ActiveQuery(Category::class); $category = $categoryQuery->findOne(2); $this->assertInstanceOf(Category::class, $category); $this->assertEquals(3, $category->getItemsQuery()->count()); @@ -1508,14 +1507,14 @@ public function testIssues(): void $this->assertEquals(1, $category->getLimitedItemsQuery()->distinct(true)->count()); /** {@see https://github.com/yiisoft/yii2/issues/3197} */ - $orderQuery = new ActiveQuery(Order::class, $this->db); + $orderQuery = new ActiveQuery(Order::class); $orders = $orderQuery->with('orderItems')->orderBy('id')->all(); $this->assertCount(3, $orders); $this->assertCount(2, $orders[0]->getOrderItems()); $this->assertCount(3, $orders[1]->getOrderItems()); $this->assertCount(1, $orders[2]->getOrderItems()); - $orderQuery = new ActiveQuery(Order::class, $this->db); + $orderQuery = new ActiveQuery(Order::class); $orders = $orderQuery->with( [ 'orderItems' => static function ($q) { @@ -1529,7 +1528,7 @@ public function testIssues(): void $this->assertCount(1, $orders[2]->getOrderItems()); /** {@see https://github.com/yiisoft/yii2/issues/8149} */ - $arClass = new Customer($this->db); + $arClass = new Customer(); $arClass->setName('test'); $arClass->setEmail('test'); @@ -1541,10 +1540,10 @@ public function testIssues(): void public function testPopulateWithoutPk(): void { - $this->checkFixture($this->db, 'customer', true); + $this->checkFixture($this->db(), 'customer', true); /** tests with single pk asArray */ - $customerQuery = new ActiveQuery(Customer::class, $this->db); + $customerQuery = new ActiveQuery(Customer::class); $aggregation = $customerQuery ->select(['{{customer}}.[[status]]', 'SUM({{order}}.[[total]]) AS [[sumtotal]]']) ->joinWith('ordersPlain', false) @@ -1567,7 +1566,7 @@ public function testPopulateWithoutPk(): void $this->assertEquals($expected, $aggregation); // tests with single pk asArray with eager loading - $customerQuery = new ActiveQuery(Customer::class, $this->db); + $customerQuery = new ActiveQuery(Customer::class); $aggregation = $customerQuery ->select(['{{customer}}.[[status]]', 'SUM({{order}}.[[total]]) AS [[sumtotal]]']) ->joinWith('ordersPlain') @@ -1591,7 +1590,7 @@ public function testPopulateWithoutPk(): void $this->assertEquals($expected, $aggregation); /** tests with single pk with Models */ - $customerQuery = new ActiveQuery(Customer::class, $this->db); + $customerQuery = new ActiveQuery(Customer::class); $aggregation = $customerQuery ->select(['{{customer}}.[[status]]', 'SUM({{order}}.[[total]]) AS [[sumTotal]]']) ->joinWith('ordersPlain', false) @@ -1611,7 +1610,7 @@ public function testPopulateWithoutPk(): void } /** tests with composite pk asArray */ - $orderItemQuery = new ActiveQuery(OrderItem::class, $this->db); + $orderItemQuery = new ActiveQuery(OrderItem::class); $aggregation = $orderItemQuery ->select(['[[order_id]]', 'SUM([[subtotal]]) AS [[subtotal]]']) ->joinWith('order', false) @@ -1637,7 +1636,7 @@ public function testPopulateWithoutPk(): void $this->assertEquals($expected, $aggregation); /** tests with composite pk with Models */ - $orderItemQuery = new ActiveQuery(OrderItem::class, $this->db); + $orderItemQuery = new ActiveQuery(OrderItem::class); $aggregation = $orderItemQuery ->select(['[[order_id]]', 'SUM([[subtotal]]) AS [[subtotal]]']) ->joinWith('order', false) @@ -1661,12 +1660,12 @@ public function testPopulateWithoutPk(): void public function testLinkWhenRelationIsIndexed2(): void { - $this->checkFixture($this->db, 'order'); + $this->checkFixture($this->db(), 'order'); - $orderQuery = new ActiveQuery(Order::class, $this->db); + $orderQuery = new ActiveQuery(Order::class); $order = $orderQuery->with('orderItems2')->where(['id' => 1])->onePopulate(); - $orderItem = new OrderItem($this->db); + $orderItem = new OrderItem(); $orderItem->setOrderId($order->getId()); $orderItem->setItemId(3); @@ -1679,9 +1678,9 @@ public function testLinkWhenRelationIsIndexed2(): void public function testEmulateExecution(): void { - $this->checkFixture($this->db, 'order'); + $this->checkFixture($this->db(), 'order'); - $customerQuery = new ActiveQuery(Customer::class, $this->db); + $customerQuery = new ActiveQuery(Customer::class); $this->assertGreaterThan(0, $customerQuery->count()); $this->assertSame([], $customerQuery->emulateExecution()->all()); @@ -1701,14 +1700,14 @@ public function testEmulateExecution(): void */ public function testUnlinkAllOnCondition(): void { - $this->checkFixture($this->db, 'item'); + $this->checkFixture($this->db(), 'item'); /** Ensure there are three items with category_id = 2 in the Items table */ - $itemQuery = new ActiveQuery(Item::class, $this->db); + $itemQuery = new ActiveQuery(Item::class); $itemsCount = $itemQuery->where(['category_id' => 2])->count(); $this->assertEquals(3, $itemsCount); - $categoryQuery = new ActiveQuery(Category::class, $this->db); + $categoryQuery = new ActiveQuery(Category::class); $categoryQuery = $categoryQuery->with('limitedItems')->where(['id' => 2]); /** @@ -1733,14 +1732,14 @@ public function testUnlinkAllOnCondition(): void */ public function testUnlinkAllOnConditionViaTable(): void { - $this->checkFixture($this->db, 'item', true); + $this->checkFixture($this->db(), 'item', true); /** Ensure there are three items with category_id = 2 in the Items table */ - $itemQuery = new ActiveQuery(Item::class, $this->db); + $itemQuery = new ActiveQuery(Item::class); $itemsCount = $itemQuery->where(['category_id' => 2])->count(); $this->assertEquals(3, $itemsCount); - $orderQuery = new ActiveQuery(Order::class, $this->db); + $orderQuery = new ActiveQuery(Order::class); $orderQuery = $orderQuery->with('limitedItems')->where(['id' => 2]); /** @@ -1764,9 +1763,9 @@ public function testUnlinkAllOnConditionViaTable(): void */ public function testIndexByAfterLoadingRelations(): void { - $this->checkFixture($this->db, 'order'); + $this->checkFixture($this->db(), 'order'); - $orderQuery = new ActiveQuery(Order::class, $this->db); + $orderQuery = new ActiveQuery(Order::class); $orderQuery->with('customer')->indexBy(function (Order $order) { $this->assertTrue($order->isRelationPopulated('customer')); $this->assertNotEmpty($order->getCustomer()?->getId()); @@ -1802,22 +1801,22 @@ public static function filterTableNamesFromAliasesProvider(): array */ public function testFilterTableNamesFromAliases(array|string $fromParams, array $expectedAliases): void { - $this->checkFixture($this->db, 'customer'); + $this->checkFixture($this->db(), 'customer'); - $customerQuery = new ActiveQuery(Customer::class, $this->db); + $customerQuery = new ActiveQuery(Customer::class); $query = $customerQuery->from($fromParams); - $aliases = Assert::invokeMethod(new Customer($this->db), 'filterValidAliases', [$query]); + $aliases = Assert::invokeMethod(new Customer(), 'filterValidAliases', [$query]); $this->assertEquals($expectedAliases, $aliases); } public function testExtraFields(): void { - $this->checkFixture($this->db, 'customer'); + $this->checkFixture($this->db(), 'customer'); - $customerQuery = new ActiveQuery(Customer::class, $this->db); + $customerQuery = new ActiveQuery(Customer::class); $query = $customerQuery->with('orders2')->where(['id' => 1])->onePopulate(); $this->assertCount(1, $query->getRelatedRecords()); @@ -1847,12 +1846,12 @@ public static function tableNameProvider(): array */ public function testRelationWhereParams(string $orderTableName, string $orderItemTableName): void { - $driverName = $this->db->getDriverName(); + $driverName = $this->db()->getDriverName(); - $this->checkFixture($this->db, 'order'); + $this->checkFixture($this->db(), 'order'); - $order = new Order($this->db); - $orderItem = new OrderItem($this->db); + $order = new Order(); + $orderItem = new OrderItem(); $this->assertSame('order', $order->getTableName()); $this->assertSame('order_item', $orderItem->getTableName()); @@ -1863,7 +1862,7 @@ public function testRelationWhereParams(string $orderTableName, string $orderIte $this->assertSame($orderTableName, $order->getTableName()); $this->assertSame($orderItemTableName, $orderItem->getTableName()); - $orderQuery = new ActiveQuery(Order::class, $this->db); + $orderQuery = new ActiveQuery(Order::class); $order = $orderQuery->findOne(1); $itemsSQL = $order->getOrderItemsQuery()->createCommand()->getRawSql(); $expectedSQL = DbHelper::replaceQuotes( @@ -1887,9 +1886,9 @@ public function testRelationWhereParams(string $orderTableName, string $orderIte public function testOutdatedRelationsAreResetForExistingRecords(): void { - $this->checkFixture($this->db, 'order_item', true); + $this->checkFixture($this->db(), 'order_item', true); - $orderItemQuery = new ActiveQuery(OrderItem::class, $this->db); + $orderItemQuery = new ActiveQuery(OrderItem::class); $orderItems = $orderItemQuery->findOne(1); $this->assertEquals(1, $orderItems->getOrder()->getId()); $this->assertEquals(1, $orderItems->getItem()->getId()); @@ -1909,9 +1908,9 @@ public function testOutdatedRelationsAreResetForExistingRecords(): void public function testOutdatedCompositeKeyRelationsAreReset(): void { - $this->checkFixture($this->db, 'dossier'); + $this->checkFixture($this->db(), 'dossier'); - $dossierQuery = new ActiveQuery(Dossier::class, $this->db); + $dossierQuery = new ActiveQuery(Dossier::class); $dossiers = $dossierQuery->findOne(['department_id' => 1, 'employee_id' => 1]); $this->assertEquals('John Doe', $dossiers->getEmployee()->getFullName()); @@ -1926,7 +1925,7 @@ public function testOutdatedCompositeKeyRelationsAreReset(): void // unset($dossiers->employee_id); // $this->assertNull($dossiers->getEmployee()); - $dossier = new Dossier($this->db); + $dossier = new Dossier(); $this->assertNull($dossier->getEmployee()); $dossier->setEmployeeId(1); @@ -1939,9 +1938,9 @@ public function testOutdatedCompositeKeyRelationsAreReset(): void public function testOutdatedViaTableRelationsAreReset(): void { - $this->checkFixture($this->db, 'order', true); + $this->checkFixture($this->db(), 'order', true); - $orderQuery = new ActiveQuery(Order::class, $this->db); + $orderQuery = new ActiveQuery(Order::class); $orders = $orderQuery->findOne(1); $orderItemIds = ArArrayHelper::getColumn($orders->getItems(), 'id'); @@ -1956,7 +1955,7 @@ public function testOutdatedViaTableRelationsAreReset(): void $orders->setId(null); $this->assertSame([], $orders->getItems()); - $order = new Order($this->db); + $order = new Order(); $this->assertSame([], $order->getItems()); $order->setId(3); @@ -1966,9 +1965,9 @@ public function testOutdatedViaTableRelationsAreReset(): void public function testInverseOfDynamic(): void { - $this->checkFixture($this->db, 'customer'); + $this->checkFixture($this->db(), 'customer'); - $customerQuery = new ActiveQuery(Customer::class, $this->db); + $customerQuery = new ActiveQuery(Customer::class); $customer = $customerQuery->findOne(1); @@ -1999,9 +1998,9 @@ public function testInverseOfDynamic(): void public function testOptimisticLock(): void { - $this->checkFixture($this->db, 'document'); + $this->checkFixture($this->db(), 'document'); - $documentQuery = new ActiveQuery(Document::class, $this->db); + $documentQuery = new ActiveQuery(Document::class); $record = $documentQuery->findOne(1); $record->content = 'New Content'; @@ -2018,9 +2017,9 @@ public function testOptimisticLock(): void public function testOptimisticLockOnDelete(): void { - $this->checkFixture($this->db, 'document', true); + $this->checkFixture($this->db(), 'document', true); - $documentQuery = new ActiveQuery(Document::class, $this->db); + $documentQuery = new ActiveQuery(Document::class); $document = $documentQuery->findOne(1); $this->assertSame(0, $document->version); @@ -2033,9 +2032,9 @@ public function testOptimisticLockOnDelete(): void public function testOptimisticLockAfterDelete(): void { - $this->checkFixture($this->db, 'document', true); + $this->checkFixture($this->db(), 'document', true); - $documentQuery = new ActiveQuery(Document::class, $this->db); + $documentQuery = new ActiveQuery(Document::class); $document = $documentQuery->findOne(1); $this->assertSame(0, $document->version); @@ -2051,22 +2050,22 @@ public function testOptimisticLockAfterDelete(): void */ public function testBit(): void { - $this->checkFixture($this->db, 'bit_values'); + $this->checkFixture($this->db(), 'bit_values'); - $bitValueQuery = new ActiveQuery(BitValues::class, $this->db); + $bitValueQuery = new ActiveQuery(BitValues::class); $falseBit = $bitValueQuery->findOne(1); $this->assertEquals(false, $falseBit->val); - $bitValueQuery = new ActiveQuery(BitValues::class, $this->db); + $bitValueQuery = new ActiveQuery(BitValues::class); $trueBit = $bitValueQuery->findOne(2); $this->assertEquals(true, $trueBit->val); } public function testUpdateAttributes(): void { - $this->checkFixture($this->db, 'order'); + $this->checkFixture($this->db(), 'order'); - $orderQuery = new ActiveQuery(Order::class, $this->db); + $orderQuery = new ActiveQuery(Order::class); $order = $orderQuery->findOne(1); $newTotal = 978; $this->assertSame(1, $order->updateAttributes(['total' => $newTotal])); @@ -2076,7 +2075,7 @@ public function testUpdateAttributes(): void $this->assertEquals($newTotal, $order->getTotal()); /** @see https://github.com/yiisoft/yii2/issues/12143 */ - $newOrder = new Order($this->db); + $newOrder = new Order(); $this->assertTrue($newOrder->getIsNewRecord()); $newTotal = 200; @@ -2092,9 +2091,9 @@ public function testUpdateAttributes(): void */ public function testAmbiguousColumnFindOne(): void { - $this->checkFixture($this->db, 'customer'); + $this->checkFixture($this->db(), 'customer'); - $customerQuery = new CustomerQuery(Customer::class, $this->db); + $customerQuery = new CustomerQuery(Customer::class); $customerQuery->joinWithProfile = true; @@ -2107,9 +2106,9 @@ public function testAmbiguousColumnFindOne(): void public function testCustomARRelation(): void { - $this->checkFixture($this->db, 'order_item'); + $this->checkFixture($this->db(), 'order_item'); - $orderItem = new ActiveQuery(OrderItem::class, $this->db); + $orderItem = new ActiveQuery(OrderItem::class); $orderItem = $orderItem->findOne(1); @@ -2119,7 +2118,7 @@ public function testCustomARRelation(): void public function testGetAttributes(): void { $attributesExpected = []; - $this->checkFixture($this->db, 'customer'); + $this->checkFixture($this->db(), 'customer'); $attributesExpected['id'] = 1; $attributesExpected['email'] = 'user1@example.com'; @@ -2129,7 +2128,7 @@ public function testGetAttributes(): void $attributesExpected['bool_status'] = true; $attributesExpected['profile_id'] = 1; - $customer = new ActiveQuery(Customer::class, $this->db); + $customer = new ActiveQuery(Customer::class); $attributes = $customer->findOne(1)->getAttributes(); @@ -2138,9 +2137,9 @@ public function testGetAttributes(): void public function testGetAttributesOnly(): void { - $this->checkFixture($this->db, 'customer'); + $this->checkFixture($this->db(), 'customer'); - $customer = new ActiveQuery(Customer::class, $this->db); + $customer = new ActiveQuery(Customer::class); $attributes = $customer->findOne(1)->getAttributes(['id', 'email', 'name']); @@ -2149,9 +2148,9 @@ public function testGetAttributesOnly(): void public function testGetAttributesExcept(): void { - $this->checkFixture($this->db, 'customer'); + $this->checkFixture($this->db(), 'customer'); - $customer = new ActiveQuery(Customer::class, $this->db); + $customer = new ActiveQuery(Customer::class); $attributes = $customer->findOne(1)->getAttributes(null, ['status', 'bool_status', 'profile_id']); @@ -2163,9 +2162,9 @@ public function testGetAttributesExcept(): void public function testFields(): void { - $this->checkFixture($this->db, 'order_item'); + $this->checkFixture($this->db(), 'order_item'); - $orderItem = new ActiveQuery(OrderItem::class, $this->db); + $orderItem = new ActiveQuery(OrderItem::class); $fields = $orderItem->findOne(['order_id' => 1, 'item_id' => 2])->fields(); @@ -2177,9 +2176,9 @@ public function testFields(): void public function testGetOldAttribute(): void { - $this->checkFixture($this->db, 'customer'); + $this->checkFixture($this->db(), 'customer'); - $customer = new ActiveQuery(Customer::class, $this->db); + $customer = new ActiveQuery(Customer::class); $query = $customer->findOne(1); $this->assertEquals('user1', $query->getOldAttribute('name')); @@ -2195,7 +2194,7 @@ public function testGetOldAttributes(): void { $attributes = []; $attributesNew = []; - $this->checkFixture($this->db, 'customer'); + $this->checkFixture($this->db(), 'customer'); $attributes['id'] = 1; $attributes['email'] = 'user1@example.com'; @@ -2205,7 +2204,7 @@ public function testGetOldAttributes(): void $attributes['bool_status'] = true; $attributes['profile_id'] = 1; - $customer = new ActiveQuery(Customer::class, $this->db); + $customer = new ActiveQuery(Customer::class); $query = $customer->findOne(1); $this->assertEquals($query->getAttributes(), $attributes); @@ -2227,9 +2226,9 @@ public function testGetOldAttributes(): void public function testIsAttributeChanged(): void { - $this->checkFixture($this->db, 'customer'); + $this->checkFixture($this->db(), 'customer'); - $customer = new ActiveQuery(Customer::class, $this->db); + $customer = new ActiveQuery(Customer::class); $query = $customer->findOne(1); $this->assertEquals('user1', $query->getAttribute('name')); @@ -2244,9 +2243,9 @@ public function testIsAttributeChanged(): void public function testIsAttributeChangedNotIdentical(): void { - $this->checkFixture($this->db, 'customer'); + $this->checkFixture($this->db(), 'customer'); - $customer = new ActiveQuery(Customer::class, $this->db); + $customer = new ActiveQuery(Customer::class); $query = $customer->findOne(1); $this->assertEquals('user1', $query->getAttribute('name')); @@ -2261,9 +2260,9 @@ public function testIsAttributeChangedNotIdentical(): void public function testOldAttributeAfterInsertAndUpdate(): void { - $this->checkFixture($this->db, 'customer'); + $this->checkFixture($this->db(), 'customer'); - $customer = new Customer($this->db); + $customer = new Customer(); $customer->setAttributes([ 'email' => 'info@example.com', @@ -2286,9 +2285,9 @@ public function testCheckRelationUnknownPropertyException(): void { self::markTestSkipped('There is no check for access to an unknown property.'); - $this->checkFixture($this->db, 'customer'); + $this->checkFixture($this->db(), 'customer'); - $customer = new ActiveQuery(Customer::class, $this->db); + $customer = new ActiveQuery(Customer::class); $query = $customer->findOne(1); @@ -2301,9 +2300,9 @@ public function testCheckRelationInvalidCallException(): void { self::markTestSkipped('There is no check for access to an unknown property.'); - $this->checkFixture($this->db, 'customer'); + $this->checkFixture($this->db(), 'customer'); - $customer = new ActiveQuery(Customer::class, $this->db); + $customer = new ActiveQuery(Customer::class); $query = $customer->findOne(2); @@ -2316,9 +2315,9 @@ public function testCheckRelationInvalidCallException(): void public function testGetRelationInvalidArgumentException(): void { - $this->checkFixture($this->db, 'customer'); + $this->checkFixture($this->db(), 'customer'); - $customer = new ActiveQuery(Customer::class, $this->db); + $customer = new ActiveQuery(Customer::class); $query = $customer->findOne(1); @@ -2334,9 +2333,9 @@ public function testGetRelationInvalidArgumentExceptionHasNoRelationNamed(): voi { self::markTestSkipped('The same as test testGetRelationInvalidArgumentException()'); - $this->checkFixture($this->db, 'customer'); + $this->checkFixture($this->db(), 'customer'); - $customer = new ActiveQuery(Customer::class, $this->db); + $customer = new ActiveQuery(Customer::class); $query = $customer->findOne(1); @@ -2352,9 +2351,9 @@ public function testGetRelationInvalidArgumentExceptionCaseSensitive(): void { self::markTestSkipped('The same as test testGetRelationInvalidArgumentException()'); - $this->checkFixture($this->db, 'customer'); + $this->checkFixture($this->db(), 'customer'); - $customer = new ActiveQuery(Customer::class, $this->db); + $customer = new ActiveQuery(Customer::class); $query = $customer->findOne(1); @@ -2368,9 +2367,9 @@ public function testGetRelationInvalidArgumentExceptionCaseSensitive(): void public function testExists(): void { - $this->checkFixture($this->db, 'customer'); + $this->checkFixture($this->db(), 'customer'); - $customer = new ActiveQuery(Customer::class, $this->db); + $customer = new ActiveQuery(Customer::class); $this->assertTrue($customer->where(['id' => 2])->exists()); $this->assertFalse($customer->where(['id' => 5])->exists()); @@ -2383,33 +2382,33 @@ public function testExists(): void public function testUnlink(): void { - $this->checkFixture($this->db, 'customer'); + $this->checkFixture($this->db(), 'customer'); /** has many without delete */ - $customerQuery = new ActiveQuery(Customer::class, $this->db); + $customerQuery = new ActiveQuery(Customer::class); $customer = $customerQuery->findOne(2); $this->assertCount(2, $customer->getOrdersWithNullFK()); $customer->unlink('ordersWithNullFK', $customer->getOrdersWithNullFK()[1], false); $this->assertCount(1, $customer->getOrdersWithNullFK()); - $orderWithNullFKQuery = new ActiveQuery(OrderWithNullFK::class, $this->db); + $orderWithNullFKQuery = new ActiveQuery(OrderWithNullFK::class); $orderWithNullFK = $orderWithNullFKQuery->findOne(3); $this->assertEquals(3, $orderWithNullFK->getId()); $this->assertNull($orderWithNullFK->getCustomerId()); /** has many with delete */ - $customerQuery = new ActiveQuery(Customer::class, $this->db); + $customerQuery = new ActiveQuery(Customer::class); $customer = $customerQuery->findOne(2); $this->assertCount(2, $customer->getOrders()); $customer->unlink('orders', $customer->getOrders()[1], true); $this->assertCount(1, $customer->getOrders()); - $orderQuery = new ActiveQuery(Order::class, $this->db); + $orderQuery = new ActiveQuery(Order::class); $this->assertNull($orderQuery->findOne(3)); /** via model with delete */ - $orderQuery = new ActiveQuery(Order::class, $this->db); + $orderQuery = new ActiveQuery(Order::class); $order = $orderQuery->findOne(2); $this->assertCount(3, $order->getItems()); $this->assertCount(3, $order->getOrderItems()); @@ -2427,18 +2426,18 @@ public function testUnlink(): void public function testUnlinkAllAndConditionSetNull(): void { - $this->checkFixture($this->db, 'order_item_with_null_fk'); + $this->checkFixture($this->db(), 'order_item_with_null_fk'); /** in this test all orders are owned by customer 1 */ - $orderWithNullFKInstance = new OrderWithNullFK($this->db); + $orderWithNullFKInstance = new OrderWithNullFK(); $orderWithNullFKInstance->updateAll(['customer_id' => 1]); - $customerQuery = new ActiveQuery(Customer::class, $this->db); + $customerQuery = new ActiveQuery(Customer::class); $customer = $customerQuery->findOne(1); $this->assertCount(3, $customer->getOrdersWithNullFK()); $this->assertCount(1, $customer->getExpensiveOrdersWithNullFK()); - $orderWithNullFKQuery = new ActiveQuery(OrderWithNullFK::class, $this->db); + $orderWithNullFKQuery = new ActiveQuery(OrderWithNullFK::class); $this->assertEquals(3, $orderWithNullFKQuery->count()); $customer->unlinkAll('expensiveOrdersWithNullFK'); @@ -2453,18 +2452,18 @@ public function testUnlinkAllAndConditionSetNull(): void public function testUnlinkAllAndConditionDelete(): void { - $this->checkFixture($this->db, 'customer', true); + $this->checkFixture($this->db(), 'customer', true); /** in this test all orders are owned by customer 1 */ - $orderInstance = new Order($this->db); + $orderInstance = new Order(); $orderInstance->updateAll(['customer_id' => 1]); - $customerQuery = new ActiveQuery(Customer::class, $this->db); + $customerQuery = new ActiveQuery(Customer::class); $customer = $customerQuery->findOne(1); $this->assertCount(3, $customer->getOrders()); $this->assertCount(1, $customer->getExpensiveOrders()); - $orderQuery = new ActiveQuery(Order::class, $this->db); + $orderQuery = new ActiveQuery(Order::class); $this->assertEquals(3, $orderQuery->count()); $customer->unlinkAll('expensiveOrders', true); @@ -2479,9 +2478,9 @@ public function testUnlinkAllAndConditionDelete(): void public function testUpdate(): void { - $this->checkFixture($this->db, 'customer'); + $this->checkFixture($this->db(), 'customer'); - $customerQuery = new ActiveQuery(Customer::class, $this->db); + $customerQuery = new ActiveQuery(Customer::class); $customer = $customerQuery->findOne(2); $this->assertInstanceOf(Customer::class, $customer); $this->assertEquals('user2', $customer->getAttribute('name')); @@ -2497,14 +2496,14 @@ public function testUpdate(): void $this->assertEquals('user2x', $customer2->getAttribute('name')); /** no update */ - $customerQuery = new ActiveQuery(Customer::class, $this->db); + $customerQuery = new ActiveQuery(Customer::class); $customer = $customerQuery->findOne(1); $customer->setAttribute('name', 'user1'); $this->assertEquals(0, $customer->update()); /** updateAll */ - $customerQuery = new ActiveQuery(Customer::class, $this->db); + $customerQuery = new ActiveQuery(Customer::class); $customer = $customerQuery->findOne(3); $this->assertEquals('user3', $customer->getAttribute('name')); @@ -2523,11 +2522,11 @@ public function testUpdate(): void public function testUpdateCounters(): void { - $this->checkFixture($this->db, 'order_item', true); + $this->checkFixture($this->db(), 'order_item', true); /** updateCounters */ $pk = ['order_id' => 2, 'item_id' => 4]; - $orderItemQuery = new ActiveQuery(OrderItem::class, $this->db); + $orderItemQuery = new ActiveQuery(OrderItem::class); $orderItem = $orderItemQuery->findOne($pk); $this->assertEquals(1, $orderItem->getQuantity()); @@ -2540,11 +2539,11 @@ public function testUpdateCounters(): void /** updateAllCounters */ $pk = ['order_id' => 1, 'item_id' => 2]; - $orderItemQuery = new ActiveQuery(OrderItem::class, $this->db); + $orderItemQuery = new ActiveQuery(OrderItem::class); $orderItem = $orderItemQuery->findOne($pk); $this->assertEquals(2, $orderItem->getQuantity()); - $orderItem = new OrderItem($this->db); + $orderItem = new OrderItem(); $ret = $orderItem->updateAllCounters(['quantity' => 3, 'subtotal' => -10], $pk); $this->assertEquals(1, $ret); @@ -2555,10 +2554,10 @@ public function testUpdateCounters(): void public function testDelete(): void { - $this->checkFixture($this->db, 'customer', true); + $this->checkFixture($this->db(), 'customer', true); /** delete */ - $customerQuery = new ActiveQuery(Customer::class, $this->db); + $customerQuery = new ActiveQuery(Customer::class); $customer = $customerQuery->findOne(2); $this->assertInstanceOf(Customer::class, $customer); $this->assertEquals('user2', $customer->getName()); @@ -2569,11 +2568,11 @@ public function testDelete(): void $this->assertNull($customer); /** deleteAll */ - $customerQuery = new ActiveQuery(Customer::class, $this->db); + $customerQuery = new ActiveQuery(Customer::class); $customers = $customerQuery->all(); $this->assertCount(2, $customers); - $customer = new Customer($this->db); + $customer = new Customer(); $ret = $customer->deleteAll(); $this->assertEquals(2, $ret); @@ -2589,9 +2588,9 @@ public function testDelete(): void */ public function testViaWithCallable(): void { - $this->checkFixture($this->db, 'order', true); + $this->checkFixture($this->db(), 'order', true); - $orderQuery = new ActiveQuery(Order::class, $this->db); + $orderQuery = new ActiveQuery(Order::class); $order = $orderQuery->findOne(2); @@ -2607,27 +2606,27 @@ public function testViaWithCallable(): void public function testLink(): void { - $this->checkFixture($this->db, 'customer', true); + $this->checkFixture($this->db(), 'customer', true); - $customerQuery = new ActiveQuery(Customer::class, $this->db); + $customerQuery = new ActiveQuery(Customer::class); $customer = $customerQuery->findOne(2); $this->assertCount(2, $customer->getOrders()); /** has many */ - $order = new Order($this->db); + $order = new Order(); $order->setTotal(100); $order->setCreatedAt(time()); $this->assertTrue($order->getIsNewRecord()); /** belongs to */ - $order = new Order($this->db); + $order = new Order(); $order->setTotal(100); $order->setCreatedAt(time()); $this->assertTrue($order->getIsNewRecord()); - $customerQuery = new ActiveQuery(Customer::class, $this->db); + $customerQuery = new ActiveQuery(Customer::class); $customer = $customerQuery->findOne(1); $this->assertNull($order->getCustomer()); @@ -2637,22 +2636,22 @@ public function testLink(): void $this->assertEquals(1, $order->getCustomer()->getPrimaryKey()); /** via model */ - $orderQuery = new ActiveQuery(Order::class, $this->db); + $orderQuery = new ActiveQuery(Order::class); $order = $orderQuery->findOne(1); $this->assertCount(2, $order->getItems()); $this->assertCount(2, $order->getOrderItems()); - $orderItemQuery = new ActiveQuery(OrderItem::class, $this->db); + $orderItemQuery = new ActiveQuery(OrderItem::class); $orderItem = $orderItemQuery->findOne(['order_id' => 1, 'item_id' => 3]); $this->assertNull($orderItem); - $itemQuery = new ActiveQuery(Item::class, $this->db); + $itemQuery = new ActiveQuery(Item::class); $item = $itemQuery->findOne(3); $order->link('items', $item, ['quantity' => 10, 'subtotal' => 100]); $this->assertCount(3, $order->getItems()); $this->assertCount(3, $order->getOrderItems()); - $orderItemQuery = new ActiveQuery(OrderItem::class, $this->db); + $orderItemQuery = new ActiveQuery(OrderItem::class); $orderItem = $orderItemQuery->findOne(['order_id' => 1, 'item_id' => 3]); $this->assertInstanceOf(OrderItem::class, $orderItem); $this->assertEquals(10, $orderItem->getQuantity()); @@ -2661,23 +2660,23 @@ public function testLink(): void public function testEqual(): void { - $this->checkFixture($this->db, 'customer'); + $this->checkFixture($this->db(), 'customer'); - $customerA = (new ActiveQuery(Customer::class, $this->db))->findOne(1); - $customerB = (new ActiveQuery(Customer::class, $this->db))->findOne(2); + $customerA = (new ActiveQuery(Customer::class))->findOne(1); + $customerB = (new ActiveQuery(Customer::class))->findOne(2); $this->assertFalse($customerA->equals($customerB)); - $customerB = (new ActiveQuery(Customer::class, $this->db))->findOne(1); + $customerB = (new ActiveQuery(Customer::class))->findOne(1); $this->assertTrue($customerA->equals($customerB)); - $customerA = (new ActiveQuery(Customer::class, $this->db))->findOne(1); - $customerB = (new ActiveQuery(Item::class, $this->db))->findOne(1); + $customerA = (new ActiveQuery(Customer::class))->findOne(1); + $customerB = (new ActiveQuery(Item::class))->findOne(1); $this->assertFalse($customerA->equals($customerB)); } public function testARClassAsString(): void { - $query = new ActiveQuery(Customer::class, $this->db); + $query = new ActiveQuery(Customer::class); $this->assertSame($query->getARClass(), Customer::class); $this->assertSame($query->getARClassName(), Customer::class); @@ -2686,8 +2685,8 @@ public function testARClassAsString(): void public function testARClassAsInstance(): void { - $customer = new Customer($this->db); - $query = new ActiveQuery($customer, $this->db); + $customer = new Customer(); + $query = new ActiveQuery($customer); $this->assertSame($query->getARClass(), $customer); $this->assertSame($query->getARClassName(), Customer::class); @@ -2696,8 +2695,8 @@ public function testARClassAsInstance(): void public function testARClassAsClosure(): void { - $closure = fn (ConnectionInterface $db): Customer => new Customer($db); - $query = new ActiveQuery($closure, $this->db); + $closure = fn (): Customer => new Customer(); + $query = new ActiveQuery($closure); $this->assertSame($query->getARClass(), $closure); $this->assertSame($query->getARClassName(), Customer::class); diff --git a/tests/ActiveRecordFactoryTest.php b/tests/ActiveRecordFactoryTest.php deleted file mode 100644 index 11399df8b..000000000 --- a/tests/ActiveRecordFactoryTest.php +++ /dev/null @@ -1,98 +0,0 @@ -assertInstanceOf(Customer::class, $this->arFactory->createAR(Customer::class)); - } - - public function testCreateARWithConnection(): void - { - $customerAR = $this->arFactory->createAR(arClass: Customer::class, db: $this->db); - $db = Assert::invokeMethod($customerAR, 'db'); - - $this->assertSame($this->db, $db); - $this->assertSame('customer', $customerAR->getTableName()); - $this->assertInstanceOf(Customer::class, $customerAR); - } - - public function testCreateARWithTableName(): void - { - $model = $this->arFactory->createAR(ArrayAndJsonTypes::class); - - $this->assertSame('{{%array_and_json_types}}', $model->getTableName()); - $this->assertInstanceOf(ArrayAndJsonTypes::class, $model); - - $model = $model->withTableName('array_and_json_types'); - - $this->assertSame('array_and_json_types', $model->getTableName()); - $this->assertInstanceOf(ArrayAndJsonTypes::class, $model); - } - - public function testCreateQueryTo(): void - { - /** example create active query */ - $customerQuery = $this->arFactory->createQueryTo(Customer::class); - - $this->assertInstanceOf(ActiveQuery::class, $customerQuery); - - /** example create active query custom */ - $customerQuery = $this->arFactory->createQueryTo(arClass: Customer::class, queryClass: CustomerQuery::class); - - $this->assertInstanceOf(CustomerQuery::class, $customerQuery); - } - - public function testCreateQueryToWithConnection(): void - { - /** example create active query */ - $customerQuery = $this->arFactory->createQueryTo( - arClass: Customer::class, - queryClass: CustomerQuery::class, - db: $this->db, - ); - $db = Assert::inaccessibleProperty($customerQuery, 'db'); - - $this->assertSame($this->db, $db); - $this->assertInstanceOf(ActiveQuery::class, $customerQuery); - } - - public function testCreateQueryToWithTableName(): void - { - /** example create active query */ - $modelQuery = $this->arFactory->createQueryTo( - arClass: fn (): ActiveRecordInterface => $this->arFactory - ->createAR(ArrayAndJsonTypes::class) - ->withTableName('array_and_json_types'), - ); - $tableName = $modelQuery->getARInstance()->getTableName(); - - $this->assertSame('array_and_json_types', $tableName); - $this->assertInstanceOf(ActiveQuery::class, $modelQuery); - } - - public function testGetArInstanceWithConstructor(): void - { - $this->checkFixture($this->db, 'customer', true); - - $query = $this->arFactory->createQueryTo(CustomerWithConstructor::class); - $customer = $query->onePopulate(); - - $this->assertNotNull($customer->getProfile()); - } -} diff --git a/tests/ActiveRecordTest.php b/tests/ActiveRecordTest.php index 8447e77d4..54126b82a 100644 --- a/tests/ActiveRecordTest.php +++ b/tests/ActiveRecordTest.php @@ -7,12 +7,14 @@ use DivisionByZeroError; use ReflectionException; use Yiisoft\ActiveRecord\ActiveQuery; +use Yiisoft\ActiveRecord\ConnectionProvider; use Yiisoft\ActiveRecord\Tests\Stubs\ActiveRecord\Animal; use Yiisoft\ActiveRecord\Tests\Stubs\ActiveRecord\Cat; use Yiisoft\ActiveRecord\Tests\Stubs\ActiveRecord\Customer; use Yiisoft\ActiveRecord\Tests\Stubs\ActiveRecord\CustomerClosureField; use Yiisoft\ActiveRecord\Tests\Stubs\ActiveRecord\CustomerForArrayable; use Yiisoft\ActiveRecord\Tests\Stubs\ActiveRecord\CustomerWithAlias; +use Yiisoft\ActiveRecord\Tests\Stubs\ActiveRecord\CustomerWithCustomConnection; use Yiisoft\ActiveRecord\Tests\Stubs\ActiveRecord\Dog; use Yiisoft\ActiveRecord\Tests\Stubs\ActiveRecord\Item; use Yiisoft\ActiveRecord\Tests\Stubs\ActiveRecord\NoExist; @@ -33,9 +35,9 @@ abstract class ActiveRecordTest extends TestCase { public function testStoreNull(): void { - $this->checkFixture($this->db, 'null_values', true); + $this->checkFixture($this->db(), 'null_values', true); - $record = new NullValues($this->db); + $record = new NullValues(); $this->assertNull($record->getAttribute('var1')); $this->assertNull($record->getAttribute('var2')); @@ -81,9 +83,9 @@ public function testStoreNull(): void public function testStoreEmpty(): void { - $this->checkFixture($this->db, 'null_values'); + $this->checkFixture($this->db(), 'null_values'); - $record = new NullValues($this->db); + $record = new NullValues(); /** this is to simulate empty html form submission */ $record->var1 = ''; @@ -101,10 +103,10 @@ public function testStoreEmpty(): void public function testIsPrimaryKey(): void { - $this->checkFixture($this->db, 'customer'); + $this->checkFixture($this->db(), 'customer'); - $customer = new Customer($this->db); - $orderItem = new OrderItem($this->db); + $customer = new Customer(); + $orderItem = new OrderItem(); $this->assertTrue($customer->isPrimaryKey(['id'])); $this->assertFalse($customer->isPrimaryKey([])); @@ -123,9 +125,9 @@ public function testIsPrimaryKey(): void public function testOutdatedRelationsAreResetForNewRecords(): void { - $this->checkFixture($this->db, 'order_item'); + $this->checkFixture($this->db(), 'order_item'); - $orderItem = new OrderItem($this->db); + $orderItem = new OrderItem(); $orderItem->setOrderId(1); $orderItem->setItemId(3); @@ -146,9 +148,9 @@ public function testOutdatedRelationsAreResetForNewRecords(): void public function testDefaultValues(): void { - $this->checkFixture($this->db, 'type'); + $this->checkFixture($this->db(), 'type'); - $arClass = new Type($this->db); + $arClass = new Type(); $arClass->loadDefaultValues(); @@ -159,13 +161,13 @@ public function testDefaultValues(): void $this->assertEquals(true, $arClass->bool_col2); $this->assertEquals('2002-01-01 00:00:00', $arClass->time); - $arClass = new Type($this->db); + $arClass = new Type(); $arClass->char_col2 = 'not something'; $arClass->loadDefaultValues(); $this->assertEquals('not something', $arClass->char_col2); - $arClass = new Type($this->db); + $arClass = new Type(); $arClass->char_col2 = 'not something'; $arClass->loadDefaultValues(false); @@ -174,9 +176,9 @@ public function testDefaultValues(): void public function testCastValues(): void { - $this->checkFixture($this->db, 'type'); + $this->checkFixture($this->db(), 'type'); - $arClass = new Type($this->db); + $arClass = new Type(); $arClass->int_col = 123; $arClass->int_col2 = 456; @@ -192,7 +194,7 @@ public function testCastValues(): void $arClass->save(); /** @var $model Type */ - $aqClass = new ActiveQuery(Type::class, $this->db); + $aqClass = new ActiveQuery(Type::class); $query = $aqClass->onePopulate(); $this->assertSame(123, $query->int_col); @@ -205,15 +207,15 @@ public function testCastValues(): void public function testPopulateRecordCallWhenQueryingOnParentClass(): void { - $this->checkFixture($this->db, 'cat'); + $this->checkFixture($this->db(), 'cat'); - $cat = new Cat($this->db); + $cat = new Cat(); $cat->save(); - $dog = new Dog($this->db); + $dog = new Dog(); $dog->save(); - $animal = new ActiveQuery(Animal::class, $this->db); + $animal = new ActiveQuery(Animal::class); $animals = $animal->where(['type' => Dog::class])->onePopulate(); $this->assertEquals('bark', $animals->getDoes()); @@ -224,9 +226,9 @@ public function testPopulateRecordCallWhenQueryingOnParentClass(): void public function testSaveEmpty(): void { - $this->checkFixture($this->db, 'null_values', true); + $this->checkFixture($this->db(), 'null_values', true); - $record = new NullValues($this->db); + $record = new NullValues(); $this->assertTrue($record->save()); $this->assertEquals(1, $record->id); @@ -237,9 +239,9 @@ public function testSaveEmpty(): void */ public function testNoTablenameReplacement(): void { - $this->checkFixture($this->db, 'customer'); + $this->checkFixture($this->db(), 'customer'); - $customer = new Customer($this->db); + $customer = new Customer(); $customer->setName('Some {{weird}} name'); $customer->setEmail('test@example.com'); @@ -286,9 +288,9 @@ public function testLegalValuesForFindByCondition( array $validFilter, ?string $alias = null ): void { - $this->checkFixture($this->db, 'customer'); + $this->checkFixture($this->db(), 'customer'); - $activeQuery = new ActiveQuery($modelClassName, $this->db); + $activeQuery = new ActiveQuery($modelClassName); if ($alias !== null) { $activeQuery->alias('csr'); @@ -298,7 +300,7 @@ public function testLegalValuesForFindByCondition( $query = Assert::invokeMethod($activeQuery, 'findByCondition', [$validFilter]); - $this->db->getQueryBuilder()->build($query); + $this->db()->getQueryBuilder()->build($query); $this->assertTrue(true); } @@ -339,25 +341,25 @@ public static function illegalValuesForFindByCondition(): array */ public function testValueEscapingInFindByCondition(string $modelClassName, array $filterWithInjection): void { - $this->checkFixture($this->db, 'customer'); + $this->checkFixture($this->db(), 'customer'); $this->expectException(InvalidArgumentException::class); $this->expectExceptionMessageMatches( '/^Key "(.+)?" is not a column name and can not be used as a filter$/' ); - $query = new ActiveQuery($modelClassName, $this->db); + $query = new ActiveQuery($modelClassName); /** @var Query $query */ $query = Assert::invokeMethod($query, 'findByCondition', $filterWithInjection); - $this->db->getQueryBuilder()->build($query); + $this->db()->getQueryBuilder()->build($query); } public function testRefreshQuerySetAliasFindRecord(): void { - $this->checkFixture($this->db, 'customer'); + $this->checkFixture($this->db(), 'customer'); - $customer = new CustomerWithAlias($this->db); + $customer = new CustomerWithAlias(); $customer->id = 1; $customer->refresh(); @@ -367,15 +369,15 @@ public function testRefreshQuerySetAliasFindRecord(): void public function testResetNotSavedRelation(): void { - $this->checkFixture($this->db, 'order'); + $this->checkFixture($this->db(), 'order'); - $order = new Order($this->db); + $order = new Order(); $order->setCustomerId(1); $order->setCreatedAt(1_325_502_201); $order->setTotal(0); - $orderItem = new OrderItem($this->db); + $orderItem = new OrderItem(); $order->getOrderItems(); @@ -390,9 +392,9 @@ public function testIssetException(): void { self::markTestSkipped('There are no magic properties in the Cat class'); - $this->checkFixture($this->db, 'cat'); + $this->checkFixture($this->db(), 'cat'); - $cat = new Cat($this->db); + $cat = new Cat(); $this->expectException(Exception::class); isset($cat->exception); @@ -402,9 +404,9 @@ public function testIssetThrowable(): void { self::markTestSkipped('There are no magic properties in the Cat class'); - $this->checkFixture($this->db, 'cat'); + $this->checkFixture($this->db(), 'cat'); - $cat = new Cat($this->db); + $cat = new Cat(); $this->expectException(DivisionByZeroError::class); isset($cat->throwable); @@ -414,9 +416,9 @@ public function testIssetNonExisting(): void { self::markTestSkipped('There are no magic properties in the Cat class'); - $this->checkFixture($this->db, 'cat'); + $this->checkFixture($this->db(), 'cat'); - $cat = new Cat($this->db); + $cat = new Cat(); $this->assertFalse(isset($cat->non_existing)); $this->assertFalse(isset($cat->non_existing_property)); @@ -425,20 +427,20 @@ public function testIssetNonExisting(): void public function testSetAttributes(): void { $attributes = []; - $this->checkFixture($this->db, 'customer'); + $this->checkFixture($this->db(), 'customer'); $attributes['email'] = 'samdark@mail.ru'; $attributes['name'] = 'samdark'; $attributes['address'] = 'rusia'; $attributes['status'] = 1; - if ($this->db->getDriverName() === 'pgsql') { + if ($this->db()->getDriverName() === 'pgsql') { $attributes['bool_status'] = true; } $attributes['profile_id'] = null; - $customer = new Customer($this->db); + $customer = new Customer(); $customer->setAttributes($attributes); @@ -449,9 +451,9 @@ public function testSetAttributeNoExist(): void { self::markTestSkipped('There are no magic properties in the Cat class'); - $this->checkFixture($this->db, 'cat'); + $this->checkFixture($this->db(), 'cat'); - $cat = new Cat($this->db); + $cat = new Cat(); $this->expectException(InvalidArgumentException::class); $this->expectExceptionMessage( @@ -463,9 +465,9 @@ public function testSetAttributeNoExist(): void public function testSetOldAttribute(): void { - $this->checkFixture($this->db, 'customer'); + $this->checkFixture($this->db(), 'customer'); - $customer = new Customer($this->db); + $customer = new Customer(); $this->assertEmpty($customer->getOldAttribute('name')); @@ -476,9 +478,9 @@ public function testSetOldAttribute(): void public function testSetOldAttributeException(): void { - $this->checkFixture($this->db, 'customer'); + $this->checkFixture($this->db(), 'customer'); - $customer = new Customer($this->db); + $customer = new Customer(); $this->assertEmpty($customer->getOldAttribute('name')); @@ -491,9 +493,9 @@ public function testSetOldAttributeException(): void public function testIsAttributeChangedNotChanged(): void { - $this->checkFixture($this->db, 'customer'); + $this->checkFixture($this->db(), 'customer'); - $customer = new Customer($this->db); + $customer = new Customer(); $this->assertEmpty($customer->getAttribute('email')); $this->assertEmpty($customer->getOldAttribute('email')); @@ -502,7 +504,7 @@ public function testIsAttributeChangedNotChanged(): void public function testTableSchemaException(): void { - $noExist = new NoExist($this->db); + $noExist = new NoExist(); $this->expectException(InvalidConfigException::class); $this->expectExceptionMessage('The table does not exist: NoExist'); @@ -511,9 +513,9 @@ public function testTableSchemaException(): void public function testInsert(): void { - $this->checkFixture($this->db, 'customer'); + $this->checkFixture($this->db(), 'customer'); - $customer = new Customer($this->db); + $customer = new Customer(); $customer->setEmail('user4@example.com'); $customer->setName('user4'); @@ -535,9 +537,9 @@ public function testInsert(): void */ public function testBooleanAttribute(): void { - $this->checkFixture($this->db, 'customer', true); + $this->checkFixture($this->db(), 'customer', true); - $customer = new Customer($this->db); + $customer = new Customer(); $customer->setName('boolean customer'); $customer->setEmail('mail@example.com'); @@ -553,11 +555,11 @@ public function testBooleanAttribute(): void $customer->refresh(); $this->assertEquals(0, $customer->getStatus()); - $customerQuery = new ActiveQuery(Customer::class, $this->db); + $customerQuery = new ActiveQuery(Customer::class); $customers = $customerQuery->where(['status' => 1])->all(); $this->assertCount(2, $customers); - $customerQuery = new ActiveQuery(Customer::class, $this->db); + $customerQuery = new ActiveQuery(Customer::class); $customers = $customerQuery->where(['status' => 0])->all(); $this->assertCount(1, $customers); } @@ -566,9 +568,9 @@ public function testAttributeAccess(): void { self::markTestSkipped('There are no magic properties in the Cat class'); - $this->checkFixture($this->db, 'customer'); + $this->checkFixture($this->db(), 'customer'); - $arClass = new Customer($this->db); + $arClass = new Customer(); $this->assertTrue($arClass->canSetProperty('name')); $this->assertTrue($arClass->canGetProperty('name')); @@ -582,10 +584,10 @@ public function testAttributeAccess(): void $this->assertNull($arClass->name); /** {@see https://github.com/yiisoft/yii2-gii/issues/190} */ - $baseModel = new Customer($this->db); + $baseModel = new Customer(); $this->assertFalse($baseModel->hasProperty('unExistingColumn')); - $customer = new Customer($this->db); + $customer = new Customer(); $this->assertInstanceOf(Customer::class, $customer); $this->assertTrue($customer->canGetProperty('id')); $this->assertTrue($customer->canSetProperty('id')); @@ -608,7 +610,7 @@ public function testAttributeAccess(): void $this->expectException(InvalidCallException::class); $this->expectExceptionMessage('Setting read-only property: ' . Customer::class . '::orderItems'); - $customer->orderItems = [new Item($this->db)]; + $customer->orderItems = [new Item()]; /** related attribute $customer->orderItems didn't change cause it's read-only */ $this->assertSame([], $customer->orderItems); @@ -622,15 +624,15 @@ public function testAttributeAccess(): void public function testHasAttribute(): void { - $this->checkFixture($this->db, 'customer'); + $this->checkFixture($this->db(), 'customer'); - $customer = new Customer($this->db); + $customer = new Customer(); $this->assertTrue($customer->hasAttribute('id')); $this->assertTrue($customer->hasAttribute('email')); $this->assertFalse($customer->hasAttribute('notExist')); - $customerQuery = new ActiveQuery(Customer::class, $this->db); + $customerQuery = new ActiveQuery(Customer::class); $customer = $customerQuery->findOne(1); $this->assertTrue($customer->hasAttribute('id')); $this->assertTrue($customer->hasAttribute('email')); @@ -639,13 +641,13 @@ public function testHasAttribute(): void public function testRefresh(): void { - $this->checkFixture($this->db, 'customer'); + $this->checkFixture($this->db(), 'customer'); - $customer = new Customer($this->db); + $customer = new Customer(); $this->assertFalse($customer->refresh()); - $customerQuery = new ActiveQuery(Customer::class, $this->db); + $customerQuery = new ActiveQuery(Customer::class); $customer = $customerQuery->findOne(1); $customer->setName('to be refreshed'); @@ -655,14 +657,14 @@ public function testRefresh(): void public function testEquals(): void { - $this->checkFixture($this->db, 'customer'); + $this->checkFixture($this->db(), 'customer'); - $customerA = new Customer($this->db); - $customerB = new Customer($this->db); + $customerA = new Customer(); + $customerB = new Customer(); $this->assertFalse($customerA->equals($customerB)); - $customerA = new Customer($this->db); - $customerB = new Item($this->db); + $customerA = new Customer(); + $customerB = new Item(); $this->assertFalse($customerA->equals($customerB)); } @@ -681,10 +683,10 @@ public static function providerForUnlinkDelete() */ public function testUnlinkWithViaOnCondition($delete, $count) { - $this->checkFixture($this->db, 'order', true); - $this->checkFixture($this->db, 'order_item_with_null_fk', true); + $this->checkFixture($this->db(), 'order', true); + $this->checkFixture($this->db(), 'order_item_with_null_fk', true); - $orderQuery = new ActiveQuery(Order::class, $this->db); + $orderQuery = new ActiveQuery(Order::class); $order = $orderQuery->findOne(2); $this->assertCount(1, $order->getItemsFor8()); @@ -694,7 +696,7 @@ public function testUnlinkWithViaOnCondition($delete, $count) $this->assertCount(0, $order->getItemsFor8()); $this->assertCount(2, $order->getOrderItemsWithNullFK()); - $orderItemQuery = new ActiveQuery(OrderItemWithNullFK::class, $this->db); + $orderItemQuery = new ActiveQuery(OrderItemWithNullFK::class); $this->assertCount(1, $orderItemQuery->findAll([ 'order_id' => 2, 'item_id' => 5, @@ -707,9 +709,9 @@ public function testUnlinkWithViaOnCondition($delete, $count) public function testVirtualRelation() { - $this->checkFixture($this->db, 'order', true); + $this->checkFixture($this->db(), 'order', true); - $orderQuery = new ActiveQuery(Order::class, $this->db); + $orderQuery = new ActiveQuery(Order::class); /** @var Order $order */ $order = $orderQuery->findOne(2); @@ -724,16 +726,16 @@ public function testVirtualRelation() */ public function testJoinWithEager() { - $this->checkFixture($this->db, 'customer', true); + $this->checkFixture($this->db(), 'customer', true); - $customerQuery = new ActiveQuery(Customer::class, $this->db); + $customerQuery = new ActiveQuery(Customer::class); $eagerCustomers = $customerQuery->joinWith(['items2'])->all(); $eagerItemsCount = 0; foreach ($eagerCustomers as $customer) { $eagerItemsCount += is_countable($customer->getItems2()) ? count($customer->getItems2()) : 0; } - $customerQuery = new ActiveQuery(Customer::class, $this->db); + $customerQuery = new ActiveQuery(Customer::class); $lazyCustomers = $customerQuery->all(); $lazyItemsCount = 0; foreach ($lazyCustomers as $customer) { @@ -745,9 +747,9 @@ public function testJoinWithEager() public function testToArray(): void { - $this->checkFixture($this->db, 'customer', true); + $this->checkFixture($this->db(), 'customer', true); - $customerQuery = new ActiveQuery(Customer::class, $this->db); + $customerQuery = new ActiveQuery(Customer::class); $customer = $customerQuery->findOne(1); $this->assertSame( @@ -766,9 +768,9 @@ public function testToArray(): void public function testToArrayWithClosure(): void { - $this->checkFixture($this->db, 'customer', true); + $this->checkFixture($this->db(), 'customer', true); - $customerQuery = new ActiveQuery(CustomerClosureField::class, $this->db); + $customerQuery = new ActiveQuery(CustomerClosureField::class); $customer = $customerQuery->findOne(1); $this->assertSame( @@ -787,9 +789,9 @@ public function testToArrayWithClosure(): void public function testToArrayForArrayable(): void { - $this->checkFixture($this->db, 'customer', true); + $this->checkFixture($this->db(), 'customer', true); - $customerQuery = new ActiveQuery(CustomerForArrayable::class, $this->db); + $customerQuery = new ActiveQuery(CustomerForArrayable::class); /** @var CustomerForArrayable $customer */ $customer = $customerQuery->findOne(1); @@ -841,9 +843,9 @@ public function testToArrayForArrayable(): void public function testSaveWithoutChanges(): void { - $this->checkFixture($this->db, 'customer'); + $this->checkFixture($this->db(), 'customer'); - $customerQuery = new ActiveQuery(Customer::class, $this->db); + $customerQuery = new ActiveQuery(Customer::class); $customer = $customerQuery->findOne(1); @@ -852,9 +854,9 @@ public function testSaveWithoutChanges(): void public function testGetPrimaryKey(): void { - $this->checkFixture($this->db, 'customer'); + $this->checkFixture($this->db(), 'customer'); - $customerQuery = new ActiveQuery(Customer::class, $this->db); + $customerQuery = new ActiveQuery(Customer::class); $customer = $customerQuery->findOne(1); @@ -864,9 +866,9 @@ public function testGetPrimaryKey(): void public function testGetOldPrimaryKey(): void { - $this->checkFixture($this->db, 'customer'); + $this->checkFixture($this->db(), 'customer'); - $customerQuery = new ActiveQuery(Customer::class, $this->db); + $customerQuery = new ActiveQuery(Customer::class); $customer = $customerQuery->findOne(1); $customer->setId(2); @@ -877,9 +879,9 @@ public function testGetOldPrimaryKey(): void public function testGetDirtyAttributesOnNewRecord(): void { - $this->checkFixture($this->db, 'customer'); + $this->checkFixture($this->db(), 'customer'); - $customer = new Customer($this->db); + $customer = new Customer(); $this->assertSame( [ @@ -928,9 +930,9 @@ public function testGetDirtyAttributesOnNewRecord(): void public function testGetDirtyAttributesAfterFind(): void { - $this->checkFixture($this->db, 'customer'); + $this->checkFixture($this->db(), 'customer'); - $customerQuery = new ActiveQuery(Customer::class, $this->db); + $customerQuery = new ActiveQuery(Customer::class); $customer = $customerQuery->findOne(1); $this->assertSame([], $customer->getDirtyAttributes()); @@ -951,9 +953,9 @@ public function testGetDirtyAttributesAfterFind(): void public function testRelationWithInstance(): void { - $this->checkFixture($this->db, 'customer'); + $this->checkFixture($this->db(), 'customer'); - $customerQuery = new ActiveQuery(Customer::class, $this->db); + $customerQuery = new ActiveQuery(Customer::class); $customer = $customerQuery->findOne(2); $orders = $customer->getOrdersUsingInstance(); @@ -963,4 +965,24 @@ public function testRelationWithInstance(): void $this->assertSame(2, $orders[0]->getId()); $this->assertSame(3, $orders[1]->getId()); } + + public function testWithCustomConnection(): void + { + $db = $this->createConnection(); + + ConnectionProvider::set($db, 'custom'); + $this->checkFixture($db, 'customer'); + + $customer = new CustomerWithCustomConnection(); + + $this->assertSame($this->db(), $customer->db()); + + $customer = $customer->withConnectionName('custom'); + + $this->assertSame($db, $customer->db()); + + $db->close(); + + ConnectionProvider::remove('custom'); + } } diff --git a/tests/BatchQueryResultTest.php b/tests/BatchQueryResultTest.php index 2398272e9..fa49261f5 100644 --- a/tests/BatchQueryResultTest.php +++ b/tests/BatchQueryResultTest.php @@ -12,9 +12,9 @@ abstract class BatchQueryResultTest extends TestCase { public function testQuery(): void { - $this->checkFixture($this->db, 'customer', true); + $this->checkFixture($this->db(), 'customer', true); - $customerQuery = new ActiveQuery(Customer::class, $this->db); + $customerQuery = new ActiveQuery(Customer::class); $query = $customerQuery->orderBy('id'); @@ -25,7 +25,7 @@ public function testQuery(): void $this->assertSame($result->getQuery(), $query); /** normal query */ - $customerQuery = new ActiveQuery(Customer::class, $this->db); + $customerQuery = new ActiveQuery(Customer::class); $query = $customerQuery->orderBy('id'); @@ -68,7 +68,7 @@ public function testQuery(): void $this->assertCount(0, $allRows); /** query with index */ - $customerQuery = new ActiveQuery(Customer::class, $this->db); + $customerQuery = new ActiveQuery(Customer::class); $query = $customerQuery->indexBy('name'); @@ -84,7 +84,7 @@ public function testQuery(): void $this->assertEquals('address3', $allRows['user3']->getAddress()); /** each */ - $customerQuery = new ActiveQuery(Customer::class, $this->db); + $customerQuery = new ActiveQuery(Customer::class); $query = $customerQuery->orderBy('id'); @@ -99,7 +99,7 @@ public function testQuery(): void $this->assertEquals('user3', $allRows[2]->getName()); /** each with key */ - $customerQuery = new ActiveQuery(Customer::class, $this->db); + $customerQuery = new ActiveQuery(Customer::class); $query = $customerQuery->orderBy('id')->indexBy('name'); @@ -117,10 +117,10 @@ public function testQuery(): void public function testActiveQuery(): void { - $this->checkFixture($this->db, 'customer'); + $this->checkFixture($this->db(), 'customer'); /** batch with eager loading */ - $customerQuery = new ActiveQuery(Customer::class, $this->db); + $customerQuery = new ActiveQuery(Customer::class); $query = $customerQuery->with('orders')->orderBy('id'); @@ -138,9 +138,9 @@ public function testActiveQuery(): void public function testBatchWithIndexBy(): void { - $this->checkFixture($this->db, 'customer'); + $this->checkFixture($this->db(), 'customer'); - $customerQuery = new ActiveQuery(Customer::class, $this->db); + $customerQuery = new ActiveQuery(Customer::class); $query = $customerQuery->orderBy('id')->limit(3)->indexBy('id'); diff --git a/tests/ConnectionProviderTest.php b/tests/ConnectionProviderTest.php new file mode 100644 index 000000000..e95285cd1 --- /dev/null +++ b/tests/ConnectionProviderTest.php @@ -0,0 +1,72 @@ +assertTrue(ConnectionProvider::has('default')); + $this->assertFalse(ConnectionProvider::has('db2')); + + $db = ConnectionProvider::get(); + + $this->assertTrue(ConnectionProvider::has('default')); + $this->assertSame($db, ConnectionProvider::get('default')); + + $list = ConnectionProvider::all(); + + $this->assertSame($list, ['default' => $db]); + + $db2 = $this->createConnection(); + ConnectionProvider::set($db2, 'db2'); + + $this->assertTrue(ConnectionProvider::has('db2')); + $this->assertSame($db2, ConnectionProvider::get('db2')); + + $list = ConnectionProvider::all(); + + $this->assertSame($list, ['default' => $db, 'db2' => $db2]); + + ConnectionProvider::remove('db2'); + + $this->assertFalse(ConnectionProvider::has('db2')); + + $list = ConnectionProvider::all(); + + $this->assertSame($list, ['default' => $db]); + } + + public function testConnectionProviderMiddleware(): void + { + ConnectionProvider::remove(); + + $this->assertEmpty(ConnectionProvider::all()); + $this->assertFalse(ConnectionProvider::has('default')); + + $db = $this->createConnection(); + $container = new Container(ContainerConfig::create()->withDefinitions([ConnectionInterface::class => $db])); + $request = $this->createMock(ServerRequestInterface::class); + $requestHandler = $this->createMock(RequestHandlerInterface::class); + + $dispatcher = (new MiddlewareDispatcher(new MiddlewareFactory($container))) + ->withMiddlewares([ConnectionProviderMiddleware::class]); + + $dispatcher->dispatch($request, $requestHandler); + + $this->assertTrue(ConnectionProvider::has('default')); + $this->assertSame($db, ConnectionProvider::get()); + } +} diff --git a/tests/Driver/Mssql/ActiveQueryFindTest.php b/tests/Driver/Mssql/ActiveQueryFindTest.php index 813fee8eb..bb39b9f27 100644 --- a/tests/Driver/Mssql/ActiveQueryFindTest.php +++ b/tests/Driver/Mssql/ActiveQueryFindTest.php @@ -5,23 +5,12 @@ namespace Yiisoft\ActiveRecord\Tests\Driver\Mssql; use Yiisoft\ActiveRecord\Tests\Support\MssqlHelper; +use Yiisoft\Db\Connection\ConnectionInterface; final class ActiveQueryFindTest extends \Yiisoft\ActiveRecord\Tests\ActiveQueryFindTest { - public function setUp(): void + protected function createConnection(): ConnectionInterface { - parent::setUp(); - - $mssqlHelper = new MssqlHelper(); - $this->db = $mssqlHelper->createConnection(); - } - - protected function tearDown(): void - { - parent::tearDown(); - - $this->db->close(); - - unset($this->db); + return (new MssqlHelper())->createConnection(); } } diff --git a/tests/Driver/Mssql/ActiveQueryTest.php b/tests/Driver/Mssql/ActiveQueryTest.php index 357b5bebb..6abeedf90 100644 --- a/tests/Driver/Mssql/ActiveQueryTest.php +++ b/tests/Driver/Mssql/ActiveQueryTest.php @@ -5,23 +5,12 @@ namespace Yiisoft\ActiveRecord\Tests\Driver\Mssql; use Yiisoft\ActiveRecord\Tests\Support\MssqlHelper; +use Yiisoft\Db\Connection\ConnectionInterface; final class ActiveQueryTest extends \Yiisoft\ActiveRecord\Tests\ActiveQueryTest { - public function setUp(): void + protected function createConnection(): ConnectionInterface { - parent::setUp(); - - $mssqlHelper = new MssqlHelper(); - $this->db = $mssqlHelper->createConnection(); - } - - protected function tearDown(): void - { - parent::tearDown(); - - $this->db->close(); - - unset($this->db); + return (new MssqlHelper())->createConnection(); } } diff --git a/tests/Driver/Mssql/ActiveRecordFactoryTest.php b/tests/Driver/Mssql/ActiveRecordFactoryTest.php deleted file mode 100644 index 12c039701..000000000 --- a/tests/Driver/Mssql/ActiveRecordFactoryTest.php +++ /dev/null @@ -1,28 +0,0 @@ -db = $mssqlHelper->createConnection(); - $this->arFactory = $mssqlHelper->createARFactory($this->db); - } - - protected function tearDown(): void - { - parent::tearDown(); - - $this->db->close(); - - unset($this->arFactory, $this->db); - } -} diff --git a/tests/Driver/Mssql/ActiveRecordTest.php b/tests/Driver/Mssql/ActiveRecordTest.php index a509ee7b1..39ad1fa00 100644 --- a/tests/Driver/Mssql/ActiveRecordTest.php +++ b/tests/Driver/Mssql/ActiveRecordTest.php @@ -8,29 +8,18 @@ use Yiisoft\ActiveRecord\Tests\Stubs\ActiveRecord\TestTrigger; use Yiisoft\ActiveRecord\Tests\Stubs\ActiveRecord\TestTriggerAlert; use Yiisoft\ActiveRecord\Tests\Support\MssqlHelper; +use Yiisoft\Db\Connection\ConnectionInterface; final class ActiveRecordTest extends \Yiisoft\ActiveRecord\Tests\ActiveRecordTest { - public function setUp(): void + protected function createConnection(): ConnectionInterface { - parent::setUp(); - - $mssqlHelper = new MssqlHelper(); - $this->db = $mssqlHelper->createConnection(); - } - - protected function tearDown(): void - { - parent::tearDown(); - - $this->db->close(); - - unset($this->db); + return (new MssqlHelper())->createConnection(); } public function testSaveWithTrigger(): void { - $this->checkFixture($this->db, 'test_trigger'); + $this->checkFixture($this->db(), 'test_trigger'); // drop trigger if exist $sql = <<db->createCommand($sql)->execute(); + $this->db()->createCommand($sql)->execute(); // create trigger $sql = <<db->createCommand($sql)->execute(); + $this->db()->createCommand($sql)->execute(); - $record = new TestTrigger($this->db); + $record = new TestTrigger($this->db()); $record->stringcol = 'test'; $this->assertTrue($record->save()); $this->assertEquals(1, $record->id); - $testRecordQuery = new ActiveQuery(TestTriggerAlert::class, $this->db); + $testRecordQuery = new ActiveQuery(TestTriggerAlert::class); $this->assertEquals('test', $testRecordQuery->findOne(1)->stringcol); } diff --git a/tests/Driver/Mssql/BatchQueryResultTest.php b/tests/Driver/Mssql/BatchQueryResultTest.php index dbe752085..c534f0cb3 100644 --- a/tests/Driver/Mssql/BatchQueryResultTest.php +++ b/tests/Driver/Mssql/BatchQueryResultTest.php @@ -5,23 +5,12 @@ namespace Yiisoft\ActiveRecord\Tests\Driver\Mssql; use Yiisoft\ActiveRecord\Tests\Support\MssqlHelper; +use Yiisoft\Db\Connection\ConnectionInterface; final class BatchQueryResultTest extends \Yiisoft\ActiveRecord\Tests\BatchQueryResultTest { - public function setUp(): void + protected function createConnection(): ConnectionInterface { - parent::setUp(); - - $mssqlHelper = new MssqlHelper(); - $this->db = $mssqlHelper->createConnection(); - } - - protected function tearDown(): void - { - parent::tearDown(); - - $this->db->close(); - - unset($this->db); + return (new MssqlHelper())->createConnection(); } } diff --git a/tests/Driver/Mssql/ConnectionProviderTest.php b/tests/Driver/Mssql/ConnectionProviderTest.php new file mode 100644 index 000000000..3664548f3 --- /dev/null +++ b/tests/Driver/Mssql/ConnectionProviderTest.php @@ -0,0 +1,16 @@ +createConnection(); + } +} diff --git a/tests/Driver/Mssql/MagicActiveRecordTest.php b/tests/Driver/Mssql/MagicActiveRecordTest.php index 219c6da92..174f74d0d 100644 --- a/tests/Driver/Mssql/MagicActiveRecordTest.php +++ b/tests/Driver/Mssql/MagicActiveRecordTest.php @@ -8,29 +8,18 @@ use Yiisoft\ActiveRecord\Tests\Stubs\MagicActiveRecord\TestTrigger; use Yiisoft\ActiveRecord\Tests\Stubs\MagicActiveRecord\TestTriggerAlert; use Yiisoft\ActiveRecord\Tests\Support\MssqlHelper; +use Yiisoft\Db\Connection\ConnectionInterface; final class MagicActiveRecordTest extends \Yiisoft\ActiveRecord\Tests\MagicActiveRecordTest { - public function setUp(): void + protected function createConnection(): ConnectionInterface { - parent::setUp(); - - $mssqlHelper = new MssqlHelper(); - $this->db = $mssqlHelper->createConnection(); - } - - protected function tearDown(): void - { - parent::tearDown(); - - $this->db->close(); - - unset($this->db); + return (new MssqlHelper())->createConnection(); } public function testSaveWithTrigger(): void { - $this->checkFixture($this->db, 'test_trigger'); + $this->checkFixture($this->db(), 'test_trigger'); // drop trigger if exist $sql = <<db->createCommand($sql)->execute(); + $this->db()->createCommand($sql)->execute(); // create trigger $sql = <<db->createCommand($sql)->execute(); + $this->db()->createCommand($sql)->execute(); - $record = new TestTrigger($this->db); + $record = new TestTrigger(); $record->stringcol = 'test'; $this->assertTrue($record->save()); $this->assertEquals(1, $record->id); - $testRecordQuery = new ActiveQuery(TestTriggerAlert::class, $this->db); + $testRecordQuery = new ActiveQuery(TestTriggerAlert::class); $this->assertEquals('test', $testRecordQuery->findOne(1)->stringcol); } diff --git a/tests/Driver/Mysql/ActiveQueryFindTest.php b/tests/Driver/Mysql/ActiveQueryFindTest.php index 8d57b3c83..87f111442 100644 --- a/tests/Driver/Mysql/ActiveQueryFindTest.php +++ b/tests/Driver/Mysql/ActiveQueryFindTest.php @@ -5,23 +5,12 @@ namespace Yiisoft\ActiveRecord\Tests\Driver\Mysql; use Yiisoft\ActiveRecord\Tests\Support\MysqlHelper; +use Yiisoft\Db\Connection\ConnectionInterface; final class ActiveQueryFindTest extends \Yiisoft\ActiveRecord\Tests\ActiveQueryFindTest { - public function setUp(): void + protected function createConnection(): ConnectionInterface { - parent::setUp(); - - $mysqlHelper = new MysqlHelper(); - $this->db = $mysqlHelper->createConnection(); - } - - protected function tearDown(): void - { - parent::tearDown(); - - $this->db->close(); - - unset($this->db); + return (new MysqlHelper())->createConnection(); } } diff --git a/tests/Driver/Mysql/ActiveQueryTest.php b/tests/Driver/Mysql/ActiveQueryTest.php index 9f67e7d73..be875d749 100644 --- a/tests/Driver/Mysql/ActiveQueryTest.php +++ b/tests/Driver/Mysql/ActiveQueryTest.php @@ -5,23 +5,12 @@ namespace Yiisoft\ActiveRecord\Tests\Driver\Mysql; use Yiisoft\ActiveRecord\Tests\Support\MysqlHelper; +use Yiisoft\Db\Connection\ConnectionInterface; final class ActiveQueryTest extends \Yiisoft\ActiveRecord\Tests\ActiveQueryTest { - public function setUp(): void + protected function createConnection(): ConnectionInterface { - parent::setUp(); - - $mysqlHelper = new MysqlHelper(); - $this->db = $mysqlHelper->createConnection(); - } - - protected function tearDown(): void - { - parent::tearDown(); - - $this->db->close(); - - unset($this->db); + return (new MysqlHelper())->createConnection(); } } diff --git a/tests/Driver/Mysql/ActiveRecordFactoryTest.php b/tests/Driver/Mysql/ActiveRecordFactoryTest.php deleted file mode 100644 index 1e3fe1629..000000000 --- a/tests/Driver/Mysql/ActiveRecordFactoryTest.php +++ /dev/null @@ -1,28 +0,0 @@ -db = $mysqlHelper->createConnection(); - $this->arFactory = $mysqlHelper->createARFactory($this->db); - } - - protected function tearDown(): void - { - parent::tearDown(); - - $this->db->close(); - - unset($this->arFactory, $this->db); - } -} diff --git a/tests/Driver/Mysql/ActiveRecordTest.php b/tests/Driver/Mysql/ActiveRecordTest.php index 92972d7aa..c4ecfdd21 100644 --- a/tests/Driver/Mysql/ActiveRecordTest.php +++ b/tests/Driver/Mysql/ActiveRecordTest.php @@ -9,31 +9,20 @@ use Yiisoft\ActiveRecord\Tests\Stubs\ActiveRecord\Beta; use Yiisoft\ActiveRecord\Tests\Stubs\ActiveRecord\Customer; use Yiisoft\ActiveRecord\Tests\Support\MysqlHelper; +use Yiisoft\Db\Connection\ConnectionInterface; final class ActiveRecordTest extends \Yiisoft\ActiveRecord\Tests\ActiveRecordTest { - public function setUp(): void + protected function createConnection(): ConnectionInterface { - parent::setUp(); - - $mysqlHelper = new MysqlHelper(); - $this->db = $mysqlHelper->createConnection(); - } - - protected function tearDown(): void - { - parent::tearDown(); - - $this->db->close(); - - unset($this->db); + return (new MysqlHelper())->createConnection(); } public function testCastValues(): void { - $this->checkFixture($this->db, 'type'); + $this->checkFixture($this->db(), 'type'); - $arClass = new Type($this->db); + $arClass = new Type(); $arClass->int_col = 123; $arClass->int_col2 = 456; @@ -50,7 +39,7 @@ public function testCastValues(): void $arClass->save(); /** @var $model Type */ - $aqClass = new ActiveQuery(Type::class, $this->db); + $aqClass = new ActiveQuery(Type::class); $query = $aqClass->onePopulate(); $this->assertSame(123, $query->int_col); @@ -64,9 +53,9 @@ public function testCastValues(): void public function testExplicitPkOnAutoIncrement(): void { - $this->checkFixture($this->db, 'customer'); + $this->checkFixture($this->db(), 'customer'); - $customer = new Customer($this->db); + $customer = new Customer(); $customer->setId(1337); $customer->setEmail('user1337@example.com'); @@ -86,9 +75,9 @@ public function testExplicitPkOnAutoIncrement(): void */ public function testEagerLoadingUsingStringIdentifiers(): void { - $this->checkFixture($this->db, 'beta'); + $this->checkFixture($this->db(), 'beta'); - $betaQuery = new ActiveQuery(Beta::class, $this->db); + $betaQuery = new ActiveQuery(Beta::class); $betas = $betaQuery->with('alpha')->all(); diff --git a/tests/Driver/Mysql/BatchQueryResultTest.php b/tests/Driver/Mysql/BatchQueryResultTest.php index 57ff87454..b3d3e3d00 100644 --- a/tests/Driver/Mysql/BatchQueryResultTest.php +++ b/tests/Driver/Mysql/BatchQueryResultTest.php @@ -5,23 +5,12 @@ namespace Yiisoft\ActiveRecord\Tests\Driver\Mysql; use Yiisoft\ActiveRecord\Tests\Support\MysqlHelper; +use Yiisoft\Db\Connection\ConnectionInterface; final class BatchQueryResultTest extends \Yiisoft\ActiveRecord\Tests\BatchQueryResultTest { - public function setUp(): void + protected function createConnection(): ConnectionInterface { - parent::setUp(); - - $mysqlHelper = new MysqlHelper(); - $this->db = $mysqlHelper->createConnection(); - } - - protected function tearDown(): void - { - parent::tearDown(); - - $this->db->close(); - - unset($this->db); + return (new MysqlHelper())->createConnection(); } } diff --git a/tests/Driver/Mysql/ConnectionProviderTest.php b/tests/Driver/Mysql/ConnectionProviderTest.php new file mode 100644 index 000000000..aaf56a3b2 --- /dev/null +++ b/tests/Driver/Mysql/ConnectionProviderTest.php @@ -0,0 +1,16 @@ +createConnection(); + } +} diff --git a/tests/Driver/Mysql/MagicActiveRecordTest.php b/tests/Driver/Mysql/MagicActiveRecordTest.php index 411dbe03e..f63839acb 100644 --- a/tests/Driver/Mysql/MagicActiveRecordTest.php +++ b/tests/Driver/Mysql/MagicActiveRecordTest.php @@ -8,31 +8,20 @@ use Yiisoft\ActiveRecord\Tests\Stubs\MagicActiveRecord\Beta; use Yiisoft\ActiveRecord\Tests\Stubs\MagicActiveRecord\Customer; use Yiisoft\ActiveRecord\Tests\Support\MysqlHelper; +use Yiisoft\Db\Connection\ConnectionInterface; final class MagicActiveRecordTest extends \Yiisoft\ActiveRecord\Tests\MagicActiveRecordTest { - public function setUp(): void + protected function createConnection(): ConnectionInterface { - parent::setUp(); - - $mysqlHelper = new MysqlHelper(); - $this->db = $mysqlHelper->createConnection(); - } - - protected function tearDown(): void - { - parent::tearDown(); - - $this->db->close(); - - unset($this->db); + return (new MysqlHelper())->createConnection(); } public function testExplicitPkOnAutoIncrement(): void { - $this->checkFixture($this->db, 'customer'); + $this->checkFixture($this->db(), 'customer'); - $customer = new Customer($this->db); + $customer = new Customer(); $customer->id = 1337; $customer->email = 'user1337@example.com'; @@ -52,9 +41,9 @@ public function testExplicitPkOnAutoIncrement(): void */ public function testEagerLoadingUsingStringIdentifiers(): void { - $this->checkFixture($this->db, 'beta'); + $this->checkFixture($this->db(), 'beta'); - $betaQuery = new ActiveQuery(Beta::class, $this->db); + $betaQuery = new ActiveQuery(Beta::class); $betas = $betaQuery->with('alpha')->all(); diff --git a/tests/Driver/Oracle/ActiveQueryFindTest.php b/tests/Driver/Oracle/ActiveQueryFindTest.php index d56f8fb34..3252b8ca3 100644 --- a/tests/Driver/Oracle/ActiveQueryFindTest.php +++ b/tests/Driver/Oracle/ActiveQueryFindTest.php @@ -9,42 +9,31 @@ use Yiisoft\ActiveRecord\Tests\Stubs\ActiveRecord\Customer; use Yiisoft\ActiveRecord\Tests\Stubs\ActiveRecord\Order; use Yiisoft\ActiveRecord\Tests\Support\OracleHelper; +use Yiisoft\Db\Connection\ConnectionInterface; final class ActiveQueryFindTest extends \Yiisoft\ActiveRecord\Tests\ActiveQueryFindTest { - public function setUp(): void + protected function createConnection(): ConnectionInterface { - parent::setUp(); - - $oracleHelper = new OracleHelper(); - $this->db = $oracleHelper->createConnection(); - } - - protected function tearDown(): void - { - parent::tearDown(); - - $this->db->close(); - - unset($this->db); + return (new OracleHelper())->createConnection(); } public function testFindLimit(): void { - $this->checkFixture($this->db, 'customer', true); + $this->checkFixture($this->db(), 'customer', true); /** one */ - $customerQuery = new ActiveQuery(CustomerWithRownumid::class, $this->db); + $customerQuery = new ActiveQuery(CustomerWithRownumid::class); $customer = $customerQuery->orderBy('id')->onePopulate(); $this->assertEquals('user1', $customer->getName()); /** all */ - $customerQuery = new ActiveQuery(CustomerWithRownumid::class, $this->db); + $customerQuery = new ActiveQuery(CustomerWithRownumid::class); $customers = $customerQuery->allPopulate(); $this->assertCount(3, $customers); /** limit */ - $customerQuery = new ActiveQuery(CustomerWithRownumid::class, $this->db); + $customerQuery = new ActiveQuery(CustomerWithRownumid::class); $customers = $customerQuery->orderBy('id')->limit(1)->allPopulate(); $this->assertCount(1, $customers); $this->assertEquals('user1', $customers[0]->getName()); @@ -66,7 +55,7 @@ public function testFindLimit(): void $this->assertCount(0, $customers); /** offset */ - $customerQuery = new ActiveQuery(CustomerWithRownumid::class, $this->db); + $customerQuery = new ActiveQuery(CustomerWithRownumid::class); $customer = $customerQuery->orderBy('id')->offset(0)->onePopulate(); $this->assertEquals('user1', $customer->getName()); @@ -82,9 +71,9 @@ public function testFindLimit(): void public function testFindEager(): void { - $this->checkFixture($this->db, 'customer', true); + $this->checkFixture($this->db(), 'customer', true); - $customerQuery = new ActiveQuery(Customer::class, $this->db); + $customerQuery = new ActiveQuery(Customer::class); $customers = $customerQuery->with('orders')->indexBy('id')->all(); ksort($customers); @@ -105,7 +94,7 @@ public function testFindEager(): void $this->assertCount(1, $customer->getRelatedRecords()); /** multiple with() calls */ - $orderQuery = new ActiveQuery(Order::class, $this->db); + $orderQuery = new ActiveQuery(Order::class); $orders = $orderQuery->with('customer', 'items')->all(); $this->assertCount(3, $orders); $this->assertTrue($orders[0]->isRelationPopulated('customer')); @@ -119,10 +108,10 @@ public function testFindEager(): void public function testFindAsArray(): void { - $this->checkFixture($this->db, 'customer'); + $this->checkFixture($this->db(), 'customer'); /** asArray */ - $customerQuery = new ActiveQuery(Customer::class, $this->db); + $customerQuery = new ActiveQuery(Customer::class); $customer = $customerQuery->where(['[[id]]' => 2])->asArray()->onePopulate(); $this->assertEquals([ 'id' => 2, @@ -135,7 +124,7 @@ public function testFindAsArray(): void ], $customer); /** find all asArray */ - $customerQuery = new ActiveQuery(Customer::class, $this->db); + $customerQuery = new ActiveQuery(Customer::class); $customers = $customerQuery->asArray()->all(); $this->assertCount(3, $customers); $this->assertArrayHasKey('id', $customers[0]); diff --git a/tests/Driver/Oracle/ActiveQueryTest.php b/tests/Driver/Oracle/ActiveQueryTest.php index ccce1fa4e..12e0062dc 100644 --- a/tests/Driver/Oracle/ActiveQueryTest.php +++ b/tests/Driver/Oracle/ActiveQueryTest.php @@ -9,26 +9,15 @@ use Yiisoft\ActiveRecord\Tests\Driver\Oracle\Stubs\Order; use Yiisoft\ActiveRecord\Tests\Driver\Oracle\Stubs\BitValues; use Yiisoft\ActiveRecord\Tests\Support\OracleHelper; +use Yiisoft\Db\Connection\ConnectionInterface; use Yiisoft\Db\Exception\Exception; use Yiisoft\Db\Exception\InvalidConfigException; final class ActiveQueryTest extends \Yiisoft\ActiveRecord\Tests\ActiveQueryTest { - public function setUp(): void + protected function createConnection(): ConnectionInterface { - parent::setUp(); - - $oracleHelper = new OracleHelper(); - $this->db = $oracleHelper->createConnection(); - } - - protected function tearDown(): void - { - parent::tearDown(); - - $this->db->close(); - - unset($this->db); + return (new OracleHelper())->createConnection(); } /** @@ -45,10 +34,10 @@ protected function tearDown(): void public function testJoinWithAlias(string $aliasMethod): void { $orders = []; - $this->checkFixture($this->db, 'order', true); + $this->checkFixture($this->db(), 'order', true); /** left join and eager loading */ - $orderQuery = new ActiveQuery(Order::class, $this->db); + $orderQuery = new ActiveQuery(Order::class); $query = $orderQuery->joinWith(['customer c']); if ($aliasMethod === 'explicit') { @@ -70,7 +59,7 @@ public function testJoinWithAlias(string $aliasMethod): void $this->assertTrue($orders[2]->isRelationPopulated('customer')); /** inner join filtering and eager loading */ - $orderQuery = new ActiveQuery(Order::class, $this->db); + $orderQuery = new ActiveQuery(Order::class); $query = $orderQuery->innerJoinWith(['customer c']); if ($aliasMethod === 'explicit') { @@ -90,7 +79,7 @@ public function testJoinWithAlias(string $aliasMethod): void $this->assertTrue($orders[1]->isRelationPopulated('customer')); /** inner join filtering without eager loading */ - $orderQuery = new ActiveQuery(Order::class, $this->db); + $orderQuery = new ActiveQuery(Order::class); $query = $orderQuery->innerJoinWith(['customer c'], false); if ($aliasMethod === 'explicit') { @@ -110,7 +99,7 @@ public function testJoinWithAlias(string $aliasMethod): void $this->assertFalse($orders[1]->isRelationPopulated('customer')); /** join with via-relation */ - $orderQuery = new ActiveQuery(Order::class, $this->db); + $orderQuery = new ActiveQuery(Order::class); $query = $orderQuery->innerJoinWith(['books b']); if ($aliasMethod === 'explicit') { @@ -136,7 +125,7 @@ public function testJoinWithAlias(string $aliasMethod): void $this->assertTrue($orders[1]->isRelationPopulated('books')); /** joining sub relations */ - $orderQuery = new ActiveQuery(Order::class, $this->db); + $orderQuery = new ActiveQuery(Order::class); $query = $orderQuery->innerJoinWith( [ 'items i' => static function ($q) use ($aliasMethod) { @@ -181,7 +170,7 @@ public function testJoinWithAlias(string $aliasMethod): void if ($aliasMethod === 'explicit' || $aliasMethod === 'querysyntax') { $relationName = 'books' . ucfirst($aliasMethod); - $orderQuery = new ActiveQuery(Order::class, $this->db); + $orderQuery = new ActiveQuery(Order::class); $orders = $orderQuery->joinWith(["$relationName b"])->orderBy('order.id')->all(); $this->assertCount(3, $orders); @@ -200,7 +189,7 @@ public function testJoinWithAlias(string $aliasMethod): void if ($aliasMethod === 'explicit' || $aliasMethod === 'querysyntax') { $relationName = 'books' . ucfirst($aliasMethod) . 'A'; - $orderQuery = new ActiveQuery(Order::class, $this->db); + $orderQuery = new ActiveQuery(Order::class); $orders = $orderQuery->joinWith([(string)$relationName])->orderBy('order.id')->all(); $this->assertCount(3, $orders); @@ -216,7 +205,7 @@ public function testJoinWithAlias(string $aliasMethod): void } /** join with count and query */ - $orderQuery = new ActiveQuery(Order::class, $this->db); + $orderQuery = new ActiveQuery(Order::class); $query = $orderQuery->joinWith(['customer c']); if ($aliasMethod === 'explicit') { @@ -233,7 +222,7 @@ public function testJoinWithAlias(string $aliasMethod): void $this->assertCount(3, $orders); /** relational query */ - $orderQuery = new ActiveQuery(Order::class, $this->db); + $orderQuery = new ActiveQuery(Order::class); $order = $orderQuery->findOne(1); $customerQuery = $order->getCustomerQuery()->innerJoinWith(['orders o'], false); @@ -250,7 +239,7 @@ public function testJoinWithAlias(string $aliasMethod): void $this->assertNotNull($customer); /** join with sub-relation called inside Closure */ - $orderQuery = new ActiveQuery(Order::class, $this->db); + $orderQuery = new ActiveQuery(Order::class); $orders = $orderQuery->joinWith( [ 'items' => static function ($q) use ($aliasMethod) { @@ -282,13 +271,13 @@ public function testJoinWithAlias(string $aliasMethod): void */ public function testJoinWithSameTable(): void { - $this->checkFixture($this->db, 'order'); + $this->checkFixture($this->db(), 'order'); /** * join with the same table but different aliases alias is defined in the relation definition without eager * loading */ - $query = new ActiveQuery(Order::class, $this->db); + $query = new ActiveQuery(Order::class); $query ->joinWith('bookItems', false) ->joinWith('movieItems', false) @@ -304,7 +293,7 @@ public function testJoinWithSameTable(): void $this->assertFalse($orders[0]->isRelationPopulated('movieItems')); /** with eager loading */ - $query = new ActiveQuery(Order::class, $this->db); + $query = new ActiveQuery(Order::class); $query->joinWith('bookItems', true)->joinWith('movieItems', true)->where(['{{movies}}.[[name]]' => 'Toy Story']); $orders = $query->all(); $this->assertCount( @@ -322,7 +311,7 @@ public function testJoinWithSameTable(): void * join with the same table but different aliases alias is defined in the call to joinWith() without eager * loading */ - $query = new ActiveQuery(Order::class, $this->db); + $query = new ActiveQuery(Order::class); $query ->joinWith( [ @@ -349,7 +338,7 @@ public function testJoinWithSameTable(): void $this->assertFalse($orders[0]->isRelationPopulated('itemsIndexed')); /** with eager loading, only for one relation as it would be overwritten otherwise. */ - $query = new ActiveQuery(Order::class, $this->db); + $query = new ActiveQuery(Order::class); $query ->joinWith( [ @@ -374,7 +363,7 @@ public function testJoinWithSameTable(): void $this->assertTrue($orders[0]->isRelationPopulated('itemsIndexed')); /** with eager loading, and the other relation */ - $query = new ActiveQuery(Order::class, $this->db); + $query = new ActiveQuery(Order::class); $query ->joinWith( [ @@ -405,13 +394,13 @@ public function testJoinWithSameTable(): void */ public function testBit(): void { - $this->checkFixture($this->db, 'bit_values'); + $this->checkFixture($this->db(), 'bit_values'); - $bitValueQuery = new ActiveQuery(BitValues::class, $this->db); + $bitValueQuery = new ActiveQuery(BitValues::class); $falseBit = $bitValueQuery->findOne(1); $this->assertEquals('0', $falseBit->val); - $bitValueQuery = new ActiveQuery(BitValues::class, $this->db); + $bitValueQuery = new ActiveQuery(BitValues::class); $trueBit = $bitValueQuery->findOne(2); $this->assertEquals('1', $trueBit->val); } diff --git a/tests/Driver/Oracle/ActiveRecordFactoryTest.php b/tests/Driver/Oracle/ActiveRecordFactoryTest.php deleted file mode 100644 index d01c5341a..000000000 --- a/tests/Driver/Oracle/ActiveRecordFactoryTest.php +++ /dev/null @@ -1,28 +0,0 @@ -db = $oracleHelper->createConnection(); - $this->arFactory = $oracleHelper->createARFactory($this->db); - } - - protected function tearDown(): void - { - parent::tearDown(); - - $this->db->close(); - - unset($this->arFactory, $this->db); - } -} diff --git a/tests/Driver/Oracle/ActiveRecordTest.php b/tests/Driver/Oracle/ActiveRecordTest.php index 5b41b4541..4f82fc7ef 100644 --- a/tests/Driver/Oracle/ActiveRecordTest.php +++ b/tests/Driver/Oracle/ActiveRecordTest.php @@ -9,33 +9,22 @@ use Yiisoft\ActiveRecord\Tests\Stubs\ActiveRecord\CustomerClosureField; use Yiisoft\ActiveRecord\Tests\Stubs\ActiveRecord\Type; use Yiisoft\ActiveRecord\Tests\Support\OracleHelper; +use Yiisoft\Db\Connection\ConnectionInterface; final class ActiveRecordTest extends \Yiisoft\ActiveRecord\Tests\ActiveRecordTest { - public function setUp(): void + protected function createConnection(): ConnectionInterface { - parent::setUp(); - - $oracleHelper = new OracleHelper(); - $this->db = $oracleHelper->createConnection(); - } - - protected function tearDown(): void - { - parent::tearDown(); - - $this->db->close(); - - unset($this->db); + return (new OracleHelper())->createConnection(); } public function testCastValues(): void { $this->markTestSkipped('Cant bind floats without support from a custom PDO driver.'); - $this->checkFixture($this->db, 'customer'); + $this->checkFixture($this->db(), 'customer'); - $arClass = new Type($this->db); + $arClass = new Type(); $arClass->int_col = 123; $arClass->int_col2 = 456; $arClass->smallint_col = 42; @@ -49,7 +38,7 @@ public function testCastValues(): void $arClass->bool_col2 = 0; $arClass->save(); - $aqClass = new ActiveQuery(Type::class, $this->db); + $aqClass = new ActiveQuery(Type::class); $query = $aqClass->onePopulate(); $this->assertSame(123, $query->int_col); @@ -66,9 +55,9 @@ public function testCastValues(): void public function testDefaultValues(): void { - $this->checkFixture($this->db, 'customer'); + $this->checkFixture($this->db(), 'customer'); - $arClass = new Type($this->db); + $arClass = new Type(); $arClass->loadDefaultValues(); $this->assertEquals(1, $arClass->int_col2); $this->assertEquals('something', $arClass->char_col2); @@ -78,13 +67,13 @@ public function testDefaultValues(): void // not testing $arClass->time, because oci\Schema can't read default value - $arClass = new Type($this->db); + $arClass = new Type(); $arClass->char_col2 = 'not something'; $arClass->loadDefaultValues(); $this->assertEquals('not something', $arClass->char_col2); - $arClass = new Type($this->db); + $arClass = new Type(); $arClass->char_col2 = 'not something'; $arClass->loadDefaultValues(false); @@ -98,9 +87,9 @@ public function testDefaultValues(): void */ public function testBooleanAttribute(): void { - $this->checkFixture($this->db, 'customer', true); + $this->checkFixture($this->db(), 'customer', true); - $customer = new Customer($this->db); + $customer = new Customer(); $customer->setName('boolean customer'); $customer->setEmail('mail@example.com'); @@ -116,20 +105,20 @@ public function testBooleanAttribute(): void $customer->refresh(); $this->assertEquals(0, $customer->getStatus()); - $customerQuery = new ActiveQuery(Customer::class, $this->db); + $customerQuery = new ActiveQuery(Customer::class); $customers = $customerQuery->where(['status' => 1])->all(); $this->assertCount(2, $customers); - $customerQuery = new ActiveQuery(Customer::class, $this->db); + $customerQuery = new ActiveQuery(Customer::class); $customers = $customerQuery->where(['status' => '0'])->all(); $this->assertCount(1, $customers); } public function testToArray(): void { - $this->checkFixture($this->db, 'customer', true); + $this->checkFixture($this->db(), 'customer', true); - $customerQuery = new ActiveQuery(Customer::class, $this->db); + $customerQuery = new ActiveQuery(Customer::class); $customer = $customerQuery->findOne(1); $this->assertSame( @@ -148,9 +137,9 @@ public function testToArray(): void public function testToArrayWithClosure(): void { - $this->checkFixture($this->db, 'customer', true); + $this->checkFixture($this->db(), 'customer', true); - $customerQuery = new ActiveQuery(CustomerClosureField::class, $this->db); + $customerQuery = new ActiveQuery(CustomerClosureField::class); $customer = $customerQuery->findOne(1); $this->assertSame( diff --git a/tests/Driver/Oracle/BatchQueryResultTest.php b/tests/Driver/Oracle/BatchQueryResultTest.php index 6324c7dd6..c04d4afbf 100644 --- a/tests/Driver/Oracle/BatchQueryResultTest.php +++ b/tests/Driver/Oracle/BatchQueryResultTest.php @@ -7,31 +7,20 @@ use Yiisoft\ActiveRecord\ActiveQuery; use Yiisoft\ActiveRecord\Tests\Driver\Oracle\Stubs\Customer; use Yiisoft\ActiveRecord\Tests\Support\OracleHelper; +use Yiisoft\Db\Connection\ConnectionInterface; final class BatchQueryResultTest extends \Yiisoft\ActiveRecord\Tests\BatchQueryResultTest { - public function setUp(): void + protected function createConnection(): ConnectionInterface { - parent::setUp(); - - $oracleHelper = new OracleHelper(); - $this->db = $oracleHelper->createConnection(); - } - - protected function tearDown(): void - { - parent::tearDown(); - - $this->db->close(); - - unset($this->db); + return (new OracleHelper())->createConnection(); } public function testBatchWithIndexBy(): void { - $this->checkFixture($this->db, 'customer'); + $this->checkFixture($this->db(), 'customer'); - $customerQuery = new ActiveQuery(Customer::class, $this->db); + $customerQuery = new ActiveQuery(Customer::class); $query = $customerQuery->orderBy('id')->limit(3)->indexBy('id'); diff --git a/tests/Driver/Oracle/ConnectionProviderTest.php b/tests/Driver/Oracle/ConnectionProviderTest.php new file mode 100644 index 000000000..2f3713237 --- /dev/null +++ b/tests/Driver/Oracle/ConnectionProviderTest.php @@ -0,0 +1,16 @@ +createConnection(); + } +} diff --git a/tests/Driver/Oracle/MagicActiveRecordTest.php b/tests/Driver/Oracle/MagicActiveRecordTest.php index c2c95fd59..44c4e1e34 100644 --- a/tests/Driver/Oracle/MagicActiveRecordTest.php +++ b/tests/Driver/Oracle/MagicActiveRecordTest.php @@ -9,33 +9,22 @@ use Yiisoft\ActiveRecord\Tests\Stubs\MagicActiveRecord\CustomerClosureField; use Yiisoft\ActiveRecord\Tests\Stubs\MagicActiveRecord\Type; use Yiisoft\ActiveRecord\Tests\Support\OracleHelper; +use Yiisoft\Db\Connection\ConnectionInterface; final class MagicActiveRecordTest extends \Yiisoft\ActiveRecord\Tests\MagicActiveRecordTest { - public function setUp(): void + protected function createConnection(): ConnectionInterface { - parent::setUp(); - - $oracleHelper = new OracleHelper(); - $this->db = $oracleHelper->createConnection(); - } - - protected function tearDown(): void - { - parent::tearDown(); - - $this->db->close(); - - unset($this->db); + return (new OracleHelper())->createConnection(); } public function testCastValues(): void { $this->markTestSkipped('Cant bind floats without support from a custom PDO driver.'); - $this->checkFixture($this->db, 'customer'); + $this->checkFixture($this->db(), 'customer'); - $arClass = new Type($this->db); + $arClass = new Type(); $arClass->int_col = 123; $arClass->int_col2 = 456; $arClass->smallint_col = 42; @@ -49,7 +38,7 @@ public function testCastValues(): void $arClass->bool_col2 = 0; $arClass->save(); - $aqClass = new ActiveQuery(Type::class, $this->db); + $aqClass = new ActiveQuery(Type::class); $query = $aqClass->onePopulate(); $this->assertSame(123, $query->int_col); @@ -66,9 +55,9 @@ public function testCastValues(): void public function testDefaultValues(): void { - $this->checkFixture($this->db, 'customer'); + $this->checkFixture($this->db(), 'customer'); - $arClass = new Type($this->db); + $arClass = new Type(); $arClass->loadDefaultValues(); $this->assertEquals(1, $arClass->int_col2); $this->assertEquals('something', $arClass->char_col2); @@ -78,13 +67,13 @@ public function testDefaultValues(): void // not testing $arClass->time, because oci\Schema can't read default value - $arClass = new Type($this->db); + $arClass = new Type(); $arClass->char_col2 = 'not something'; $arClass->loadDefaultValues(); $this->assertEquals('not something', $arClass->char_col2); - $arClass = new Type($this->db); + $arClass = new Type(); $arClass->char_col2 = 'not something'; $arClass->loadDefaultValues(false); @@ -98,9 +87,9 @@ public function testDefaultValues(): void */ public function testBooleanAttribute(): void { - $this->checkFixture($this->db, 'customer', true); + $this->checkFixture($this->db(), 'customer', true); - $customer = new Customer($this->db); + $customer = new Customer(); $customer->name = 'boolean customer'; $customer->email = 'mail@example.com'; @@ -116,20 +105,20 @@ public function testBooleanAttribute(): void $customer->refresh(); $this->assertEquals('0', $customer->status); - $customerQuery = new ActiveQuery(Customer::class, $this->db); + $customerQuery = new ActiveQuery(Customer::class); $customers = $customerQuery->where(['status' => '1'])->all(); $this->assertCount(2, $customers); - $customerQuery = new ActiveQuery(Customer::class, $this->db); + $customerQuery = new ActiveQuery(Customer::class); $customers = $customerQuery->where(['status' => '0'])->all(); $this->assertCount(1, $customers); } public function testToArray(): void { - $this->checkFixture($this->db, 'customer', true); + $this->checkFixture($this->db(), 'customer', true); - $customerQuery = new ActiveQuery(Customer::class, $this->db); + $customerQuery = new ActiveQuery(Customer::class); $customer = $customerQuery->findOne(1); $this->assertSame( @@ -148,9 +137,9 @@ public function testToArray(): void public function testToArrayWithClosure(): void { - $this->checkFixture($this->db, 'customer', true); + $this->checkFixture($this->db(), 'customer', true); - $customerQuery = new ActiveQuery(CustomerClosureField::class, $this->db); + $customerQuery = new ActiveQuery(CustomerClosureField::class); $customer = $customerQuery->findOne(1); $this->assertSame( diff --git a/tests/Driver/Pgsql/ActiveQueryFindTest.php b/tests/Driver/Pgsql/ActiveQueryFindTest.php index 5da779ba8..e50bfb7da 100644 --- a/tests/Driver/Pgsql/ActiveQueryFindTest.php +++ b/tests/Driver/Pgsql/ActiveQueryFindTest.php @@ -7,32 +7,21 @@ use Yiisoft\ActiveRecord\ActiveQuery; use Yiisoft\ActiveRecord\Tests\Stubs\ActiveRecord\Customer; use Yiisoft\ActiveRecord\Tests\Support\PgsqlHelper; +use Yiisoft\Db\Connection\ConnectionInterface; final class ActiveQueryFindTest extends \Yiisoft\ActiveRecord\Tests\ActiveQueryFindTest { - public function setUp(): void + protected function createConnection(): ConnectionInterface { - parent::setUp(); - - $pgsqlHelper = new PgsqlHelper(); - $this->db = $pgsqlHelper->createConnection(); - } - - protected function tearDown(): void - { - parent::tearDown(); - - $this->db->close(); - - unset($this->db); + return (new PgsqlHelper())->createConnection(); } public function testFindAsArray(): void { - $this->checkFixture($this->db, 'customer', true); + $this->checkFixture($this->db(), 'customer', true); /** asArray */ - $customerQuery = new ActiveQuery(Customer::class, $this->db); + $customerQuery = new ActiveQuery(Customer::class); $customer = $customerQuery->where(['id' => 2])->asArray()->onePopulate(); $this->assertEquals([ 'id' => 2, @@ -45,7 +34,7 @@ public function testFindAsArray(): void ], $customer); /** find all asArray */ - $customerQuery = new ActiveQuery(Customer::class, $this->db); + $customerQuery = new ActiveQuery(Customer::class); $customers = $customerQuery->asArray()->all(); $this->assertCount(3, $customers); $this->assertArrayHasKey('id', $customers[0]); diff --git a/tests/Driver/Pgsql/ActiveQueryTest.php b/tests/Driver/Pgsql/ActiveQueryTest.php index 2474d0abd..5d4b13775 100644 --- a/tests/Driver/Pgsql/ActiveQueryTest.php +++ b/tests/Driver/Pgsql/ActiveQueryTest.php @@ -5,23 +5,12 @@ namespace Yiisoft\ActiveRecord\Tests\Driver\Pgsql; use Yiisoft\ActiveRecord\Tests\Support\PgsqlHelper; +use Yiisoft\Db\Connection\ConnectionInterface; final class ActiveQueryTest extends \Yiisoft\ActiveRecord\Tests\ActiveQueryTest { - public function setUp(): void + protected function createConnection(): ConnectionInterface { - parent::setUp(); - - $pgsqlHelper = new PgsqlHelper(); - $this->db = $pgsqlHelper->createConnection(); - } - - protected function tearDown(): void - { - parent::tearDown(); - - $this->db->close(); - - unset($this->db); + return (new PgsqlHelper())->createConnection(); } } diff --git a/tests/Driver/Pgsql/ActiveRecordFactoryTest.php b/tests/Driver/Pgsql/ActiveRecordFactoryTest.php deleted file mode 100644 index 064e1059e..000000000 --- a/tests/Driver/Pgsql/ActiveRecordFactoryTest.php +++ /dev/null @@ -1,28 +0,0 @@ -db = $pgsqlHelper->createConnection(); - $this->arFactory = $pgsqlHelper->createARFactory($this->db); - } - - protected function tearDown(): void - { - parent::tearDown(); - - $this->db->close(); - - unset($this->arFactory, $this->db); - } -} diff --git a/tests/Driver/Pgsql/ActiveRecordTest.php b/tests/Driver/Pgsql/ActiveRecordTest.php index 74707f18a..df91df2c9 100644 --- a/tests/Driver/Pgsql/ActiveRecordTest.php +++ b/tests/Driver/Pgsql/ActiveRecordTest.php @@ -16,6 +16,7 @@ use Yiisoft\ActiveRecord\Tests\Stubs\ActiveRecord\DefaultPk; use Yiisoft\ActiveRecord\Tests\Stubs\ActiveRecord\UserAR; use Yiisoft\ActiveRecord\Tests\Support\PgsqlHelper; +use Yiisoft\Db\Connection\ConnectionInterface; use Yiisoft\Db\Expression\ArrayExpression; use Yiisoft\Db\Expression\Expression; use Yiisoft\Db\Expression\JsonExpression; @@ -23,28 +24,16 @@ final class ActiveRecordTest extends \Yiisoft\ActiveRecord\Tests\ActiveRecordTest { - public function setUp(): void + protected function createConnection(): ConnectionInterface { - parent::setUp(); - - $pgsqlHelper = new PgsqlHelper(); - $this->db = $pgsqlHelper->createConnection(); - } - - protected function tearDown(): void - { - parent::tearDown(); - - $this->db->close(); - - unset($this->db); + return (new PgsqlHelper())->createConnection(); } public function testDefaultValues(): void { - $this->checkFixture($this->db, 'type'); + $this->checkFixture($this->db(), 'type'); - $arClass = new Type($this->db); + $arClass = new Type($this->db()); $arClass->loadDefaultValues(); @@ -55,13 +44,13 @@ public function testDefaultValues(): void $this->assertEquals(true, $arClass->bool_col2); $this->assertEquals('2002-01-01 00:00:00', $arClass->time); - $arClass = new Type($this->db); + $arClass = new Type($this->db()); $arClass->char_col2 = 'not something'; $arClass->loadDefaultValues(); $this->assertEquals('not something', $arClass->char_col2); - $arClass = new Type($this->db); + $arClass = new Type($this->db()); $arClass->char_col2 = 'not something'; $arClass->loadDefaultValues(false); @@ -70,9 +59,9 @@ public function testDefaultValues(): void public function testCastValues(): void { - $this->checkFixture($this->db, 'type'); + $this->checkFixture($this->db(), 'type'); - $arClass = new Type($this->db); + $arClass = new Type($this->db()); $arClass->int_col = 123; $arClass->int_col2 = 456; @@ -88,7 +77,7 @@ public function testCastValues(): void $arClass->save(); /** @var $model Type */ - $aqClass = new ActiveQuery(Type::class, $this->db); + $aqClass = new ActiveQuery(Type::class); $query = $aqClass->onePopulate(); $this->assertSame(123, $query->int_col); @@ -101,9 +90,9 @@ public function testCastValues(): void public function testExplicitPkOnAutoIncrement(): void { - $this->checkFixture($this->db, 'customer'); + $this->checkFixture($this->db(), 'customer'); - $customer = new Customer($this->db); + $customer = new Customer($this->db()); $customer->setId(1337); $customer->setEmail('user1337@example.com'); $customer->setName('user1337'); @@ -120,9 +109,9 @@ public function testExplicitPkOnAutoIncrement(): void */ public function testEagerLoadingUsingStringIdentifiers(): void { - $this->checkFixture($this->db, 'beta'); + $this->checkFixture($this->db(), 'beta'); - $betaQuery = new ActiveQuery(Beta::class, $this->db); + $betaQuery = new ActiveQuery(Beta::class); $betas = $betaQuery->with('alpha')->all(); $this->assertNotEmpty($betas); @@ -140,9 +129,9 @@ public function testEagerLoadingUsingStringIdentifiers(): void public function testBooleanAttribute(): void { - $this->checkFixture($this->db, 'customer', true); + $this->checkFixture($this->db(), 'customer', true); - $customer = new Customer($this->db); + $customer = new Customer($this->db()); $customer->setName('boolean customer'); $customer->setEmail('mail@example.com'); $customer->setBoolStatus(false); @@ -157,7 +146,7 @@ public function testBooleanAttribute(): void $customer->refresh(); $this->assertTrue($customer->getBoolStatus()); - $customerQuery = new ActiveQuery(Customer::class, $this->db); + $customerQuery = new ActiveQuery(Customer::class); $customers = $customerQuery->where(['bool_status' => true])->all(); $this->assertCount(3, $customers); @@ -167,11 +156,11 @@ public function testBooleanAttribute(): void public function testBooleanValues(): void { - $this->checkFixture($this->db, 'bool_values'); + $this->checkFixture($this->db(), 'bool_values'); - $command = $this->db->createCommand(); + $command = $this->db()->createCommand(); $command->insertBatch('bool_values', [[true], [false]], ['bool_col'])->execute(); - $boolARQuery = new ActiveQuery(BoolAR::class, $this->db); + $boolARQuery = new ActiveQuery(BoolAR::class); $this->assertTrue($boolARQuery->where(['bool_col' => true])->onePopulate()->bool_col); $this->assertFalse($boolARQuery->where(['bool_col' => false])->onePopulate()->bool_col); @@ -193,11 +182,11 @@ public function testBooleanValues(): void */ public function testBooleanValues2(): void { - $this->checkFixture($this->db, 'bool_user'); + $this->checkFixture($this->db(), 'bool_user'); - //$this->db->setCharset('utf8'); - $this->db->createCommand('DROP TABLE IF EXISTS bool_user;')->execute(); - $this->db->createCommand()->createTable('bool_user', [ + //$this->db()->setCharset('utf8'); + $this->db()->createCommand('DROP TABLE IF EXISTS bool_user;')->execute(); + $this->db()->createCommand()->createTable('bool_user', [ 'id' => SchemaPgsql::TYPE_PK, 'username' => SchemaPgsql::TYPE_STRING . ' NOT NULL', 'auth_key' => SchemaPgsql::TYPE_STRING . '(32) NOT NULL', @@ -209,13 +198,13 @@ public function testBooleanValues2(): void 'created_at' => SchemaPgsql::TYPE_INTEGER . ' NOT NULL', 'updated_at' => SchemaPgsql::TYPE_INTEGER . ' NOT NULL', ])->execute(); - $this->db->createCommand()->addColumn( + $this->db()->createCommand()->addColumn( 'bool_user', 'is_deleted', SchemaPgsql::TYPE_BOOLEAN . ' NOT NULL DEFAULT FALSE' )->execute(); - $user = new UserAR($this->db); + $user = new UserAR(); $user->username = 'test'; $user->auth_key = 'test'; $user->password_hash = 'test'; @@ -224,7 +213,7 @@ public function testBooleanValues2(): void $user->updated_at = time(); $user->save(); - $userQuery = new ActiveQuery(UserAR::class, $this->db); + $userQuery = new ActiveQuery(UserAR::class); $this->assertCount(1, $userQuery->where(['is_deleted' => false])->all()); $this->assertCount(0, $userQuery->where(['is_deleted' => true])->all()); $this->assertCount(1, $userQuery->where(['is_deleted' => [true, false]])->all()); @@ -232,9 +221,9 @@ public function testBooleanValues2(): void public function testBooleanDefaultValues(): void { - $this->checkFixture($this->db, 'bool_values'); + $this->checkFixture($this->db(), 'bool_values'); - $arClass = new BoolAR($this->db); + $arClass = new BoolAR(); $this->assertNull($arClass->bool_col); $this->assertTrue($arClass->default_true); @@ -250,9 +239,9 @@ public function testBooleanDefaultValues(): void public function testPrimaryKeyAfterSave(): void { - $this->checkFixture($this->db, 'default_pk'); + $this->checkFixture($this->db(), 'default_pk'); - $record = new DefaultPk($this->db); + $record = new DefaultPk(); $record->type = 'type'; @@ -358,9 +347,9 @@ public static function arrayValuesProvider(): array */ public function testArrayValues($attributes): void { - $this->checkFixture($this->db, 'array_and_json_types', true); + $this->checkFixture($this->db(), 'array_and_json_types', true); - $type = new ArrayAndJsonTypes($this->db); + $type = new ArrayAndJsonTypes(); foreach ($attributes as $attribute => $expected) { $type->setAttribute($attribute, $expected[0]); @@ -368,7 +357,7 @@ public function testArrayValues($attributes): void $type->save(); - $typeQuery = new ActiveQuery($type::class, $this->db); + $typeQuery = new ActiveQuery($type::class); $type = $typeQuery->onePopulate(); @@ -402,9 +391,9 @@ public function testArrayValues($attributes): void public function testToArray(): void { - $this->checkFixture($this->db, 'customer', true); + $this->checkFixture($this->db(), 'customer', true); - $customerQuery = new ActiveQuery(Customer::class, $this->db); + $customerQuery = new ActiveQuery(Customer::class); $customer = $customerQuery->findOne(1); $this->assertSame( @@ -423,9 +412,9 @@ public function testToArray(): void public function testToArrayWithClosure(): void { - $this->checkFixture($this->db, 'customer', true); + $this->checkFixture($this->db(), 'customer', true); - $customerQuery = new ActiveQuery(CustomerClosureField::class, $this->db); + $customerQuery = new ActiveQuery(CustomerClosureField::class); $customer = $customerQuery->findOne(1); $this->assertSame( diff --git a/tests/Driver/Pgsql/BatchQueryResultTest.php b/tests/Driver/Pgsql/BatchQueryResultTest.php index 827a18ea3..2af09578f 100644 --- a/tests/Driver/Pgsql/BatchQueryResultTest.php +++ b/tests/Driver/Pgsql/BatchQueryResultTest.php @@ -5,23 +5,12 @@ namespace Yiisoft\ActiveRecord\Tests\Driver\Pgsql; use Yiisoft\ActiveRecord\Tests\Support\PgsqlHelper; +use Yiisoft\Db\Connection\ConnectionInterface; final class BatchQueryResultTest extends \Yiisoft\ActiveRecord\Tests\BatchQueryResultTest { - public function setUp(): void + protected function createConnection(): ConnectionInterface { - parent::setUp(); - - $pgsqlHelper = new PgsqlHelper(); - $this->db = $pgsqlHelper->createConnection(); - } - - protected function tearDown(): void - { - parent::tearDown(); - - $this->db->close(); - - unset($this->db); + return (new PgsqlHelper())->createConnection(); } } diff --git a/tests/Driver/Pgsql/ConnectionProviderTest.php b/tests/Driver/Pgsql/ConnectionProviderTest.php new file mode 100644 index 000000000..84b67503c --- /dev/null +++ b/tests/Driver/Pgsql/ConnectionProviderTest.php @@ -0,0 +1,16 @@ +createConnection(); + } +} diff --git a/tests/Driver/Pgsql/MagicActiveRecordTest.php b/tests/Driver/Pgsql/MagicActiveRecordTest.php index 20821bcb9..d14936646 100644 --- a/tests/Driver/Pgsql/MagicActiveRecordTest.php +++ b/tests/Driver/Pgsql/MagicActiveRecordTest.php @@ -15,6 +15,7 @@ use Yiisoft\ActiveRecord\Tests\Stubs\MagicActiveRecord\DefaultPk; use Yiisoft\ActiveRecord\Tests\Stubs\MagicActiveRecord\UserAR; use Yiisoft\ActiveRecord\Tests\Support\PgsqlHelper; +use Yiisoft\Db\Connection\ConnectionInterface; use Yiisoft\Db\Expression\ArrayExpression; use Yiisoft\Db\Expression\Expression; use Yiisoft\Db\Expression\JsonExpression; @@ -22,28 +23,16 @@ final class MagicActiveRecordTest extends \Yiisoft\ActiveRecord\Tests\MagicActiveRecordTest { - public function setUp(): void + protected function createConnection(): ConnectionInterface { - parent::setUp(); - - $pgsqlHelper = new PgsqlHelper(); - $this->db = $pgsqlHelper->createConnection(); - } - - protected function tearDown(): void - { - parent::tearDown(); - - $this->db->close(); - - unset($this->db); + return (new PgsqlHelper())->createConnection(); } public function testExplicitPkOnAutoIncrement(): void { - $this->checkFixture($this->db, 'customer'); + $this->checkFixture($this->db(), 'customer'); - $customer = new Customer($this->db); + $customer = new Customer(); $customer->id = 1337; $customer->email = 'user1337@example.com'; $customer->name = 'user1337'; @@ -60,9 +49,9 @@ public function testExplicitPkOnAutoIncrement(): void */ public function testEagerLoadingUsingStringIdentifiers(): void { - $this->checkFixture($this->db, 'beta'); + $this->checkFixture($this->db(), 'beta'); - $betaQuery = new ActiveQuery(Beta::class, $this->db); + $betaQuery = new ActiveQuery(Beta::class); $betas = $betaQuery->with('alpha')->all(); $this->assertNotEmpty($betas); @@ -80,9 +69,9 @@ public function testEagerLoadingUsingStringIdentifiers(): void public function testBooleanAttribute(): void { - $this->checkFixture($this->db, 'customer', true); + $this->checkFixture($this->db(), 'customer', true); - $customer = new Customer($this->db); + $customer = new Customer(); $customer->name = 'boolean customer'; $customer->email = 'mail@example.com'; $customer->bool_status = false; @@ -97,7 +86,7 @@ public function testBooleanAttribute(): void $customer->refresh(); $this->assertTrue($customer->bool_status); - $customerQuery = new ActiveQuery(Customer::class, $this->db); + $customerQuery = new ActiveQuery(Customer::class); $customers = $customerQuery->where(['bool_status' => true])->all(); $this->assertCount(3, $customers); @@ -107,11 +96,11 @@ public function testBooleanAttribute(): void public function testBooleanValues(): void { - $this->checkFixture($this->db, 'bool_values'); + $this->checkFixture($this->db(), 'bool_values'); - $command = $this->db->createCommand(); + $command = $this->db()->createCommand(); $command->batchInsert('bool_values', ['bool_col'], [[true], [false]])->execute(); - $boolARQuery = new ActiveQuery(BoolAR::class, $this->db); + $boolARQuery = new ActiveQuery(BoolAR::class); $this->assertTrue($boolARQuery->where(['bool_col' => true])->onePopulate()->bool_col); $this->assertFalse($boolARQuery->where(['bool_col' => false])->onePopulate()->bool_col); @@ -133,11 +122,11 @@ public function testBooleanValues(): void */ public function testBooleanValues2(): void { - $this->checkFixture($this->db, 'bool_user'); + $this->checkFixture($this->db(), 'bool_user'); - //$this->db->setCharset('utf8'); - $this->db->createCommand('DROP TABLE IF EXISTS bool_user;')->execute(); - $this->db->createCommand()->createTable('bool_user', [ + //$this->db()->setCharset('utf8'); + $this->db()->createCommand('DROP TABLE IF EXISTS bool_user;')->execute(); + $this->db()->createCommand()->createTable('bool_user', [ 'id' => SchemaPgsql::TYPE_PK, 'username' => SchemaPgsql::TYPE_STRING . ' NOT NULL', 'auth_key' => SchemaPgsql::TYPE_STRING . '(32) NOT NULL', @@ -149,13 +138,13 @@ public function testBooleanValues2(): void 'created_at' => SchemaPgsql::TYPE_INTEGER . ' NOT NULL', 'updated_at' => SchemaPgsql::TYPE_INTEGER . ' NOT NULL', ])->execute(); - $this->db->createCommand()->addColumn( + $this->db()->createCommand()->addColumn( 'bool_user', 'is_deleted', SchemaPgsql::TYPE_BOOLEAN . ' NOT NULL DEFAULT FALSE' )->execute(); - $user = new UserAR($this->db); + $user = new UserAR(); $user->username = 'test'; $user->auth_key = 'test'; $user->password_hash = 'test'; @@ -164,7 +153,7 @@ public function testBooleanValues2(): void $user->updated_at = time(); $user->save(); - $userQuery = new ActiveQuery(UserAR::class, $this->db); + $userQuery = new ActiveQuery(UserAR::class); $this->assertCount(1, $userQuery->where(['is_deleted' => false])->all()); $this->assertCount(0, $userQuery->where(['is_deleted' => true])->all()); $this->assertCount(1, $userQuery->where(['is_deleted' => [true, false]])->all()); @@ -172,9 +161,9 @@ public function testBooleanValues2(): void public function testBooleanDefaultValues(): void { - $this->checkFixture($this->db, 'bool_values'); + $this->checkFixture($this->db(), 'bool_values'); - $arClass = new BoolAR($this->db); + $arClass = new BoolAR(); $this->assertNull($arClass->bool_col); $this->assertNull($arClass->default_true); @@ -190,9 +179,9 @@ public function testBooleanDefaultValues(): void public function testPrimaryKeyAfterSave(): void { - $this->checkFixture($this->db, 'default_pk'); + $this->checkFixture($this->db(), 'default_pk'); - $record = new DefaultPk($this->db); + $record = new DefaultPk(); $record->type = 'type'; @@ -298,9 +287,9 @@ public static function arrayValuesProvider(): array */ public function testArrayValues($attributes): void { - $this->checkFixture($this->db, 'array_and_json_types', true); + $this->checkFixture($this->db(), 'array_and_json_types', true); - $type = new ArrayAndJsonTypes($this->db); + $type = new ArrayAndJsonTypes(); foreach ($attributes as $attribute => $expected) { $type->$attribute = $expected[0]; @@ -308,7 +297,7 @@ public function testArrayValues($attributes): void $type->save(); - $typeQuery = new ActiveQuery($type::class, $this->db); + $typeQuery = new ActiveQuery($type::class); $type = $typeQuery->onePopulate(); @@ -342,9 +331,9 @@ public function testArrayValues($attributes): void public function testToArray(): void { - $this->checkFixture($this->db, 'customer', true); + $this->checkFixture($this->db(), 'customer', true); - $customerQuery = new ActiveQuery(Customer::class, $this->db); + $customerQuery = new ActiveQuery(Customer::class); $customer = $customerQuery->findOne(1); $this->assertSame( @@ -363,9 +352,9 @@ public function testToArray(): void public function testToArrayWithClosure(): void { - $this->checkFixture($this->db, 'customer', true); + $this->checkFixture($this->db(), 'customer', true); - $customerQuery = new ActiveQuery(CustomerClosureField::class, $this->db); + $customerQuery = new ActiveQuery(CustomerClosureField::class); $customer = $customerQuery->findOne(1); $this->assertSame( diff --git a/tests/Driver/Sqlite/ActiveQueryFindTest.php b/tests/Driver/Sqlite/ActiveQueryFindTest.php index 145b420f1..9a3c68215 100644 --- a/tests/Driver/Sqlite/ActiveQueryFindTest.php +++ b/tests/Driver/Sqlite/ActiveQueryFindTest.php @@ -5,23 +5,12 @@ namespace Yiisoft\ActiveRecord\Tests\Driver\Sqlite; use Yiisoft\ActiveRecord\Tests\Support\SqliteHelper; +use Yiisoft\Db\Connection\ConnectionInterface; final class ActiveQueryFindTest extends \Yiisoft\ActiveRecord\Tests\ActiveQueryFindTest { - public function setUp(): void + protected function createConnection(): ConnectionInterface { - parent::setUp(); - - $sqliteHelper = new SqliteHelper(); - $this->db = $sqliteHelper->createConnection(); - } - - protected function tearDown(): void - { - parent::tearDown(); - - $this->db->close(); - - unset($this->db); + return (new SqliteHelper())->createConnection(); } } diff --git a/tests/Driver/Sqlite/ActiveQueryTest.php b/tests/Driver/Sqlite/ActiveQueryTest.php index 6cdf47094..651b55946 100644 --- a/tests/Driver/Sqlite/ActiveQueryTest.php +++ b/tests/Driver/Sqlite/ActiveQueryTest.php @@ -5,23 +5,12 @@ namespace Yiisoft\ActiveRecord\Tests\Driver\Sqlite; use Yiisoft\ActiveRecord\Tests\Support\SqliteHelper; +use Yiisoft\Db\Connection\ConnectionInterface; final class ActiveQueryTest extends \Yiisoft\ActiveRecord\Tests\ActiveQueryTest { - public function setUp(): void + protected function createConnection(): ConnectionInterface { - parent::setUp(); - - $pgsqlHelper = new SqliteHelper(); - $this->db = $pgsqlHelper->createConnection(); - } - - protected function tearDown(): void - { - parent::tearDown(); - - $this->db->close(); - - unset($this->db); + return (new SqliteHelper())->createConnection(); } } diff --git a/tests/Driver/Sqlite/ActiveRecordFactoryTest.php b/tests/Driver/Sqlite/ActiveRecordFactoryTest.php deleted file mode 100644 index 3ce4995e3..000000000 --- a/tests/Driver/Sqlite/ActiveRecordFactoryTest.php +++ /dev/null @@ -1,28 +0,0 @@ -db = $sqliteHelper->createConnection(); - $this->arFactory = $sqliteHelper->createARFactory($this->db); - } - - protected function tearDown(): void - { - parent::tearDown(); - - $this->db->close(); - - unset($this->arFactory, $this->db); - } -} diff --git a/tests/Driver/Sqlite/ActiveRecordTest.php b/tests/Driver/Sqlite/ActiveRecordTest.php index 0d4ecb520..f9da98db3 100644 --- a/tests/Driver/Sqlite/ActiveRecordTest.php +++ b/tests/Driver/Sqlite/ActiveRecordTest.php @@ -8,31 +8,20 @@ use Yiisoft\ActiveRecord\Tests\Stubs\ActiveRecord\Beta; use Yiisoft\ActiveRecord\Tests\Stubs\ActiveRecord\Customer; use Yiisoft\ActiveRecord\Tests\Support\SqliteHelper; +use Yiisoft\Db\Connection\ConnectionInterface; final class ActiveRecordTest extends \Yiisoft\ActiveRecord\Tests\ActiveRecordTest { - public function setUp(): void + protected function createConnection(): ConnectionInterface { - parent::setUp(); - - $sqliteHelper = new SqliteHelper(); - $this->db = $sqliteHelper->createConnection(); - } - - protected function tearDown(): void - { - parent::tearDown(); - - $this->db->close(); - - unset($this->db); + return (new SqliteHelper())->createConnection(); } public function testExplicitPkOnAutoIncrement(): void { - $this->checkFixture($this->db, 'customer', true); + $this->checkFixture($this->db(), 'customer', true); - $customer = new Customer($this->db); + $customer = new Customer(); $customer->setId(1337); $customer->setEmail('user1337@example.com'); @@ -51,9 +40,9 @@ public function testExplicitPkOnAutoIncrement(): void */ public function testEagerLoadingUsingStringIdentifiers(): void { - $this->checkFixture($this->db, 'beta'); + $this->checkFixture($this->db(), 'beta'); - $betaQuery = new ActiveQuery(Beta::class, $this->db); + $betaQuery = new ActiveQuery(Beta::class); $betas = $betaQuery->with('alpha')->all(); diff --git a/tests/Driver/Sqlite/BatchQueryResultTest.php b/tests/Driver/Sqlite/BatchQueryResultTest.php index 53998858d..b28642dfa 100644 --- a/tests/Driver/Sqlite/BatchQueryResultTest.php +++ b/tests/Driver/Sqlite/BatchQueryResultTest.php @@ -5,23 +5,12 @@ namespace Yiisoft\ActiveRecord\Tests\Driver\Sqlite; use Yiisoft\ActiveRecord\Tests\Support\SqliteHelper; +use Yiisoft\Db\Connection\ConnectionInterface; final class BatchQueryResultTest extends \Yiisoft\ActiveRecord\Tests\BatchQueryResultTest { - public function setUp(): void + protected function createConnection(): ConnectionInterface { - parent::setUp(); - - $sqliteHelper = new SqliteHelper(); - $this->db = $sqliteHelper->createConnection(); - } - - protected function tearDown(): void - { - parent::tearDown(); - - $this->db->close(); - - unset($this->db); + return (new SqliteHelper())->createConnection(); } } diff --git a/tests/Driver/Sqlite/ConnectionProviderTest.php b/tests/Driver/Sqlite/ConnectionProviderTest.php new file mode 100644 index 000000000..9803721cb --- /dev/null +++ b/tests/Driver/Sqlite/ConnectionProviderTest.php @@ -0,0 +1,16 @@ +createConnection(); + } +} diff --git a/tests/Driver/Sqlite/MagicActiveRecordTest.php b/tests/Driver/Sqlite/MagicActiveRecordTest.php index fa8dabbd7..a198cda0c 100644 --- a/tests/Driver/Sqlite/MagicActiveRecordTest.php +++ b/tests/Driver/Sqlite/MagicActiveRecordTest.php @@ -8,31 +8,20 @@ use Yiisoft\ActiveRecord\Tests\Stubs\MagicActiveRecord\Beta; use Yiisoft\ActiveRecord\Tests\Stubs\MagicActiveRecord\Customer; use Yiisoft\ActiveRecord\Tests\Support\SqliteHelper; +use Yiisoft\Db\Connection\ConnectionInterface; final class MagicActiveRecordTest extends \Yiisoft\ActiveRecord\Tests\MagicActiveRecordTest { - public function setUp(): void + protected function createConnection(): ConnectionInterface { - parent::setUp(); - - $sqliteHelper = new SqliteHelper(); - $this->db = $sqliteHelper->createConnection(); - } - - protected function tearDown(): void - { - parent::tearDown(); - - $this->db->close(); - - unset($this->db); + return (new SqliteHelper())->createConnection(); } public function testExplicitPkOnAutoIncrement(): void { - $this->checkFixture($this->db, 'customer', true); + $this->checkFixture($this->db(), 'customer', true); - $customer = new Customer($this->db); + $customer = new Customer(); $customer->id = 1337; $customer->email = 'user1337@example.com'; @@ -51,9 +40,9 @@ public function testExplicitPkOnAutoIncrement(): void */ public function testEagerLoadingUsingStringIdentifiers(): void { - $this->checkFixture($this->db, 'beta'); + $this->checkFixture($this->db(), 'beta'); - $betaQuery = new ActiveQuery(Beta::class, $this->db); + $betaQuery = new ActiveQuery(Beta::class); $betas = $betaQuery->with('alpha')->all(); diff --git a/tests/MagicActiveRecordTest.php b/tests/MagicActiveRecordTest.php index a01177190..daa91778a 100644 --- a/tests/MagicActiveRecordTest.php +++ b/tests/MagicActiveRecordTest.php @@ -34,9 +34,9 @@ abstract class MagicActiveRecordTest extends TestCase { public function testStoreNull(): void { - $this->checkFixture($this->db, 'null_values', true); + $this->checkFixture($this->db(), 'null_values', true); - $record = new NullValues($this->db); + $record = new NullValues(); $this->assertNull($record->getAttribute('var1')); $this->assertNull($record->getAttribute('var2')); @@ -82,9 +82,9 @@ public function testStoreNull(): void public function testStoreEmpty(): void { - $this->checkFixture($this->db, 'null_values'); + $this->checkFixture($this->db(), 'null_values'); - $record = new NullValues($this->db); + $record = new NullValues(); /** this is to simulate empty html form submission */ $record->var1 = ''; @@ -102,10 +102,10 @@ public function testStoreEmpty(): void public function testIsPrimaryKey(): void { - $this->checkFixture($this->db, 'customer'); + $this->checkFixture($this->db(), 'customer'); - $customer = new Customer($this->db); - $orderItem = new OrderItem($this->db); + $customer = new Customer(); + $orderItem = new OrderItem(); $this->assertTrue($customer->isPrimaryKey(['id'])); $this->assertFalse($customer->isPrimaryKey([])); @@ -124,9 +124,9 @@ public function testIsPrimaryKey(): void public function testOutdatedRelationsAreResetForNewRecords(): void { - $this->checkFixture($this->db, 'order_item'); + $this->checkFixture($this->db(), 'order_item'); - $orderItem = new OrderItem($this->db); + $orderItem = new OrderItem(); $orderItem->order_id = 1; $orderItem->item_id = 3; @@ -148,9 +148,9 @@ public function testOutdatedRelationsAreResetForNewRecords(): void public function testDefaultValues(): void { - $this->checkFixture($this->db, 'type'); + $this->checkFixture($this->db(), 'type'); - $arClass = new Type($this->db); + $arClass = new Type(); $arClass->loadDefaultValues(); @@ -161,13 +161,13 @@ public function testDefaultValues(): void $this->assertEquals(true, $arClass->bool_col2); $this->assertEquals('2002-01-01 00:00:00', $arClass->time); - $arClass = new Type($this->db); + $arClass = new Type(); $arClass->char_col2 = 'not something'; $arClass->loadDefaultValues(); $this->assertEquals('not something', $arClass->char_col2); - $arClass = new Type($this->db); + $arClass = new Type(); $arClass->char_col2 = 'not something'; $arClass->loadDefaultValues(false); @@ -176,9 +176,9 @@ public function testDefaultValues(): void public function testCastValues(): void { - $this->checkFixture($this->db, 'type'); + $this->checkFixture($this->db(), 'type'); - $arClass = new Type($this->db); + $arClass = new Type(); $arClass->int_col = 123; $arClass->int_col2 = 456; @@ -194,7 +194,7 @@ public function testCastValues(): void $arClass->save(); /** @var $model Type */ - $aqClass = new ActiveQuery(Type::class, $this->db); + $aqClass = new ActiveQuery(Type::class); $query = $aqClass->onePopulate(); $this->assertSame(123, $query->int_col); @@ -207,15 +207,15 @@ public function testCastValues(): void public function testPopulateRecordCallWhenQueryingOnParentClass(): void { - $this->checkFixture($this->db, 'cat'); + $this->checkFixture($this->db(), 'cat'); - $cat = new Cat($this->db); + $cat = new Cat(); $cat->save(); - $dog = new Dog($this->db); + $dog = new Dog(); $dog->save(); - $animal = new ActiveQuery(Animal::class, $this->db); + $animal = new ActiveQuery(Animal::class); $animals = $animal->where(['type' => Dog::class])->onePopulate(); $this->assertEquals('bark', $animals->getDoes()); @@ -226,9 +226,9 @@ public function testPopulateRecordCallWhenQueryingOnParentClass(): void public function testSaveEmpty(): void { - $this->checkFixture($this->db, 'null_values', true); + $this->checkFixture($this->db(), 'null_values', true); - $record = new NullValues($this->db); + $record = new NullValues(); $this->assertTrue($record->save()); $this->assertEquals(1, $record->id); @@ -239,9 +239,9 @@ public function testSaveEmpty(): void */ public function testNoTablenameReplacement(): void { - $this->checkFixture($this->db, 'customer'); + $this->checkFixture($this->db(), 'customer'); - $customer = new Customer($this->db); + $customer = new Customer(); $customer->name = 'Some {{weird}} name'; $customer->email = 'test@example.com'; @@ -288,9 +288,9 @@ public function testLegalValuesForFindByCondition( array $validFilter, ?string $alias = null ): void { - $this->checkFixture($this->db, 'customer'); + $this->checkFixture($this->db(), 'customer'); - $activeQuery = new ActiveQuery($modelClassName, $this->db); + $activeQuery = new ActiveQuery($modelClassName); if ($alias !== null) { $activeQuery->alias('csr'); @@ -300,7 +300,7 @@ public function testLegalValuesForFindByCondition( $query = Assert::invokeMethod($activeQuery, 'findByCondition', [$validFilter]); - $this->db->getQueryBuilder()->build($query); + $this->db()->getQueryBuilder()->build($query); $this->assertTrue(true); } @@ -341,25 +341,25 @@ public static function illegalValuesForFindByCondition(): array */ public function testValueEscapingInFindByCondition(string $modelClassName, array $filterWithInjection): void { - $this->checkFixture($this->db, 'customer'); + $this->checkFixture($this->db(), 'customer'); $this->expectException(InvalidArgumentException::class); $this->expectExceptionMessageMatches( '/^Key "(.+)?" is not a column name and can not be used as a filter$/' ); - $query = new ActiveQuery($modelClassName, $this->db); + $query = new ActiveQuery($modelClassName); /** @var Query $query */ $query = Assert::invokeMethod($query, 'findByCondition', $filterWithInjection); - $this->db->getQueryBuilder()->build($query); + $this->db()->getQueryBuilder()->build($query); } public function testRefreshQuerySetAliasFindRecord(): void { - $this->checkFixture($this->db, 'customer'); + $this->checkFixture($this->db(), 'customer'); - $customer = new CustomerWithAlias($this->db); + $customer = new CustomerWithAlias(); $customer->id = 1; $customer->refresh(); @@ -369,15 +369,15 @@ public function testRefreshQuerySetAliasFindRecord(): void public function testResetNotSavedRelation(): void { - $this->checkFixture($this->db, 'order'); + $this->checkFixture($this->db(), 'order'); - $order = new Order($this->db); + $order = new Order(); $order->customer_id = 1; $order->created_at = 1_325_502_201; $order->total = 0; - $orderItem = new OrderItem($this->db); + $orderItem = new OrderItem(); $order->orderItems; @@ -390,9 +390,9 @@ public function testResetNotSavedRelation(): void public function testIssetException(): void { - $this->checkFixture($this->db, 'cat'); + $this->checkFixture($this->db(), 'cat'); - $cat = new Cat($this->db); + $cat = new Cat(); $this->expectException(Exception::class); isset($cat->exception); @@ -400,9 +400,9 @@ public function testIssetException(): void public function testIssetThrowable(): void { - $this->checkFixture($this->db, 'cat'); + $this->checkFixture($this->db(), 'cat'); - $cat = new Cat($this->db); + $cat = new Cat(); $this->expectException(DivisionByZeroError::class); isset($cat->throwable); @@ -410,9 +410,9 @@ public function testIssetThrowable(): void public function testIssetNonExisting(): void { - $this->checkFixture($this->db, 'cat'); + $this->checkFixture($this->db(), 'cat'); - $cat = new Cat($this->db); + $cat = new Cat(); $this->assertFalse(isset($cat->non_existing)); $this->assertFalse(isset($cat->non_existing_property)); @@ -421,20 +421,20 @@ public function testIssetNonExisting(): void public function testSetAttributes(): void { $attributes = []; - $this->checkFixture($this->db, 'customer'); + $this->checkFixture($this->db(), 'customer'); $attributes['email'] = 'samdark@mail.ru'; $attributes['name'] = 'samdark'; $attributes['address'] = 'rusia'; $attributes['status'] = 1; - if ($this->db->getDriverName() === 'pgsql') { + if ($this->db()->getDriverName() === 'pgsql') { $attributes['bool_status'] = true; } $attributes['profile_id'] = null; - $customer = new Customer($this->db); + $customer = new Customer(); $customer->setAttributes($attributes); @@ -443,9 +443,9 @@ public function testSetAttributes(): void public function testSetAttributeNoExist(): void { - $this->checkFixture($this->db, 'cat'); + $this->checkFixture($this->db(), 'cat'); - $cat = new Cat($this->db); + $cat = new Cat(); $this->expectException(InvalidArgumentException::class); $this->expectExceptionMessage( @@ -457,9 +457,9 @@ public function testSetAttributeNoExist(): void public function testSetOldAttribute(): void { - $this->checkFixture($this->db, 'customer'); + $this->checkFixture($this->db(), 'customer'); - $customer = new Customer($this->db); + $customer = new Customer(); $this->assertEmpty($customer->getOldAttribute('name')); @@ -470,9 +470,9 @@ public function testSetOldAttribute(): void public function testSetOldAttributeException(): void { - $this->checkFixture($this->db, 'customer'); + $this->checkFixture($this->db(), 'customer'); - $customer = new Customer($this->db); + $customer = new Customer(); $this->assertEmpty($customer->getOldAttribute('name')); @@ -485,9 +485,9 @@ public function testSetOldAttributeException(): void public function testIsAttributeChangedNotChanged(): void { - $this->checkFixture($this->db, 'customer'); + $this->checkFixture($this->db(), 'customer'); - $customer = new Customer($this->db); + $customer = new Customer(); $this->assertEmpty($customer->getAttribute('name')); $this->assertEmpty($customer->getOldAttribute('name')); @@ -496,7 +496,7 @@ public function testIsAttributeChangedNotChanged(): void public function testTableSchemaException(): void { - $noExist = new NoExist($this->db); + $noExist = new NoExist(); $this->expectException(InvalidConfigException::class); $this->expectExceptionMessage('The table does not exist: NoExist'); @@ -505,9 +505,9 @@ public function testTableSchemaException(): void public function testInsert(): void { - $this->checkFixture($this->db, 'customer'); + $this->checkFixture($this->db(), 'customer'); - $customer = new Customer($this->db); + $customer = new Customer(); $customer->email = 'user4@example.com'; $customer->name = 'user4'; @@ -529,9 +529,9 @@ public function testInsert(): void */ public function testBooleanAttribute(): void { - $this->checkFixture($this->db, 'customer', true); + $this->checkFixture($this->db(), 'customer', true); - $customer = new Customer($this->db); + $customer = new Customer(); $customer->name = 'boolean customer'; $customer->email = 'mail@example.com'; @@ -547,20 +547,20 @@ public function testBooleanAttribute(): void $customer->refresh(); $this->assertEquals(0, $customer->status); - $customerQuery = new ActiveQuery(Customer::class, $this->db); + $customerQuery = new ActiveQuery(Customer::class); $customers = $customerQuery->where(['status' => true])->all(); $this->assertCount(2, $customers); - $customerQuery = new ActiveQuery(Customer::class, $this->db); + $customerQuery = new ActiveQuery(Customer::class); $customers = $customerQuery->where(['status' => false])->all(); $this->assertCount(1, $customers); } public function testAttributeAccess(): void { - $this->checkFixture($this->db, 'customer'); + $this->checkFixture($this->db(), 'customer'); - $arClass = new Customer($this->db); + $arClass = new Customer(); $this->assertTrue($arClass->canSetProperty('name')); $this->assertTrue($arClass->canGetProperty('name')); @@ -574,10 +574,10 @@ public function testAttributeAccess(): void $this->assertNull($arClass->name); /** {@see https://github.com/yiisoft/yii2-gii/issues/190} */ - $baseModel = new Customer($this->db); + $baseModel = new Customer(); $this->assertFalse($baseModel->hasProperty('unExistingColumn')); - $customer = new Customer($this->db); + $customer = new Customer(); $this->assertInstanceOf(Customer::class, $customer); $this->assertTrue($customer->canGetProperty('id')); $this->assertTrue($customer->canSetProperty('id')); @@ -600,7 +600,7 @@ public function testAttributeAccess(): void $this->expectException(InvalidCallException::class); $this->expectExceptionMessage('Setting read-only property: ' . Customer::class . '::orderItems'); - $customer->orderItems = [new Item($this->db)]; + $customer->orderItems = [new Item()]; /** related attribute $customer->orderItems didn't change cause it's read-only */ $this->assertSame([], $customer->orderItems); @@ -614,15 +614,15 @@ public function testAttributeAccess(): void public function testHasAttribute(): void { - $this->checkFixture($this->db, 'customer'); + $this->checkFixture($this->db(), 'customer'); - $customer = new Customer($this->db); + $customer = new Customer(); $this->assertTrue($customer->hasAttribute('id')); $this->assertTrue($customer->hasAttribute('email')); $this->assertFalse($customer->hasAttribute('notExist')); - $customerQuery = new ActiveQuery(Customer::class, $this->db); + $customerQuery = new ActiveQuery(Customer::class); $customer = $customerQuery->findOne(1); $this->assertTrue($customer->hasAttribute('id')); $this->assertTrue($customer->hasAttribute('email')); @@ -631,13 +631,13 @@ public function testHasAttribute(): void public function testRefresh(): void { - $this->checkFixture($this->db, 'customer'); + $this->checkFixture($this->db(), 'customer'); - $customer = new Customer($this->db); + $customer = new Customer(); $this->assertFalse($customer->refresh()); - $customerQuery = new ActiveQuery(Customer::class, $this->db); + $customerQuery = new ActiveQuery(Customer::class); $customer = $customerQuery->findOne(1); $customer->name = 'to be refreshed'; @@ -647,14 +647,14 @@ public function testRefresh(): void public function testEquals(): void { - $this->checkFixture($this->db, 'customer'); + $this->checkFixture($this->db(), 'customer'); - $customerA = new Customer($this->db); - $customerB = new Customer($this->db); + $customerA = new Customer(); + $customerB = new Customer(); $this->assertFalse($customerA->equals($customerB)); - $customerA = new Customer($this->db); - $customerB = new Item($this->db); + $customerA = new Customer(); + $customerB = new Item(); $this->assertFalse($customerA->equals($customerB)); } @@ -673,10 +673,10 @@ public static function providerForUnlinkDelete() */ public function testUnlinkWithViaOnCondition($delete, $count) { - $this->checkFixture($this->db, 'order', true); - $this->checkFixture($this->db, 'order_item_with_null_fk', true); + $this->checkFixture($this->db(), 'order', true); + $this->checkFixture($this->db(), 'order_item_with_null_fk', true); - $orderQuery = new ActiveQuery(Order::class, $this->db); + $orderQuery = new ActiveQuery(Order::class); $order = $orderQuery->findOne(2); $this->assertCount(1, $order->itemsFor8); @@ -686,7 +686,7 @@ public function testUnlinkWithViaOnCondition($delete, $count) $this->assertCount(0, $order->itemsFor8); $this->assertCount(2, $order->orderItemsWithNullFK); - $orderItemQuery = new ActiveQuery(OrderItemWithNullFK::class, $this->db); + $orderItemQuery = new ActiveQuery(OrderItemWithNullFK::class); $this->assertCount(1, $orderItemQuery->findAll([ 'order_id' => 2, 'item_id' => 5, @@ -699,9 +699,9 @@ public function testUnlinkWithViaOnCondition($delete, $count) public function testVirtualRelation() { - $this->checkFixture($this->db, 'order', true); + $this->checkFixture($this->db(), 'order', true); - $orderQuery = new ActiveQuery(Order::class, $this->db); + $orderQuery = new ActiveQuery(Order::class); /** @var Order $order */ $order = $orderQuery->findOne(2); @@ -716,16 +716,16 @@ public function testVirtualRelation() */ public function testJoinWithEager() { - $this->checkFixture($this->db, 'customer', true); + $this->checkFixture($this->db(), 'customer', true); - $customerQuery = new ActiveQuery(Customer::class, $this->db); + $customerQuery = new ActiveQuery(Customer::class); $eagerCustomers = $customerQuery->joinWith(['items2'])->all(); $eagerItemsCount = 0; foreach ($eagerCustomers as $customer) { $eagerItemsCount += is_countable($customer->items2) ? count($customer->items2) : 0; } - $customerQuery = new ActiveQuery(Customer::class, $this->db); + $customerQuery = new ActiveQuery(Customer::class); $lazyCustomers = $customerQuery->all(); $lazyItemsCount = 0; foreach ($lazyCustomers as $customer) { @@ -737,9 +737,9 @@ public function testJoinWithEager() public function testToArray(): void { - $this->checkFixture($this->db, 'customer', true); + $this->checkFixture($this->db(), 'customer', true); - $customerQuery = new ActiveQuery(Customer::class, $this->db); + $customerQuery = new ActiveQuery(Customer::class); $customer = $customerQuery->findOne(1); $this->assertSame( @@ -758,9 +758,9 @@ public function testToArray(): void public function testToArrayWithClosure(): void { - $this->checkFixture($this->db, 'customer', true); + $this->checkFixture($this->db(), 'customer', true); - $customerQuery = new ActiveQuery(CustomerClosureField::class, $this->db); + $customerQuery = new ActiveQuery(CustomerClosureField::class); $customer = $customerQuery->findOne(1); $this->assertSame( @@ -779,9 +779,9 @@ public function testToArrayWithClosure(): void public function testToArrayForArrayable(): void { - $this->checkFixture($this->db, 'customer', true); + $this->checkFixture($this->db(), 'customer', true); - $customerQuery = new ActiveQuery(CustomerForArrayable::class, $this->db); + $customerQuery = new ActiveQuery(CustomerForArrayable::class); /** @var CustomerForArrayable $customer */ $customer = $customerQuery->findOne(1); @@ -833,9 +833,9 @@ public function testToArrayForArrayable(): void public function testSaveWithoutChanges(): void { - $this->checkFixture($this->db, 'customer'); + $this->checkFixture($this->db(), 'customer'); - $customerQuery = new ActiveQuery(Customer::class, $this->db); + $customerQuery = new ActiveQuery(Customer::class); $customer = $customerQuery->findOne(1); @@ -844,9 +844,9 @@ public function testSaveWithoutChanges(): void public function testGetPrimaryKey(): void { - $this->checkFixture($this->db, 'customer'); + $this->checkFixture($this->db(), 'customer'); - $customerQuery = new ActiveQuery(Customer::class, $this->db); + $customerQuery = new ActiveQuery(Customer::class); $customer = $customerQuery->findOne(1); @@ -856,9 +856,9 @@ public function testGetPrimaryKey(): void public function testGetOldPrimaryKey(): void { - $this->checkFixture($this->db, 'customer'); + $this->checkFixture($this->db(), 'customer'); - $customerQuery = new ActiveQuery(Customer::class, $this->db); + $customerQuery = new ActiveQuery(Customer::class); $customer = $customerQuery->findOne(1); $customer->id = 2; @@ -869,9 +869,9 @@ public function testGetOldPrimaryKey(): void public function testGetDirtyAttributesOnNewRecord(): void { - $this->checkFixture($this->db, 'customer'); + $this->checkFixture($this->db(), 'customer'); - $customer = new Customer($this->db); + $customer = new Customer(); $this->assertSame([], $customer->getDirtyAttributes()); @@ -898,9 +898,9 @@ public function testGetDirtyAttributesOnNewRecord(): void public function testGetDirtyAttributesAfterFind(): void { - $this->checkFixture($this->db, 'customer'); + $this->checkFixture($this->db(), 'customer'); - $customerQuery = new ActiveQuery(Customer::class, $this->db); + $customerQuery = new ActiveQuery(Customer::class); $customer = $customerQuery->findOne(1); $this->assertSame([], $customer->getDirtyAttributes()); @@ -921,15 +921,15 @@ public function testGetDirtyAttributesAfterFind(): void public function testGetDirtyAttributesWithProperties(): void { - $this->checkFixture($this->db, 'customer'); + $this->checkFixture($this->db(), 'customer'); - $customer = new CustomerWithProperties($this->db); + $customer = new CustomerWithProperties(); $this->assertSame([ 'name' => null, 'address' => null, ], $customer->getDirtyAttributes()); - $customerQuery = new ActiveQuery(CustomerWithProperties::class, $this->db); + $customerQuery = new ActiveQuery(CustomerWithProperties::class); $customer = $customerQuery->findOne(1); $this->assertSame([], $customer->getDirtyAttributes()); diff --git a/tests/Stubs/ActiveRecord/Animal.php b/tests/Stubs/ActiveRecord/Animal.php index d430c8924..7346abc99 100644 --- a/tests/Stubs/ActiveRecord/Animal.php +++ b/tests/Stubs/ActiveRecord/Animal.php @@ -6,7 +6,6 @@ use Yiisoft\ActiveRecord\Tests\Stubs\ActiveRecord; use Yiisoft\ActiveRecord\ActiveRecordInterface; -use Yiisoft\Db\Connection\ConnectionInterface; /** * Class Animal. @@ -23,10 +22,8 @@ public function getTableName(): string return 'animal'; } - public function __construct(ConnectionInterface $db) + public function __construct() { - parent::__construct($db); - $this->type = static::class; } @@ -39,7 +36,7 @@ public function instantiate($row): ActiveRecordInterface { $class = $row['type']; - return new $class($this->db()); + return new $class(); } public function setDoes(string $value): void diff --git a/tests/Stubs/ActiveRecord/ArrayAndJsonTypes.php b/tests/Stubs/ActiveRecord/ArrayAndJsonTypes.php index 080f1f0e8..7cdcfc7ea 100644 --- a/tests/Stubs/ActiveRecord/ArrayAndJsonTypes.php +++ b/tests/Stubs/ActiveRecord/ArrayAndJsonTypes.php @@ -5,15 +5,12 @@ namespace Yiisoft\ActiveRecord\Tests\Stubs\ActiveRecord; use Yiisoft\ActiveRecord\Tests\Stubs\ActiveRecord; -use Yiisoft\ActiveRecord\Trait\CustomTableNameTrait; use Yiisoft\Db\Expression\ArrayExpression; use Yiisoft\Db\Expression\Expression; use Yiisoft\Db\Expression\JsonExpression; final class ArrayAndJsonTypes extends ActiveRecord { - use CustomTableNameTrait; - public int $id; public array|ArrayExpression|null $intarray_col = null; public array|ArrayExpression|null $textarray2_col = null; diff --git a/tests/Stubs/ActiveRecord/Customer.php b/tests/Stubs/ActiveRecord/Customer.php index 99dfe9944..786487756 100644 --- a/tests/Stubs/ActiveRecord/Customer.php +++ b/tests/Stubs/ActiveRecord/Customer.php @@ -53,7 +53,7 @@ public function relationQuery(string $name): ActiveQueryInterface 'orderItems' => $this->getOrderItemsQuery(), 'orderItems2' => $this->getOrderItems2Query(), 'items2' => $this->getItems2Query(), - 'ordersUsingInstance' => $this->hasMany(new Order($this->db()), ['customer_id' => 'id']), + 'ordersUsingInstance' => $this->hasMany(new Order(), ['customer_id' => 'id']), default => parent::relationQuery($name), }; } diff --git a/tests/Stubs/ActiveRecord/CustomerWithConstructor.php b/tests/Stubs/ActiveRecord/CustomerWithConstructor.php deleted file mode 100644 index 90de7bb58..000000000 --- a/tests/Stubs/ActiveRecord/CustomerWithConstructor.php +++ /dev/null @@ -1,53 +0,0 @@ - $this->getProfileQuery(), - default => parent::relationQuery($name), - }; - } - - public function getProfile(): Profile|null - { - return $this->relation('profile'); - } - - public function getProfileQuery(): ActiveQuery - { - return $this->hasOne(Profile::class, ['id' => 'profile_id']); - } -} diff --git a/tests/Stubs/ActiveRecord/CustomerWithCustomConnection.php b/tests/Stubs/ActiveRecord/CustomerWithCustomConnection.php new file mode 100644 index 000000000..c8633d602 --- /dev/null +++ b/tests/Stubs/ActiveRecord/CustomerWithCustomConnection.php @@ -0,0 +1,12 @@ +db()); + return new ActiveQuery(Order::class); } } diff --git a/tests/Stubs/MagicActiveRecord/Animal.php b/tests/Stubs/MagicActiveRecord/Animal.php index 9d628b901..2e2d0ae34 100644 --- a/tests/Stubs/MagicActiveRecord/Animal.php +++ b/tests/Stubs/MagicActiveRecord/Animal.php @@ -6,7 +6,6 @@ use Yiisoft\ActiveRecord\Tests\Stubs\MagicActiveRecord; use Yiisoft\ActiveRecord\ActiveRecordInterface; -use Yiisoft\Db\Connection\ConnectionInterface; /** * Class Animal. @@ -23,10 +22,8 @@ public function getTableName(): string return 'animal'; } - public function __construct(ConnectionInterface $db) + public function __construct() { - parent::__construct($db); - $this->type = static::class; } diff --git a/tests/Stubs/MagicActiveRecord/CustomerWithConstructor.php b/tests/Stubs/MagicActiveRecord/CustomerWithConstructor.php deleted file mode 100644 index e0a146034..000000000 --- a/tests/Stubs/MagicActiveRecord/CustomerWithConstructor.php +++ /dev/null @@ -1,38 +0,0 @@ -hasOne(Profile::class, ['id' => 'profile_id']); - } -} diff --git a/tests/Stubs/MagicActiveRecord/OrderItem.php b/tests/Stubs/MagicActiveRecord/OrderItem.php index 928685cc4..1fd40ed91 100644 --- a/tests/Stubs/MagicActiveRecord/OrderItem.php +++ b/tests/Stubs/MagicActiveRecord/OrderItem.php @@ -58,6 +58,6 @@ public function getOrderItemCompositeNoJoinQuery(): ActiveQuery public function getCustomQuery(): ActiveQuery { - return new ActiveQuery(Order::class, $this->db()); + return new ActiveQuery(Order::class); } } diff --git a/tests/TestCase.php b/tests/TestCase.php index 76faebf1f..0953f364f 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -4,12 +4,13 @@ namespace Yiisoft\ActiveRecord\Tests; +use Yiisoft\ActiveRecord\ConnectionProvider; use Yiisoft\ActiveRecord\Tests\Support\DbHelper; use Yiisoft\Db\Connection\ConnectionInterface; -class TestCase extends \PHPUnit\Framework\TestCase +abstract class TestCase extends \PHPUnit\Framework\TestCase { - protected ConnectionInterface $db; + abstract protected function createConnection(): ConnectionInterface; protected function checkFixture(ConnectionInterface $db, string $tablename, bool $reset = false): void { @@ -22,4 +23,25 @@ protected function checkFixture(ConnectionInterface $db, string $tablename, bool $schema->refresh(); } } + + protected function db(): ConnectionInterface + { + return ConnectionProvider::get(); + } + + protected function setUp(): void + { + parent::setUp(); + + ConnectionProvider::set($this->createConnection()); + } + + protected function tearDown(): void + { + parent::tearDown(); + + $this->db()->close(); + + ConnectionProvider::remove(); + } }