ActiveFixtureTest.php 2.48 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11
<?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 yii\test\FixtureTrait;
Qiang Xue committed
12 13
use yii\test\InitDbFixture;
use yiiunit\data\ar\ActiveRecord;
14 15
use yiiunit\framework\db\DatabaseTestCase;

Qiang Xue committed
16 17 18 19 20 21 22 23
class Customer extends ActiveRecord
{
	public static function tableName()
	{
		return 'tbl_customer2';
	}
}

24 25
class CustomerFixture extends ActiveFixture
{
Qiang Xue committed
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
	public $modelClass = 'yiiunit\framework\test\Customer';

	protected function loadSchema()
	{
		try {
			$this->dropTable('tbl_customer2');
		} catch (\Exception $e) {
		}
		$this->createTable('tbl_customer2', [
			'id' => 'pk',
			'email' => 'string',
			'name' => 'string',
			'address' => 'string',
			'status' => 'integer',
		]);
	}
42 43 44 45 46 47
}

class MyDbTestCase
{
	use FixtureTrait;

48 49 50 51 52 53 54 55 56 57 58
	public function setUp()
	{
		$this->loadFixtures();
	}

	public function tearDown()
	{
		$this->unloadFixtures();
	}

	protected function fixtures()
59 60 61 62 63
	{
		return [
			'customers' => CustomerFixture::className(),
		];
	}
Qiang Xue committed
64 65 66 67 68 69 70

	protected function globalFixtures()
	{
		return [
			InitDbFixture::className(),
		];
	}
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
}

/**
 *
 * @author Qiang Xue <qiang.xue@gmail.com>
 * @since 2.0
 */
class ActiveFixtureTest extends DatabaseTestCase
{
	public function setUp()
	{
		parent::setUp();
		\Yii::$app->setComponent('db', $this->getConnection());
		Customer::$db = $this->getConnection();
	}

	public function tearDown()
	{
		parent::tearDown();
	}

Qiang Xue committed
92
	public function testGetData()
93 94
	{
		$test = new MyDbTestCase();
95 96
		$test->setUp();
		$fixture = $test->customers;
97 98 99 100 101 102 103 104 105 106 107
		$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']);
	}

	public function testGetModel()
	{
		$test = new MyDbTestCase();
108 109
		$test->setUp();
		$fixture = $test->customers;
110 111 112 113 114 115 116
		$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);
	}
}