Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Tigrov committed May 22, 2024
1 parent b4fa5f0 commit 80a81da
Show file tree
Hide file tree
Showing 27 changed files with 343 additions and 98 deletions.
1 change: 1 addition & 0 deletions tests/ActiveQueryFindTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,7 @@ public function testFindAsArray(): void
'name' => 'user2',
'address' => 'address2',
'status' => 1,
'bool_status' => true,
'profile_id' => null,
], $customer);

Expand Down
30 changes: 3 additions & 27 deletions tests/ActiveQueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2116,15 +2116,7 @@ public function testGetAttributes(): void
$attributesExpected['name'] = 'user1';
$attributesExpected['address'] = 'address1';
$attributesExpected['status'] = 1;

if ($this->db->getDriverName() === 'pgsql') {
$attributesExpected['bool_status'] = true;
}

if ($this->db->getDriverName() === 'oci') {
$attributesExpected['bool_status'] = '1';
}

$attributesExpected['bool_status'] = true;
$attributesExpected['profile_id'] = 1;

$customer = new ActiveQuery(Customer::class, $this->db);
Expand Down Expand Up @@ -2200,15 +2192,7 @@ public function testGetOldAttributes(): void
$attributes['name'] = 'user1';
$attributes['address'] = 'address1';
$attributes['status'] = 1;

if ($this->db->getDriverName() === 'pgsql') {
$attributes['bool_status'] = true;
}

if ($this->db->getDriverName() === 'oci') {
$attributes['bool_status'] = '1';
}

$attributes['bool_status'] = true;
$attributes['profile_id'] = 1;

$customer = new ActiveQuery(Customer::class, $this->db);
Expand All @@ -2223,15 +2207,7 @@ public function testGetOldAttributes(): void
$attributesNew['name'] = 'samdark';
$attributesNew['address'] = 'address1';
$attributesNew['status'] = 1;

if ($this->db->getDriverName() === 'pgsql') {
$attributesNew['bool_status'] = true;
}

if ($this->db->getDriverName() === 'oci') {
$attributesNew['bool_status'] = '1';
}

$attributesNew['bool_status'] = true;
$attributesNew['profile_id'] = 1;

$this->assertEquals($attributesNew, $query->getAttributes());
Expand Down
4 changes: 4 additions & 0 deletions tests/ActiveRecordTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -756,6 +756,7 @@ public function testToArray(): void
'name' => 'user1',
'address' => 'address1',
'status' => 1,
'bool_status' => true,
'profile_id' => 1,
],
$customer->toArray(),
Expand All @@ -776,6 +777,7 @@ public function testToArrayWithClosure(): void
'name' => 'user1',
'address' => 'address1',
'status' => 'active',
'bool_status' => true,
'profile_id' => 1,
],
$customer->toArray(),
Expand Down Expand Up @@ -883,6 +885,7 @@ public function testGetDirtyAttributesOnNewRecord(): void
'name' => null,
'address' => null,
'status' => 0,
'bool_status' => false,
'profile_id' => null,
],
$customer->getDirtyAttributes()
Expand All @@ -900,6 +903,7 @@ public function testGetDirtyAttributesOnNewRecord(): void
'email' => '[email protected]',
'address' => null,
'status' => 0,
'bool_status' => false,
'profile_id' => null,
],
$customer->getDirtyAttributes()
Expand Down
34 changes: 34 additions & 0 deletions tests/Driver/Mysql/ActiveRecordTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Yiisoft\ActiveRecord\Tests\Driver\Mysql;

use Yiisoft\ActiveRecord\ActiveQuery;
use Yiisoft\ActiveRecord\Tests\Driver\Mysql\Stubs\Type;
use Yiisoft\ActiveRecord\Tests\Stubs\ActiveRecord\Beta;
use Yiisoft\ActiveRecord\Tests\Stubs\ActiveRecord\Customer;
use Yiisoft\ActiveRecord\Tests\Support\MysqlHelper;
Expand All @@ -28,6 +29,39 @@ protected function tearDown(): void
unset($this->db);
}

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

$arClass = new Type($this->db);

$arClass->int_col = 123;
$arClass->int_col2 = 456;
$arClass->smallint_col = 42;
$arClass->char_col = '1337';
$arClass->char_col2 = 'test';
$arClass->char_col3 = 'test123';
$arClass->enum_col = 'B';
$arClass->float_col = 3.742;
$arClass->float_col2 = 42.1337;
$arClass->bool_col = true;
$arClass->bool_col2 = false;

$arClass->save();

/** @var $model Type */
$aqClass = new ActiveQuery(Type::class, $this->db);
$query = $aqClass->onePopulate();

$this->assertSame(123, $query->int_col);
$this->assertSame(456, $query->int_col2);
$this->assertSame(42, $query->smallint_col);
$this->assertSame('1337', trim($query->char_col));
$this->assertSame('test', $query->char_col2);
$this->assertSame('test123', $query->char_col3);
$this->assertSame('B', $query->enum_col);
}

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

declare(strict_types=1);

namespace Yiisoft\ActiveRecord\Tests\Driver\Mysql\Stubs;

/**
* Model representing type table.
*/
final class Type extends \Yiisoft\ActiveRecord\Tests\Stubs\ActiveRecord\Type
{
public string|null $enum_col = null;
}
52 changes: 52 additions & 0 deletions tests/Driver/Oracle/ActiveQueryFindTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Yiisoft\ActiveRecord\Tests\Driver\Oracle;

use Yiisoft\ActiveRecord\ActiveQuery;
use Yiisoft\ActiveRecord\Tests\Driver\Oracle\Stubs\Customer as CustomerWithRownumid;
use Yiisoft\ActiveRecord\Tests\Stubs\ActiveRecord\Customer;
use Yiisoft\ActiveRecord\Tests\Stubs\ActiveRecord\Order;
use Yiisoft\ActiveRecord\Tests\Support\OracleHelper;
Expand All @@ -28,6 +29,57 @@ protected function tearDown(): void
unset($this->db);
}

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

/** one */
$customerQuery = new ActiveQuery(CustomerWithRownumid::class, $this->db);
$customer = $customerQuery->orderBy('id')->onePopulate();
$this->assertEquals('user1', $customer->getName());

/** all */
$customerQuery = new ActiveQuery(CustomerWithRownumid::class, $this->db);
$customers = $customerQuery->allPopulate();
$this->assertCount(3, $customers);

/** limit */
$customerQuery = new ActiveQuery(CustomerWithRownumid::class, $this->db);
$customers = $customerQuery->orderBy('id')->limit(1)->allPopulate();
$this->assertCount(1, $customers);
$this->assertEquals('user1', $customers[0]->getName());

$customers = $customerQuery->orderBy('id')->limit(1)->offset(1)->allPopulate();
$this->assertCount(1, $customers);
$this->assertEquals('user2', $customers[0]->getName());

$customers = $customerQuery->orderBy('id')->limit(1)->offset(2)->allPopulate();
$this->assertCount(1, $customers);
$this->assertEquals('user3', $customers[0]->getName());

$customers = $customerQuery->orderBy('id')->limit(2)->offset(1)->allPopulate();
$this->assertCount(2, $customers);
$this->assertEquals('user2', $customers[0]->getName());
$this->assertEquals('user3', $customers[1]->getName());

$customers = $customerQuery->limit(2)->offset(3)->allPopulate();
$this->assertCount(0, $customers);

/** offset */
$customerQuery = new ActiveQuery(CustomerWithRownumid::class, $this->db);
$customer = $customerQuery->orderBy('id')->offset(0)->onePopulate();
$this->assertEquals('user1', $customer->getName());

$customer = $customerQuery->orderBy('id')->offset(1)->onePopulate();
$this->assertEquals('user2', $customer->getName());

$customer = $customerQuery->orderBy('id')->offset(2)->onePopulate();
$this->assertEquals('user3', $customer->getName());

$customer = $customerQuery->offset(3)->onePopulate();
$this->assertNull($customer);
}

public function testFindEager(): void
{
$this->checkFixture($this->db, 'customer', true);
Expand Down
18 changes: 18 additions & 0 deletions tests/Driver/Oracle/BatchQueryResultTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace Yiisoft\ActiveRecord\Tests\Driver\Oracle;

use Yiisoft\ActiveRecord\ActiveQuery;
use Yiisoft\ActiveRecord\Tests\Driver\Oracle\Stubs\Customer;
use Yiisoft\ActiveRecord\Tests\Support\OracleHelper;

final class BatchQueryResultTest extends \Yiisoft\ActiveRecord\Tests\BatchQueryResultTest
Expand All @@ -24,4 +26,20 @@ protected function tearDown(): void

unset($this->db);
}

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

$customerQuery = new ActiveQuery(Customer::class, $this->db);

$query = $customerQuery->orderBy('id')->limit(3)->indexBy('id');

$customers = $this->getAllRowsFromBatch($query->batch(2));

$this->assertCount(3, $customers);
$this->assertEquals('user1', $customers[0]->getName());
$this->assertEquals('user2', $customers[1]->getName());
$this->assertEquals('user3', $customers[2]->getName());
}
}
2 changes: 1 addition & 1 deletion tests/Driver/Oracle/MagicActiveRecordTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
namespace Yiisoft\ActiveRecord\Tests\Driver\Oracle;

use Yiisoft\ActiveRecord\ActiveQuery;
use Yiisoft\ActiveRecord\Tests\Driver\Oracle\Stubs\Customer;
use Yiisoft\ActiveRecord\Tests\Driver\Oracle\Stubs\MagicCustomer as Customer;
use Yiisoft\ActiveRecord\Tests\Stubs\MagicActiveRecord\CustomerClosureField;
use Yiisoft\ActiveRecord\Tests\Stubs\MagicActiveRecord\Type;
use Yiisoft\ActiveRecord\Tests\Support\OracleHelper;
Expand Down
8 changes: 2 additions & 6 deletions tests/Driver/Oracle/Stubs/Customer.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,11 @@

/**
* Class Customer.
*
* @property int $id
* @property string $name
* @property string $email
* @property string $address
* @property int $status
*/
final class Customer extends \Yiisoft\ActiveRecord\Tests\Stubs\ActiveRecord\Customer
{
protected string $ROWNUMID;

public function getOrdersQuery(): ActiveQuery
{
return $this->hasMany(Order::class, ['customer_id' => 'id'])->orderBy('{{customer}}.[[id]]');
Expand Down
24 changes: 24 additions & 0 deletions tests/Driver/Oracle/Stubs/MagicCustomer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

declare(strict_types=1);

namespace Yiisoft\ActiveRecord\Tests\Driver\Oracle\Stubs;

use Yiisoft\ActiveRecord\ActiveQuery;

/**
* Class Customer.
*
* @property int $id
* @property string $name
* @property string $email
* @property string $address
* @property int $status
*/
final class MagicCustomer extends \Yiisoft\ActiveRecord\Tests\Stubs\MagicActiveRecord\Customer
{
public function getOrdersQuery(): ActiveQuery
{
return $this->hasMany(Order::class, ['customer_id' => 'id'])->orderBy('{{customer}}.[[id]]');
}
}
23 changes: 23 additions & 0 deletions tests/Driver/Oracle/Stubs/MagicOrder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

declare(strict_types=1);

namespace Yiisoft\ActiveRecord\Tests\Driver\Oracle\Stubs;

use Yiisoft\ActiveRecord\ActiveQuery;

/**
* Class Order.
*
* @property int $id
* @property int $customer_id
* @property int $created_at
* @property string $total
*/
final class MagicOrder extends \Yiisoft\ActiveRecord\Tests\Stubs\MagicActiveRecord\Order
{
public function getCustomerQuery(): ActiveQuery
{
return $this->hasOne(MagicCustomer::class, ['id' => 'customer_id']);
}
}
60 changes: 60 additions & 0 deletions tests/Driver/Pgsql/ActiveRecordTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use ArrayAccess;
use Traversable;
use Yiisoft\ActiveRecord\ActiveQuery;
use Yiisoft\ActiveRecord\Tests\Driver\Pgsql\Stubs\Type;
use Yiisoft\ActiveRecord\Tests\Stubs\ActiveRecord\ArrayAndJsonTypes;
use Yiisoft\ActiveRecord\Tests\Stubs\ActiveRecord\Beta;
use Yiisoft\ActiveRecord\Tests\Stubs\ActiveRecord\BoolAR;
Expand Down Expand Up @@ -39,6 +40,65 @@ protected function tearDown(): void
unset($this->db);
}

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

$arClass = new Type($this->db);

$arClass->loadDefaultValues();

$this->assertEquals(1, $arClass->int_col2);
$this->assertEquals('something', $arClass->char_col2);
$this->assertEquals(1.23, $arClass->float_col2);
$this->assertEquals(33.22, $arClass->numeric_col);
$this->assertEquals(true, $arClass->bool_col2);
$this->assertEquals('2002-01-01 00:00:00', $arClass->time);

$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->char_col2 = 'not something';

$arClass->loadDefaultValues(false);
$this->assertEquals('something', $arClass->char_col2);
}

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

$arClass = new Type($this->db);

$arClass->int_col = 123;
$arClass->int_col2 = 456;
$arClass->smallint_col = 42;
$arClass->char_col = '1337';
$arClass->char_col2 = 'test';
$arClass->char_col3 = 'test123';
$arClass->float_col = 3.742;
$arClass->float_col2 = 42.1337;
$arClass->bool_col = true;
$arClass->bool_col2 = false;

$arClass->save();

/** @var $model Type */
$aqClass = new ActiveQuery(Type::class, $this->db);
$query = $aqClass->onePopulate();

$this->assertSame(123, $query->int_col);
$this->assertSame(456, $query->int_col2);
$this->assertSame(42, $query->smallint_col);
$this->assertSame('1337', trim($query->char_col));
$this->assertSame('test', $query->char_col2);
$this->assertSame('test123', $query->char_col3);
}

public function testExplicitPkOnAutoIncrement(): void
{
$this->checkFixture($this->db, 'customer');
Expand Down
Loading

0 comments on commit 80a81da

Please sign in to comment.