Skip to content

Commit

Permalink
Test ArrayableTrait separately (#382)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tigrov authored Aug 28, 2024
1 parent 10321c5 commit d6591f4
Show file tree
Hide file tree
Showing 48 changed files with 317 additions and 590 deletions.
14 changes: 0 additions & 14 deletions tests/ActiveQueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2160,20 +2160,6 @@ public function testGetAttributesExcept(): void
);
}

public function testFields(): void
{
$this->checkFixture($this->db(), 'order_item');

$orderItem = new ActiveQuery(OrderItem::class);

$fields = $orderItem->findOne(['order_id' => 1, 'item_id' => 2])->fields();

$this->assertEquals(
$fields,
['order_id' => 1, 'item_id' => 2, 'quantity' => 2, 'subtotal' => '40', 'price' => 20]
);
}

public function testGetOldAttribute(): void
{
$this->checkFixture($this->db(), 'customer');
Expand Down
98 changes: 0 additions & 98 deletions tests/ActiveRecordTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@
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\CustomerWithFactory;
use Yiisoft\ActiveRecord\Tests\Stubs\ActiveRecord\CustomerWithCustomConnection;
Expand Down Expand Up @@ -754,102 +752,6 @@ public function testJoinWithEager()
$this->assertEquals($eagerItemsCount, $lazyItemsCount);
}

public function testToArray(): void
{
$this->checkFixture($this->db(), 'customer', true);

$customerQuery = new ActiveQuery(Customer::class);
$customer = $customerQuery->findOne(1);

$this->assertSame(
[
'id' => 1,
'email' => '[email protected]',
'name' => 'user1',
'address' => 'address1',
'status' => 1,
'bool_status' => true,
'profile_id' => 1,
],
$customer->toArray(),
);
}

public function testToArrayWithClosure(): void
{
$this->checkFixture($this->db(), 'customer', true);

$customerQuery = new ActiveQuery(CustomerClosureField::class);
$customer = $customerQuery->findOne(1);

$this->assertSame(
[
'id' => 1,
'email' => '[email protected]',
'name' => 'user1',
'address' => 'address1',
'status' => 'active',
'bool_status' => true,
'profile_id' => 1,
],
$customer->toArray(),
);
}

public function testToArrayForArrayable(): void
{
$this->checkFixture($this->db(), 'customer', true);

$customerQuery = new ActiveQuery(CustomerForArrayable::class);

/** @var CustomerForArrayable $customer */
$customer = $customerQuery->findOne(1);
/** @var CustomerForArrayable $customer2 */
$customer2 = $customerQuery->findOne(2);
/** @var CustomerForArrayable $customer3 */
$customer3 = $customerQuery->findOne(3);

$customer->setItem($customer2);
$customer->setItems($customer3);

$this->assertSame(
[
'id' => 1,
'email' => '[email protected]',
'name' => 'user1',
'address' => 'address1',
'status' => 'active',
'item' => [
'id' => 2,
'email' => '[email protected]',
'name' => 'user2',
'status' => 'active',
],
'items' => [
[
'id' => 3,
'email' => '[email protected]',
'name' => 'user3',
'status' => 'inactive',
],
],
],
$customer->toArray([
'id',
'name',
'email',
'address',
'status',
'item.id',
'item.name',
'item.email',
'items.0.id',
'items.0.name',
'items.0.email',
]),
);
}

public function testSaveWithoutChanges(): void
{
$this->checkFixture($this->db(), 'customer');
Expand Down
133 changes: 133 additions & 0 deletions tests/ArrayableTraitTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
<?php

declare(strict_types=1);

namespace Yiisoft\ActiveRecord\Tests;

use Yiisoft\ActiveRecord\ActiveQuery;
use Yiisoft\ActiveRecord\Tests\Stubs\ActiveRecord\Customer;
use Yiisoft\ActiveRecord\Tests\Stubs\ActiveRecord\CustomerClosureField;
use Yiisoft\ActiveRecord\Tests\Stubs\ActiveRecord\CustomerForArrayable;

abstract class ArrayableTraitTest extends TestCase
{
public function testFields(): void
{
$this->checkFixture($this->db(), 'customer');

$customerQuery = new ActiveQuery(CustomerForArrayable::class);

$fields = $customerQuery->findOne(['id' => 1])->fields();

$this->assertEquals(
[
'id' => 'id',
'email' => 'email',
'name' => 'name',
'address' => 'address',
'status' => 'status',
'bool_status' => 'bool_status',
'profile_id' => 'profile_id',
'item' => 'item',
'items' => 'items',
],
$fields,
);
}

public function testToArray(): void
{
$this->checkFixture($this->db(), 'customer', true);

$customerQuery = new ActiveQuery(Customer::class);
$customer = $customerQuery->findOne(1);

$this->assertSame(
[
'id' => 1,
'email' => '[email protected]',
'name' => 'user1',
'address' => 'address1',
'status' => 1,
'bool_status' => true,
'profile_id' => 1,
],
$customer->toArray(),
);
}

public function testToArrayWithClosure(): void
{
$this->checkFixture($this->db(), 'customer', true);

$customerQuery = new ActiveQuery(CustomerClosureField::class);
$customer = $customerQuery->findOne(1);

$this->assertSame(
[
'id' => 1,
'email' => '[email protected]',
'name' => 'user1',
'address' => 'address1',
'status' => 'active',
'bool_status' => true,
'profile_id' => 1,
],
$customer->toArray(),
);
}

public function testToArrayForArrayable(): void
{
$this->checkFixture($this->db(), 'customer', true);

$customerQuery = new ActiveQuery(CustomerForArrayable::class);

/** @var CustomerForArrayable $customer */
$customer = $customerQuery->findOne(1);
/** @var CustomerForArrayable $customer2 */
$customer2 = $customerQuery->findOne(2);
/** @var CustomerForArrayable $customer3 */
$customer3 = $customerQuery->findOne(3);

$customer->setItem($customer2);
$customer->setItems($customer3);

$this->assertSame(
[
'id' => 1,
'email' => '[email protected]',
'name' => 'user1',
'address' => 'address1',
'status' => 'active',
'item' => [
'id' => 2,
'email' => '[email protected]',
'name' => 'user2',
'status' => 'active',
],
'items' => [
[
'id' => 3,
'email' => '[email protected]',
'name' => 'user3',
'status' => 'inactive',
],
],
],
$customer->toArray([
'id',
'name',
'email',
'address',
'status',
'item.id',
'item.name',
'item.email',
'items.0.id',
'items.0.name',
'items.0.email',
]),
);
}
}
2 changes: 1 addition & 1 deletion tests/Driver/Mssql/ActiveRecordTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public function testSaveWithTrigger(): void
SQL;
$this->db()->createCommand($sql)->execute();

$record = new TestTrigger($this->db());
$record = new TestTrigger();

$record->stringcol = 'test';

Expand Down
16 changes: 16 additions & 0 deletions tests/Driver/Mssql/ArrayableTraitTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

declare(strict_types=1);

namespace Yiisoft\ActiveRecord\Tests\Driver\Mssql;

use Yiisoft\ActiveRecord\Tests\Support\MssqlHelper;
use Yiisoft\Db\Connection\ConnectionInterface;

final class ArrayableTraitTest extends \Yiisoft\ActiveRecord\Tests\ArrayableTraitTest
{
protected function createConnection(): ConnectionInterface
{
return (new MssqlHelper())->createConnection();
}
}
16 changes: 16 additions & 0 deletions tests/Driver/Mysql/ArrayableTraitTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

declare(strict_types=1);

namespace Yiisoft\ActiveRecord\Tests\Driver\Mysql;

use Yiisoft\ActiveRecord\Tests\Support\MysqlHelper;
use Yiisoft\Db\Connection\ConnectionInterface;

final class ArrayableTraitTest extends \Yiisoft\ActiveRecord\Tests\ArrayableTraitTest
{
protected function createConnection(): ConnectionInterface
{
return (new MysqlHelper())->createConnection();
}
}
43 changes: 0 additions & 43 deletions tests/Driver/Oracle/ActiveRecordTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

use Yiisoft\ActiveRecord\ActiveQuery;
use Yiisoft\ActiveRecord\Tests\Driver\Oracle\Stubs\Customer;
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;
Expand Down Expand Up @@ -119,46 +118,4 @@ public function testBooleanAttribute(): void
$customers = $customerQuery->where(['status' => '0'])->all();
$this->assertCount(1, $customers);
}

public function testToArray(): void
{
$this->checkFixture($this->db(), 'customer', true);

$customerQuery = new ActiveQuery(Customer::class);
$customer = $customerQuery->findOne(1);

$this->assertSame(
[
'id' => 1,
'email' => '[email protected]',
'name' => 'user1',
'address' => 'address1',
'status' => 1,
'bool_status' => '1',
'profile_id' => 1,
],
$customer->toArray(),
);
}

public function testToArrayWithClosure(): void
{
$this->checkFixture($this->db(), 'customer', true);

$customerQuery = new ActiveQuery(CustomerClosureField::class);
$customer = $customerQuery->findOne(1);

$this->assertSame(
[
'id' => 1,
'email' => '[email protected]',
'name' => 'user1',
'address' => 'address1',
'status' => 'active',
'bool_status' => '1',
'profile_id' => 1,
],
$customer->toArray(),
);
}
}
Loading

0 comments on commit d6591f4

Please sign in to comment.