Fixes #5442: Fixed problem on load fixture dependencies with database related tests

This commit is contained in:
octicon-git-branch(16/)
octicon-tag(16/)
Leandro Gehlen
2017-05-08 16:31:12 -03:00
committed by Alexander Makarov
gitea-unlock(16/)
parent 9c0274d249
commit 25242adb91
octicon-diff(16/tw-mr-1) 9 changed files with 74 additions and 13 deletions

3
tests/data/mysql.sql
View File

@@ -42,7 +42,8 @@ CREATE TABLE `customer` (
`address` text,
`status` int (11) DEFAULT 0,
`profile_id` int(11),
PRIMARY KEY (`id`)
PRIMARY KEY (`id`),
CONSTRAINT `FK_customer_profile_id` FOREIGN KEY (`profile_id`) REFERENCES `profile` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `category` (

36
tests/framework/test/ActiveFixtureTest.php
View File

@@ -9,9 +9,18 @@ use yii\test\InitDbFixture;
use yiiunit\data\ar\Customer;
use yiiunit\framework\db\DatabaseTestCase;
class ProfileFixture extends ActiveFixture
{
public $modelClass = 'yiiunit\data\ar\Profile';
}
class CustomerFixture extends ActiveFixture
{
public $modelClass = 'yiiunit\data\ar\Customer';
public $depends = [
'yiiunit\framework\test\ProfileFixture'
];
}
class MyDbTestCase
@@ -20,8 +29,7 @@ class MyDbTestCase
public function setUp()
{
$this->unloadFixtures();
$this->loadFixtures();
$this->initFixtures();
}
public function tearDown()
@@ -34,17 +42,15 @@ class MyDbTestCase
'customers' => CustomerFixture::className(),
];
}
public function globalFixtures()
{
return [
InitDbFixture::className(),
];
}
}
abstract class ActiveFixtureTest extends DatabaseTestCase
/**
* @group fixture
*/
class ActiveFixtureTest extends DatabaseTestCase
{
protected $driverName = 'mysql';
public function setUp()
{
parent::setUp();
@@ -63,12 +69,17 @@ abstract class ActiveFixtureTest extends DatabaseTestCase
$test = new MyDbTestCase();
$test->setUp();
$fixture = $test->getFixture('customers');
$this->assertEquals(CustomerFixture::className(), get_class($fixture));
$this->assertCount(2, $fixture);
$this->assertEquals(1, $fixture['customer1']['id']);
$this->assertEquals('customer1@example.com', $fixture['customer1']['email']);
$this->assertEquals(1, $fixture['customer1']['profile_id']);
$this->assertEquals(2, $fixture['customer2']['id']);
$this->assertEquals('customer2@example.com', $fixture['customer2']['email']);
$this->assertEquals(2, $fixture['customer2']['profile_id']);
$test->tearDown();
}
@@ -77,11 +88,16 @@ abstract class ActiveFixtureTest extends DatabaseTestCase
$test = new MyDbTestCase();
$test->setUp();
$fixture = $test->getFixture('customers');
$this->assertEquals(Customer::className(), get_class($fixture->getModel('customer1')));
$this->assertEquals(1, $fixture->getModel('customer1')->id);
$this->assertEquals('customer1@example.com', $fixture->getModel('customer1')->email);
$this->assertEquals(1, $fixture['customer1']['profile_id']);
$this->assertEquals(2, $fixture->getModel('customer2')->id);
$this->assertEquals('customer2@example.com', $fixture->getModel('customer2')->email);
$this->assertEquals(2, $fixture['customer2']['profile_id']);
$test->tearDown();
}
}

3
tests/framework/test/ArrayFixtureTest.php
View File

@@ -11,6 +11,9 @@ namespace yiiunit\framework\test;
use yiiunit\TestCase;
use yii\test\ArrayFixture;
/**
* @group fixture
*/
class ArrayFixtureTest extends TestCase
{

3
tests/framework/test/FixtureTest.php
View File

@@ -11,6 +11,9 @@ use yii\test\Fixture;
use yii\test\FixtureTrait;
use yiiunit\TestCase;
/**
* @group fixture
*/
class Fixture1 extends Fixture
{
public $depends = ['yiiunit\framework\test\Fixture2'];

2
tests/framework/test/data/customer.php
View File

@@ -6,11 +6,13 @@ return [
'name' => 'customer1',
'address' => 'address1',
'status' => 1,
'profile_id' => 1
],
'customer2' => [
'email' => 'customer2@example.com',
'name' => 'customer2',
'address' => 'address2',
'status' => 2,
'profile_id' => 2
],
];

12
tests/framework/test/data/profile.php Normal file
View File

@@ -0,0 +1,12 @@
<?php
return [
'profile1' => [
'id' => 1,
'description' => 'profile 1',
],
'profile2' => [
'id' => 2,
'description' => 'profile 2',
],
];