1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yiiunit\framework\test;
use yii\test\ActiveFixture;
use yiiunit\data\ar\ActiveRecord;
use yii\test\FixtureTrait;
use yii\test\InitDbFixture;
use yiiunit\data\ar\Customer;
use yiiunit\framework\db\DatabaseTestCase;
class CustomerFixture extends ActiveFixture
{
public $modelClass = 'yiiunit\data\ar\Customer';
}
class MyDbTestCase
{
use FixtureTrait;
public function setUp()
{
$this->unloadFixtures();
$this->loadFixtures();
}
public function tearDown()
{
}
public function fixtures()
{
return [
'customers' => CustomerFixture::className(),
];
}
public function globalFixtures()
{
return [
InitDbFixture::className(),
];
}
}
/**
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
*/
class ActiveFixtureTest extends DatabaseTestCase
{
public function setUp()
{
parent::setUp();
\Yii::$app->set('db', $this->getConnection());
ActiveRecord::$db = $this->getConnection();
}
public function tearDown()
{
parent::tearDown();
}
public function testGetData()
{
$test = new MyDbTestCase();
$test->setUp();
$fixture = $test->getFixture('customers');
$this->assertEquals(CustomerFixture::className(), get_class($fixture));
$this->assertEquals(2, count($fixture));
$this->assertEquals(1, $fixture['customer1']['id']);
$this->assertEquals('customer1@example.com', $fixture['customer1']['email']);
$this->assertEquals(2, $fixture['customer2']['id']);
$this->assertEquals('customer2@example.com', $fixture['customer2']['email']);
$test->tearDown();
}
public function testGetModel()
{
$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(2, $fixture->getModel('customer2')->id);
$this->assertEquals('customer2@example.com', $fixture->getModel('customer2')->email);
$test->tearDown();
}
}