added unit test for #2568

This commit is contained in:
Carsten Brandt
2014-02-27 15:12:18 +01:00
parent ad06b80e6e
commit c21e01930e
8 changed files with 126 additions and 11 deletions

View File

@ -12,6 +12,9 @@ use yiiunit\framework\db\ActiveRecordTest;
* @property string $email * @property string $email
* @property string $address * @property string $address
* @property integer $status * @property integer $status
*
* @method CustomerQuery|Customer|null find($q = null) static
* @method CustomerQuery findBySql($sql, $params = []) static
*/ */
class Customer extends ActiveRecord class Customer extends ActiveRecord
{ {
@ -25,6 +28,11 @@ class Customer extends ActiveRecord
return 'tbl_customer'; return 'tbl_customer';
} }
public function getProfile()
{
return $this->hasOne(Profile::className(), ['id' => 'profile_id']);
}
public function getOrders() public function getOrders()
{ {
return $this->hasMany(Order::className(), ['customer_id' => 'id'])->orderBy('id'); return $this->hasMany(Order::className(), ['customer_id' => 'id'])->orderBy('id');

View File

@ -0,0 +1,22 @@
<?php
/**
* @author Carsten Brandt <mail@cebe.cc>
*/
namespace yiiunit\data\ar;
/**
* Class Profile
*
* @property integer $id
* @property string $description
*
*/
class Profile extends ActiveRecord
{
public static function tableName()
{
return 'tbl_profile';
}
}

View File

@ -9,6 +9,7 @@ DROP TABLE IF EXISTS tbl_item;
DROP TABLE IF EXISTS tbl_order; DROP TABLE IF EXISTS tbl_order;
DROP TABLE IF EXISTS tbl_category; DROP TABLE IF EXISTS tbl_category;
DROP TABLE IF EXISTS tbl_customer; DROP TABLE IF EXISTS tbl_customer;
DROP TABLE IF EXISTS tbl_profile;
DROP TABLE IF EXISTS tbl_null_values; DROP TABLE IF EXISTS tbl_null_values;
DROP TABLE IF EXISTS tbl_type; DROP TABLE IF EXISTS tbl_type;
DROP TABLE IF EXISTS tbl_constraints; DROP TABLE IF EXISTS tbl_constraints;
@ -20,12 +21,19 @@ CREATE TABLE `tbl_constraints`
); );
CREATE TABLE `tbl_profile` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`description` varchar(128) NOT NULL,
PRIMARY KEY (`id`)
);
CREATE TABLE `tbl_customer` ( CREATE TABLE `tbl_customer` (
`id` int(11) NOT NULL AUTO_INCREMENT, `id` int(11) NOT NULL AUTO_INCREMENT,
`email` varchar(128) NOT NULL, `email` varchar(128) NOT NULL,
`name` varchar(128), `name` varchar(128),
`address` string, `address` string,
`status` int (11) DEFAULT 0, `status` int (11) DEFAULT 0,
`profile_id` int(11),
PRIMARY KEY (`id`) PRIMARY KEY (`id`)
); );
@ -94,9 +102,12 @@ CREATE TABLE `tbl_composite_fk` (
CONSTRAINT `FK_composite_fk_order_item` FOREIGN KEY (`order_id`,`item_id`) REFERENCES `tbl_order_item` (`order_id`,`item_id`) ON DELETE CASCADE CONSTRAINT `FK_composite_fk_order_item` FOREIGN KEY (`order_id`,`item_id`) REFERENCES `tbl_order_item` (`order_id`,`item_id`) ON DELETE CASCADE
); );
INSERT INTO tbl_customer (email, name, address, status) VALUES ('user1@example.com', 'user1', 'address1', 1); INSERT INTO tbl_profile (description) VALUES ('profile customer 1');
INSERT INTO tbl_profile (description) VALUES ('profile customer 3');
INSERT INTO tbl_customer (email, name, address, status, profile_id) VALUES ('user1@example.com', 'user1', 'address1', 1, 1);
INSERT INTO tbl_customer (email, name, address, status) VALUES ('user2@example.com', 'user2', 'address2', 1); INSERT INTO tbl_customer (email, name, address, status) VALUES ('user2@example.com', 'user2', 'address2', 1);
INSERT INTO tbl_customer (email, name, address, status) VALUES ('user3@example.com', 'user3', 'address3', 2); INSERT INTO tbl_customer (email, name, address, status, profile_id) VALUES ('user3@example.com', 'user3', 'address3', 2, 2);
INSERT INTO tbl_category (name) VALUES ('Books'); INSERT INTO tbl_category (name) VALUES ('Books');
INSERT INTO tbl_category (name) VALUES ('Movies'); INSERT INTO tbl_category (name) VALUES ('Movies');

View File

@ -3,15 +3,25 @@ IF OBJECT_ID('[dbo].[tbl_item]', 'U') IS NOT NULL DROP TABLE [dbo].[tbl_item];
IF OBJECT_ID('[dbo].[tbl_order]', 'U') IS NOT NULL DROP TABLE [dbo].[tbl_order]; IF OBJECT_ID('[dbo].[tbl_order]', 'U') IS NOT NULL DROP TABLE [dbo].[tbl_order];
IF OBJECT_ID('[dbo].[tbl_category]', 'U') IS NOT NULL DROP TABLE [dbo].[tbl_category]; IF OBJECT_ID('[dbo].[tbl_category]', 'U') IS NOT NULL DROP TABLE [dbo].[tbl_category];
IF OBJECT_ID('[dbo].[tbl_customer]', 'U') IS NOT NULL DROP TABLE [dbo].[tbl_customer]; IF OBJECT_ID('[dbo].[tbl_customer]', 'U') IS NOT NULL DROP TABLE [dbo].[tbl_customer];
IF OBJECT_ID('[dbo].[tbl_profile]', 'U') IS NOT NULL DROP TABLE [dbo].[tbl_profile];
IF OBJECT_ID('[dbo].[tbl_type]', 'U') IS NOT NULL DROP TABLE [dbo].[tbl_type]; IF OBJECT_ID('[dbo].[tbl_type]', 'U') IS NOT NULL DROP TABLE [dbo].[tbl_type];
IF OBJECT_ID('[dbo].[tbl_null_values]', 'U') IS NOT NULL DROP TABLE [dbo].[tbl_null_values]; IF OBJECT_ID('[dbo].[tbl_null_values]', 'U') IS NOT NULL DROP TABLE [dbo].[tbl_null_values];
CREATE TABLE [dbo].[tbl_profile] (
[id] [int] IDENTITY(1,1) NOT NULL,
[description] [varchar](128) NOT NULL,
CONSTRAINT [PK_customer] PRIMARY KEY CLUSTERED (
[id] ASC
) ON [PRIMARY]
);
CREATE TABLE [dbo].[tbl_customer] ( CREATE TABLE [dbo].[tbl_customer] (
[id] [int] IDENTITY(1,1) NOT NULL, [id] [int] IDENTITY(1,1) NOT NULL,
[email] [varchar](128) NOT NULL, [email] [varchar](128) NOT NULL,
[name] [varchar](128), [name] [varchar](128),
[address] [text], [address] [text],
[status] [int] DEFAULT 0, [status] [int] DEFAULT 0,
[profile_id] [int],
CONSTRAINT [PK_customer] PRIMARY KEY CLUSTERED ( CONSTRAINT [PK_customer] PRIMARY KEY CLUSTERED (
[id] ASC [id] ASC
) ON [PRIMARY] ) ON [PRIMARY]
@ -79,9 +89,12 @@ CREATE TABLE [dbo].[tbl_type] (
[bool_col2] [tinyint] DEFAULT '1' [bool_col2] [tinyint] DEFAULT '1'
); );
INSERT INTO [dbo].[tbl_customer] ([email], [name], [address], [status]) VALUES ('user1@example.com', 'user1', 'address1', 1); INSERT INTO [dbo].[tbl_profile] ([description]) VALUES ('profile customer 1');
INSERT INTO [dbo].[tbl_profile] ([description]) VALUES ('profile customer 3');
INSERT INTO [dbo].[tbl_customer] ([email], [name], [address], [status], [profile_id]) VALUES ('user1@example.com', 'user1', 'address1', 1, 1);
INSERT INTO [dbo].[tbl_customer] ([email], [name], [address], [status]) VALUES ('user2@example.com', 'user2', 'address2', 1); INSERT INTO [dbo].[tbl_customer] ([email], [name], [address], [status]) VALUES ('user2@example.com', 'user2', 'address2', 1);
INSERT INTO [dbo].[tbl_customer] ([email], [name], [address], [status]) VALUES ('user3@example.com', 'user3', 'address3', 2); INSERT INTO [dbo].[tbl_customer] ([email], [name], [address], [status], [profile_id]) VALUES ('user3@example.com', 'user3', 'address3', 2, 2);
INSERT INTO [dbo].[tbl_category] ([name]) VALUES ('Books'); INSERT INTO [dbo].[tbl_category] ([name]) VALUES ('Books');
INSERT INTO [dbo].[tbl_category] ([name]) VALUES ('Movies'); INSERT INTO [dbo].[tbl_category] ([name]) VALUES ('Movies');

View File

@ -9,6 +9,7 @@ DROP TABLE IF EXISTS tbl_item CASCADE;
DROP TABLE IF EXISTS tbl_order CASCADE; DROP TABLE IF EXISTS tbl_order CASCADE;
DROP TABLE IF EXISTS tbl_category CASCADE; DROP TABLE IF EXISTS tbl_category CASCADE;
DROP TABLE IF EXISTS tbl_customer CASCADE; DROP TABLE IF EXISTS tbl_customer CASCADE;
DROP TABLE IF EXISTS tbl_profile CASCADE;
DROP TABLE IF EXISTS tbl_null_values CASCADE; DROP TABLE IF EXISTS tbl_null_values CASCADE;
DROP TABLE IF EXISTS tbl_type CASCADE; DROP TABLE IF EXISTS tbl_type CASCADE;
DROP TABLE IF EXISTS tbl_constraints CASCADE; DROP TABLE IF EXISTS tbl_constraints CASCADE;
@ -20,12 +21,19 @@ CREATE TABLE `tbl_constraints`
); );
CREATE TABLE `tbl_profile` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`description` varchar(128) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `tbl_customer` ( CREATE TABLE `tbl_customer` (
`id` int(11) NOT NULL AUTO_INCREMENT, `id` int(11) NOT NULL AUTO_INCREMENT,
`email` varchar(128) NOT NULL, `email` varchar(128) NOT NULL,
`name` varchar(128), `name` varchar(128),
`address` text, `address` text,
`status` int (11) DEFAULT 0, `status` int (11) DEFAULT 0,
`profile_id` int(11),
PRIMARY KEY (`id`) PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8; ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
@ -96,9 +104,12 @@ CREATE TABLE `tbl_type` (
`bool_col2` tinyint(1) DEFAULT '1' `bool_col2` tinyint(1) DEFAULT '1'
) ENGINE=InnoDB DEFAULT CHARSET=utf8; ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO tbl_customer (email, name, address, status) VALUES ('user1@example.com', 'user1', 'address1', 1); INSERT INTO tbl_profile (description) VALUES ('profile customer 1');
INSERT INTO tbl_profile (description) VALUES ('profile customer 3');
INSERT INTO tbl_customer (email, name, address, status, profile_id) VALUES ('user1@example.com', 'user1', 'address1', 1, 1);
INSERT INTO tbl_customer (email, name, address, status) VALUES ('user2@example.com', 'user2', 'address2', 1); INSERT INTO tbl_customer (email, name, address, status) VALUES ('user2@example.com', 'user2', 'address2', 1);
INSERT INTO tbl_customer (email, name, address, status) VALUES ('user3@example.com', 'user3', 'address3', 2); INSERT INTO tbl_customer (email, name, address, status, profile_id) VALUES ('user3@example.com', 'user3', 'address3', 2, 2);
INSERT INTO tbl_category (name) VALUES ('Books'); INSERT INTO tbl_category (name) VALUES ('Books');
INSERT INTO tbl_category (name) VALUES ('Movies'); INSERT INTO tbl_category (name) VALUES ('Movies');

View File

@ -9,6 +9,7 @@ DROP TABLE IF EXISTS tbl_item CASCADE;
DROP TABLE IF EXISTS tbl_order CASCADE; DROP TABLE IF EXISTS tbl_order CASCADE;
DROP TABLE IF EXISTS tbl_category CASCADE; DROP TABLE IF EXISTS tbl_category CASCADE;
DROP TABLE IF EXISTS tbl_customer CASCADE; DROP TABLE IF EXISTS tbl_customer CASCADE;
DROP TABLE IF EXISTS tbl_profile CASCADE;
DROP TABLE IF EXISTS tbl_type CASCADE; DROP TABLE IF EXISTS tbl_type CASCADE;
DROP TABLE IF EXISTS tbl_null_values CASCADE; DROP TABLE IF EXISTS tbl_null_values CASCADE;
DROP TABLE IF EXISTS tbl_constraints CASCADE; DROP TABLE IF EXISTS tbl_constraints CASCADE;
@ -19,12 +20,18 @@ CREATE TABLE tbl_constraints
field1 varchar(255) field1 varchar(255)
); );
CREATE TABLE tbl_profile (
id serial not null primary key,
description varchar(128) NOT NULL
);
CREATE TABLE tbl_customer ( CREATE TABLE tbl_customer (
id serial not null primary key, id serial not null primary key,
email varchar(128) NOT NULL, email varchar(128) NOT NULL,
name varchar(128), name varchar(128),
address text, address text,
status integer DEFAULT 0 status integer DEFAULT 0,
profile_id integer
); );
comment on column public.tbl_customer.email is 'someone@example.com'; comment on column public.tbl_customer.email is 'someone@example.com';
@ -79,9 +86,12 @@ CREATE TABLE tbl_type (
bool_col2 smallint DEFAULT '1' bool_col2 smallint DEFAULT '1'
); );
INSERT INTO tbl_customer (email, name, address, status) VALUES ('user1@example.com', 'user1', 'address1', 1); INSERT INTO tbl_profile (description) VALUES ('profile customer 1');
INSERT INTO tbl_profile (description) VALUES ('profile customer 3');
INSERT INTO tbl_customer (email, name, address, status, profile_id) VALUES ('user1@example.com', 'user1', 'address1', 1, 1);
INSERT INTO tbl_customer (email, name, address, status) VALUES ('user2@example.com', 'user2', 'address2', 1); INSERT INTO tbl_customer (email, name, address, status) VALUES ('user2@example.com', 'user2', 'address2', 1);
INSERT INTO tbl_customer (email, name, address, status) VALUES ('user3@example.com', 'user3', 'address3', 2); INSERT INTO tbl_customer (email, name, address, status, profile_id) VALUES ('user3@example.com', 'user3', 'address3', 2, 2);
INSERT INTO tbl_category (name) VALUES ('Books'); INSERT INTO tbl_category (name) VALUES ('Books');
INSERT INTO tbl_category (name) VALUES ('Movies'); INSERT INTO tbl_category (name) VALUES ('Movies');

View File

@ -9,15 +9,23 @@ DROP TABLE IF EXISTS tbl_item;
DROP TABLE IF EXISTS tbl_order; DROP TABLE IF EXISTS tbl_order;
DROP TABLE IF EXISTS tbl_category; DROP TABLE IF EXISTS tbl_category;
DROP TABLE IF EXISTS tbl_customer; DROP TABLE IF EXISTS tbl_customer;
DROP TABLE IF EXISTS tbl_profile;
DROP TABLE IF EXISTS tbl_type; DROP TABLE IF EXISTS tbl_type;
DROP TABLE IF EXISTS tbl_null_values; DROP TABLE IF EXISTS tbl_null_values;
CREATE TABLE tbl_profile (
id INTEGER NOT NULL,
description varchar(128) NOT NULL,
PRIMARY KEY (id)
);
CREATE TABLE tbl_customer ( CREATE TABLE tbl_customer (
id INTEGER NOT NULL, id INTEGER NOT NULL,
email varchar(128) NOT NULL, email varchar(128) NOT NULL,
name varchar(128), name varchar(128),
address text, address text,
status INTEGER DEFAULT 0, status INTEGER DEFAULT 0,
profile_id INTEGER,
PRIMARY KEY (id) PRIMARY KEY (id)
); );
@ -81,9 +89,12 @@ CREATE TABLE tbl_type (
bool_col2 tinyint(1) DEFAULT '1' bool_col2 tinyint(1) DEFAULT '1'
); );
INSERT INTO tbl_customer (email, name, address, status) VALUES ('user1@example.com', 'user1', 'address1', 1); INSERT INTO tbl_profile (description) VALUES ('profile customer 1');
INSERT INTO tbl_profile (description) VALUES ('profile customer 3');
INSERT INTO tbl_customer (email, name, address, status, profile_id) VALUES ('user1@example.com', 'user1', 'address1', 1, 1);
INSERT INTO tbl_customer (email, name, address, status) VALUES ('user2@example.com', 'user2', 'address2', 1); INSERT INTO tbl_customer (email, name, address, status) VALUES ('user2@example.com', 'user2', 'address2', 1);
INSERT INTO tbl_customer (email, name, address, status) VALUES ('user3@example.com', 'user3', 'address3', 2); INSERT INTO tbl_customer (email, name, address, status, profile_id) VALUES ('user3@example.com', 'user3', 'address3', 2, 2);
INSERT INTO tbl_category (name) VALUES ('Books'); INSERT INTO tbl_category (name) VALUES ('Books');
INSERT INTO tbl_category (name) VALUES ('Movies'); INSERT INTO tbl_category (name) VALUES ('Movies');

View File

@ -7,6 +7,7 @@ use yiiunit\data\ar\NullValues;
use yiiunit\data\ar\OrderItem; use yiiunit\data\ar\OrderItem;
use yiiunit\data\ar\Order; use yiiunit\data\ar\Order;
use yiiunit\data\ar\Item; use yiiunit\data\ar\Item;
use yiiunit\data\ar\Profile;
use yiiunit\framework\ar\ActiveRecordTestTrait; use yiiunit\framework\ar\ActiveRecordTestTrait;
/** /**
@ -341,6 +342,34 @@ class ActiveRecordTest extends DatabaseTestCase
$this->assertEquals(1, count($orders[2]->books2)); $this->assertEquals(1, count($orders[2]->books2));
} }
public function testJoinWithAndScope()
{
// hasOne inner join
$customers = Customer::find()->active()->innerJoinWith('profile')->orderBy('tbl_customer.id')->all();
$this->assertEquals(1, count($customers));
$this->assertEquals(1, $customers[0]->id);
$this->assertTrue($customers[0]->isRelationPopulated('profile'));
// hasOne outer join
$customers = Customer::find()->active()->joinWith('profile')->orderBy('tbl_customer.id')->all();
$this->assertEquals(2, count($customers));
$this->assertEquals(1, $customers[0]->id);
$this->assertEquals(2, $customers[1]->id);
$this->assertTrue($customers[0]->isRelationPopulated('profile'));
$this->assertTrue($customers[1]->isRelationPopulated('profile'));
$this->assertInstanceOf(Profile::className(), $customers[0]->profile);
$this->assertNull($customers[1]->profile);
// hasMany
$customers = Customer::find()->active()->joinWith('orders')->orderBy('tbl_customer.id DESC, tbl_order.id')->all();
$this->assertEquals(2, count($customers));
$this->assertEquals(2, $customers[0]->id);
$this->assertEquals(1, $customers[1]->id);
$this->assertTrue($customers[0]->isRelationPopulated('orders'));
$this->assertTrue($customers[1]->isRelationPopulated('orders'));
}
public function testInverseOf() public function testInverseOf()
{ {
// eager loading: find one and all // eager loading: find one and all