diff --git a/tests/unit/data/ar/Customer.php b/tests/unit/data/ar/Customer.php index 3a64e613ae..bac385039b 100644 --- a/tests/unit/data/ar/Customer.php +++ b/tests/unit/data/ar/Customer.php @@ -12,6 +12,9 @@ use yiiunit\framework\db\ActiveRecordTest; * @property string $email * @property string $address * @property integer $status + * + * @method CustomerQuery|Customer|null find($q = null) static + * @method CustomerQuery findBySql($sql, $params = []) static */ class Customer extends ActiveRecord { @@ -25,6 +28,11 @@ class Customer extends ActiveRecord return 'tbl_customer'; } + public function getProfile() + { + return $this->hasOne(Profile::className(), ['id' => 'profile_id']); + } + public function getOrders() { return $this->hasMany(Order::className(), ['customer_id' => 'id'])->orderBy('id'); diff --git a/tests/unit/data/ar/Profile.php b/tests/unit/data/ar/Profile.php new file mode 100644 index 0000000000..7651d8857c --- /dev/null +++ b/tests/unit/data/ar/Profile.php @@ -0,0 +1,22 @@ + + */ + +namespace yiiunit\data\ar; + +/** + * Class Profile + * + * @property integer $id + * @property string $description + * + */ +class Profile extends ActiveRecord +{ + public static function tableName() + { + return 'tbl_profile'; + } + +} \ No newline at end of file diff --git a/tests/unit/data/cubrid.sql b/tests/unit/data/cubrid.sql index 1af3c5ceda..cecc66dbe5 100644 --- a/tests/unit/data/cubrid.sql +++ b/tests/unit/data/cubrid.sql @@ -9,6 +9,7 @@ DROP TABLE IF EXISTS tbl_item; DROP TABLE IF EXISTS tbl_order; DROP TABLE IF EXISTS tbl_category; DROP TABLE IF EXISTS tbl_customer; +DROP TABLE IF EXISTS tbl_profile; DROP TABLE IF EXISTS tbl_null_values; DROP TABLE IF EXISTS tbl_type; 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` ( `id` int(11) NOT NULL AUTO_INCREMENT, `email` varchar(128) NOT NULL, `name` varchar(128), `address` string, `status` int (11) DEFAULT 0, + `profile_id` int(11), 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 ); -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 ('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 ('Movies'); diff --git a/tests/unit/data/mssql.sql b/tests/unit/data/mssql.sql index e25dbbbfe5..0b9dfaa0f3 100644 --- a/tests/unit/data/mssql.sql +++ b/tests/unit/data/mssql.sql @@ -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_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_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_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] ( [id] [int] IDENTITY(1,1) NOT NULL, [email] [varchar](128) NOT NULL, [name] [varchar](128), [address] [text], [status] [int] DEFAULT 0, + [profile_id] [int], CONSTRAINT [PK_customer] PRIMARY KEY CLUSTERED ( [id] ASC ) ON [PRIMARY] @@ -79,9 +89,12 @@ CREATE TABLE [dbo].[tbl_type] ( [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 ('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 ('Movies'); diff --git a/tests/unit/data/mysql.sql b/tests/unit/data/mysql.sql index 71d61591a9..f2e1e77968 100644 --- a/tests/unit/data/mysql.sql +++ b/tests/unit/data/mysql.sql @@ -9,6 +9,7 @@ DROP TABLE IF EXISTS tbl_item CASCADE; DROP TABLE IF EXISTS tbl_order CASCADE; DROP TABLE IF EXISTS tbl_category 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_type 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` ( `id` int(11) NOT NULL AUTO_INCREMENT, `email` varchar(128) NOT NULL, `name` varchar(128), `address` text, `status` int (11) DEFAULT 0, + `profile_id` int(11), PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; @@ -96,9 +104,12 @@ CREATE TABLE `tbl_type` ( `bool_col2` tinyint(1) DEFAULT '1' ) 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 ('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 ('Movies'); diff --git a/tests/unit/data/postgres.sql b/tests/unit/data/postgres.sql index 2d8737bd74..32557b26ad 100644 --- a/tests/unit/data/postgres.sql +++ b/tests/unit/data/postgres.sql @@ -9,6 +9,7 @@ DROP TABLE IF EXISTS tbl_item CASCADE; DROP TABLE IF EXISTS tbl_order CASCADE; DROP TABLE IF EXISTS tbl_category 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_null_values CASCADE; DROP TABLE IF EXISTS tbl_constraints CASCADE; @@ -19,12 +20,18 @@ CREATE TABLE tbl_constraints field1 varchar(255) ); +CREATE TABLE tbl_profile ( + id serial not null primary key, + description varchar(128) NOT NULL +); + CREATE TABLE tbl_customer ( id serial not null primary key, email varchar(128) NOT NULL, name varchar(128), address text, - status integer DEFAULT 0 + status integer DEFAULT 0, + profile_id integer ); comment on column public.tbl_customer.email is 'someone@example.com'; @@ -79,9 +86,12 @@ CREATE TABLE tbl_type ( 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 ('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 ('Movies'); diff --git a/tests/unit/data/sqlite.sql b/tests/unit/data/sqlite.sql index fcdb68c6ca..8d103d6fad 100644 --- a/tests/unit/data/sqlite.sql +++ b/tests/unit/data/sqlite.sql @@ -9,15 +9,23 @@ DROP TABLE IF EXISTS tbl_item; DROP TABLE IF EXISTS tbl_order; DROP TABLE IF EXISTS tbl_category; DROP TABLE IF EXISTS tbl_customer; +DROP TABLE IF EXISTS tbl_profile; DROP TABLE IF EXISTS tbl_type; 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 ( id INTEGER NOT NULL, email varchar(128) NOT NULL, name varchar(128), address text, status INTEGER DEFAULT 0, + profile_id INTEGER, PRIMARY KEY (id) ); @@ -81,9 +89,12 @@ CREATE TABLE tbl_type ( 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 ('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 ('Movies'); diff --git a/tests/unit/framework/db/ActiveRecordTest.php b/tests/unit/framework/db/ActiveRecordTest.php index 26515df72e..8b98815ce7 100644 --- a/tests/unit/framework/db/ActiveRecordTest.php +++ b/tests/unit/framework/db/ActiveRecordTest.php @@ -7,6 +7,7 @@ use yiiunit\data\ar\NullValues; use yiiunit\data\ar\OrderItem; use yiiunit\data\ar\Order; use yiiunit\data\ar\Item; +use yiiunit\data\ar\Profile; use yiiunit\framework\ar\ActiveRecordTestTrait; /** @@ -341,6 +342,34 @@ class ActiveRecordTest extends DatabaseTestCase $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() { // eager loading: find one and all