| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597 |
- <?php
- /**
- * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
- * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
- *
- * Licensed under The MIT License
- * For full copyright and license information, please see the LICENSE.txt
- * Redistributions of files must retain the above copyright notice
- *
- * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
- * @link http://cakephp.org CakePHP(tm) Project
- * @since 1.2.0
- * @license http://www.opensource.org/licenses/mit-license.php MIT License
- */
- namespace Cake\Test\TestCase\TestSuite;
- use Cake\Database\Schema\Table;
- use Cake\Datasource\ConnectionManager;
- use Cake\Log\Log;
- use Cake\TestSuite\Fixture\TestFixture;
- use Cake\TestSuite\TestCase;
- use Exception;
- /**
- * ArticlesFixture class
- *
- */
- class ArticlesFixture extends TestFixture
- {
- /**
- * Table property
- *
- * @var string
- */
- public $table = 'articles';
- /**
- * Fields array
- *
- * @var array
- */
- public $fields = [
- 'id' => ['type' => 'integer'],
- 'name' => ['type' => 'string', 'length' => '255'],
- 'created' => ['type' => 'datetime'],
- '_constraints' => [
- 'primary' => ['type' => 'primary', 'columns' => ['id']]
- ]
- ];
- /**
- * Records property
- *
- * @var array
- */
- public $records = [
- ['name' => 'Gandalf', 'created' => '2009-04-28 19:20:00'],
- ['name' => 'Captain Picard', 'created' => '2009-04-28 19:20:00'],
- ['name' => 'Chewbacca', 'created' => '2009-04-28 19:20:00']
- ];
- }
- /**
- * StringsTestsFixture class
- *
- */
- class StringsTestsFixture extends TestFixture
- {
- /**
- * Table property
- *
- * @var string
- */
- public $table = 'strings';
- /**
- * Fields array
- *
- * @var array
- */
- public $fields = [
- 'id' => ['type' => 'integer'],
- 'name' => ['type' => 'string', 'length' => '255'],
- 'email' => ['type' => 'string', 'length' => '255'],
- 'age' => ['type' => 'integer', 'default' => 10]
- ];
- /**
- * Records property
- *
- * @var array
- */
- public $records = [
- ['name' => 'Mark Doe', 'email' => 'mark.doe@email.com'],
- ['name' => 'John Doe', 'email' => 'john.doe@email.com', 'age' => 20],
- ['email' => 'jane.doe@email.com', 'name' => 'Jane Doe', 'age' => 30]
- ];
- }
- /**
- * ImportsFixture class
- *
- */
- class ImportsFixture extends TestFixture
- {
- /**
- * Import property
- *
- * @var mixed
- */
- public $import = ['table' => 'posts', 'connection' => 'test'];
- /**
- * Records property
- *
- * @var array
- */
- public $records = [
- ['title' => 'Hello!', 'body' => 'Hello world!']
- ];
- }
- /**
- * This class allows testing the fixture data insertion when the properties
- * $fields and $import are not set
- *
- */
- class LettersFixture extends TestFixture
- {
- /**
- * records property
- *
- * @var array
- */
- public $records = [
- ['letter' => 'a'],
- ['letter' => 'b'],
- ['letter' => 'c']
- ];
- }
- /**
- * Test case for TestFixture
- *
- */
- class TestFixtureTest extends TestCase
- {
- /**
- * Fixtures for this test.
- *
- * @var array
- */
- public $fixtures = ['core.posts'];
- /**
- * Set up
- *
- * @return void
- */
- public function setUp()
- {
- parent::setUp();
- Log::reset();
- }
- /**
- * Tear down
- *
- * @return void
- */
- public function tearDown()
- {
- parent::tearDown();
- Log::reset();
- }
- /**
- * test initializing a static fixture
- *
- * @return void
- */
- public function testInitStaticFixture()
- {
- $Fixture = new ArticlesFixture();
- $this->assertEquals('articles', $Fixture->table);
- $Fixture = new ArticlesFixture();
- $Fixture->table = null;
- $Fixture->init();
- $this->assertEquals('articles', $Fixture->table);
- $schema = $Fixture->schema();
- $this->assertInstanceOf('Cake\Database\Schema\Table', $schema);
- $fields = $Fixture->fields;
- unset($fields['_constraints'], $fields['_indexes']);
- $this->assertEquals(
- array_keys($fields),
- $schema->columns(),
- 'Fields do not match'
- );
- $this->assertEquals(array_keys($Fixture->fields['_constraints']), $schema->constraints());
- $this->assertEmpty($schema->indexes());
- }
- /**
- * test import fixture initialization
- *
- * @return void
- */
- public function testInitImport()
- {
- $fixture = new ImportsFixture();
- $fixture->fields = $fixture->records = null;
- $fixture->import = [
- 'table' => 'posts',
- 'connection' => 'test',
- ];
- $fixture->init();
- $expected = [
- 'id',
- 'author_id',
- 'title',
- 'body',
- 'published',
- ];
- $this->assertEquals($expected, $fixture->schema()->columns());
- }
- /**
- * test import fixture initialization
- *
- * @return void
- */
- public function testInitImportModel()
- {
- $fixture = new ImportsFixture();
- $fixture->fields = $fixture->records = null;
- $fixture->import = [
- 'model' => 'Posts',
- 'connection' => 'test',
- ];
- $fixture->init();
- $expected = [
- 'id',
- 'author_id',
- 'title',
- 'body',
- 'published',
- ];
- $this->assertEquals($expected, $fixture->schema()->columns());
- }
- /**
- * test schema reflection without $import or $fields and without the table existing
- * it will throw an exception
- *
- * @expectedException \Cake\Core\Exception\Exception
- * @expectedExceptionMessage Cannot describe schema for table `letters` for fixture `Cake\Test\TestCase\TestSuite\LettersFixture` : the table does not exist.
- * @return void
- */
- public function testInitNoImportNoFieldsException()
- {
- $fixture = new LettersFixture();
- $fixture->init();
- }
- /**
- * test schema reflection without $import or $fields will reflect the schema
- *
- * @return void
- */
- public function testInitNoImportNoFields()
- {
- $db = ConnectionManager::get('test');
- $table = new Table('letters', [
- 'id' => ['type' => 'integer'],
- 'letter' => ['type' => 'string', 'length' => 1]
- ]);
- $table->addConstraint('primary', ['type' => 'primary', 'columns' => ['id']]);
- $sql = $table->createSql($db);
- foreach ($sql as $stmt) {
- $db->execute($stmt);
- }
- $fixture = new LettersFixture();
- $fixture->init();
- $this->assertEquals(['id', 'letter'], $fixture->schema()->columns());
- $db = $this->getMockBuilder('Cake\Database\Connection')
- ->setMethods(['prepare', 'execute'])
- ->disableOriginalConstructor()
- ->getMock();
- $db->expects($this->never())
- ->method('prepare');
- $db->expects($this->never())
- ->method('execute');
- $this->assertTrue($fixture->create($db));
- $this->assertTrue($fixture->drop($db));
- // Cleanup.
- $db = ConnectionManager::get('test');
- $db->execute('DROP TABLE letters');
- }
- /**
- * test create method
- *
- * @return void
- */
- public function testCreate()
- {
- $fixture = new ArticlesFixture();
- $db = $this->getMockBuilder('Cake\Database\Connection')
- ->disableOriginalConstructor()
- ->getMock();
- $table = $this->getMockBuilder('Cake\Database\Schema\Table')
- ->setConstructorArgs(['articles'])
- ->getMock();
- $table->expects($this->once())
- ->method('createSql')
- ->with($db)
- ->will($this->returnValue(['sql', 'sql']));
- $fixture->schema($table);
- $statement = $this->getMockBuilder('\PDOStatement')
- ->setMethods(['execute', 'closeCursor'])
- ->getMock();
- $statement->expects($this->atLeastOnce())->method('closeCursor');
- $statement->expects($this->atLeastOnce())->method('execute');
- $db->expects($this->exactly(2))
- ->method('prepare')
- ->will($this->returnValue($statement));
- $this->assertTrue($fixture->create($db));
- }
- /**
- * test create method, trigger error
- *
- * @expectedException \PHPUnit_Framework_Error
- * @return void
- */
- public function testCreateError()
- {
- $fixture = new ArticlesFixture();
- $db = $this->getMockBuilder('Cake\Database\Connection')
- ->disableOriginalConstructor()
- ->getMock();
- $table = $this->getMockBuilder('Cake\Database\Schema\Table')
- ->setConstructorArgs(['articles'])
- ->getMock();
- $table->expects($this->once())
- ->method('createSql')
- ->with($db)
- ->will($this->throwException(new Exception('oh noes')));
- $fixture->schema($table);
- $fixture->create($db);
- }
- /**
- * test the insert method
- *
- * @return void
- */
- public function testInsert()
- {
- $fixture = new ArticlesFixture();
- $db = $this->getMockBuilder('Cake\Database\Connection')
- ->disableOriginalConstructor()
- ->getMock();
- $query = $this->getMockBuilder('Cake\Database\Query')
- ->setConstructorArgs([$db])
- ->getMock();
- $db->expects($this->once())
- ->method('newQuery')
- ->will($this->returnValue($query));
- $query->expects($this->once())
- ->method('insert')
- ->with(['name', 'created'], ['name' => 'string', 'created' => 'datetime'])
- ->will($this->returnSelf());
- $query->expects($this->once())
- ->method('into')
- ->with('articles')
- ->will($this->returnSelf());
- $expected = [
- ['name' => 'Gandalf', 'created' => '2009-04-28 19:20:00'],
- ['name' => 'Captain Picard', 'created' => '2009-04-28 19:20:00'],
- ['name' => 'Chewbacca', 'created' => '2009-04-28 19:20:00']
- ];
- $query->expects($this->at(2))
- ->method('values')
- ->with($expected[0])
- ->will($this->returnSelf());
- $query->expects($this->at(3))
- ->method('values')
- ->with($expected[1])
- ->will($this->returnSelf());
- $query->expects($this->at(4))
- ->method('values')
- ->with($expected[2])
- ->will($this->returnSelf());
- $statement = $this->getMockBuilder('\PDOStatement')
- ->setMethods(['closeCursor'])
- ->getMock();
- $statement->expects($this->once())->method('closeCursor');
- $query->expects($this->once())
- ->method('execute')
- ->will($this->returnValue($statement));
- $this->assertSame($statement, $fixture->insert($db));
- }
- /**
- * test the insert method
- *
- * @return void
- */
- public function testInsertImport()
- {
- $fixture = new ImportsFixture();
- $db = $this->getMockBuilder('Cake\Database\Connection')
- ->disableOriginalConstructor()
- ->getMock();
- $query = $this->getMockBuilder('Cake\Database\Query')
- ->setConstructorArgs([$db])
- ->getMock();
- $db->expects($this->once())
- ->method('newQuery')
- ->will($this->returnValue($query));
- $query->expects($this->once())
- ->method('insert')
- ->with(['title', 'body'], ['title' => 'string', 'body' => 'text'])
- ->will($this->returnSelf());
- $query->expects($this->once())
- ->method('into')
- ->with('posts')
- ->will($this->returnSelf());
- $expected = [
- ['title' => 'Hello!', 'body' => 'Hello world!'],
- ];
- $query->expects($this->at(2))
- ->method('values')
- ->with($expected[0])
- ->will($this->returnSelf());
- $statement = $this->getMockBuilder('\PDOStatement')
- ->setMethods(['closeCursor'])
- ->getMock();
- $statement->expects($this->once())->method('closeCursor');
- $query->expects($this->once())
- ->method('execute')
- ->will($this->returnValue($statement));
- $this->assertSame($statement, $fixture->insert($db));
- }
- /**
- * test the insert method
- *
- * @return void
- */
- public function testInsertStrings()
- {
- $fixture = new StringsTestsFixture();
- $db = $this->getMockBuilder('Cake\Database\Connection')
- ->disableOriginalConstructor()
- ->getMock();
- $query = $this->getMockBuilder('Cake\Database\Query')
- ->setConstructorArgs([$db])
- ->getMock();
- $db->expects($this->once())
- ->method('newQuery')
- ->will($this->returnValue($query));
- $query->expects($this->once())
- ->method('insert')
- ->with(['name', 'email', 'age'], ['name' => 'string', 'email' => 'string', 'age' => 'integer'])
- ->will($this->returnSelf());
- $query->expects($this->once())
- ->method('into')
- ->with('strings')
- ->will($this->returnSelf());
- $expected = [
- ['name' => 'Mark Doe', 'email' => 'mark.doe@email.com', 'age' => null],
- ['name' => 'John Doe', 'email' => 'john.doe@email.com', 'age' => 20],
- ['name' => 'Jane Doe', 'email' => 'jane.doe@email.com', 'age' => 30],
- ];
- $query->expects($this->at(2))
- ->method('values')
- ->with($expected[0])
- ->will($this->returnSelf());
- $query->expects($this->at(3))
- ->method('values')
- ->with($expected[1])
- ->will($this->returnSelf());
- $query->expects($this->at(4))
- ->method('values')
- ->with($expected[2])
- ->will($this->returnSelf());
- $statement = $this->getMockBuilder('\PDOStatement')
- ->setMethods(['closeCursor'])
- ->getMock();
- $statement->expects($this->once())->method('closeCursor');
- $query->expects($this->once())
- ->method('execute')
- ->will($this->returnValue($statement));
- $this->assertSame($statement, $fixture->insert($db));
- }
- /**
- * Test the drop method
- *
- * @return void
- */
- public function testDrop()
- {
- $fixture = new ArticlesFixture();
- $db = $this->getMockBuilder('Cake\Database\Connection')
- ->disableOriginalConstructor()
- ->getMock();
- $statement = $this->getMockBuilder('\PDOStatement')
- ->setMethods(['closeCursor'])
- ->getMock();
- $statement->expects($this->once())->method('closeCursor');
- $db->expects($this->once())->method('execute')
- ->with('sql')
- ->will($this->returnValue($statement));
- $table = $this->getMockBuilder('Cake\Database\Schema\Table')
- ->setConstructorArgs(['articles'])
- ->getMock();
- $table->expects($this->once())
- ->method('dropSql')
- ->with($db)
- ->will($this->returnValue(['sql']));
- $fixture->schema($table);
- $this->assertTrue($fixture->drop($db));
- }
- /**
- * Test the truncate method.
- *
- * @return void
- */
- public function testTruncate()
- {
- $fixture = new ArticlesFixture();
- $db = $this->getMockBuilder('Cake\Database\Connection')
- ->disableOriginalConstructor()
- ->getMock();
- $statement = $this->getMockBuilder('\PDOStatement')
- ->setMethods(['closeCursor'])
- ->getMock();
- $statement->expects($this->once())->method('closeCursor');
- $db->expects($this->once())->method('execute')
- ->with('sql')
- ->will($this->returnValue($statement));
- $table = $this->getMockBuilder('Cake\Database\Schema\Table')
- ->setConstructorArgs(['articles'])
- ->getMock();
- $table->expects($this->once())
- ->method('truncateSql')
- ->with($db)
- ->will($this->returnValue(['sql']));
- $fixture->schema($table);
- $this->assertTrue($fixture->truncate($db));
- }
- }
|