| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455 |
- <?php
- /**
- * CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
- * 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://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
- * @since 1.2.0
- * @license http://www.opensource.org/licenses/mit-license.php MIT License
- */
- namespace Cake\Test\TestCase\TestSuite;
- 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!']
- ];
- }
- /**
- * 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 create method
- *
- * @return void
- */
- public function testCreate()
- {
- $fixture = new ArticlesFixture();
- $db = $this->getMock('Cake\Database\Connection', [], [], '', false);
- $table = $this->getMock('Cake\Database\Schema\Table', [], ['articles']);
- $table->expects($this->once())
- ->method('createSql')
- ->with($db)
- ->will($this->returnValue(['sql', 'sql']));
- $fixture->schema($table);
- $statement = $this->getMock('\PDOStatement', ['closeCursor']);
- $statement->expects($this->atLeastOnce())->method('closeCursor');
- $db->expects($this->exactly(2))->method('execute')
- ->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->getMock('Cake\Database\Connection', [], [], '', false);
- $table = $this->getMock('Cake\Database\Schema\Table', [], ['articles']);
- $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->getMock('Cake\Database\Connection', [], [], '', false);
- $query = $this->getMock('Cake\Database\Query', [], [$db]);
- $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->getMock('\PDOStatement', ['closeCursor']);
- $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->getMock('Cake\Database\Connection', [], [], '', false);
- $query = $this->getMock('Cake\Database\Query', [], [$db]);
- $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->getMock('\PDOStatement', ['closeCursor']);
- $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->getMock('Cake\Database\Connection', [], [], '', false);
- $query = $this->getMock('Cake\Database\Query', [], [$db]);
- $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->getMock('\PDOStatement', ['closeCursor']);
- $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->getMock('Cake\Database\Connection', [], [], '', false);
- $statement = $this->getMock('\PDOStatement', ['closeCursor']);
- $statement->expects($this->once())->method('closeCursor');
- $db->expects($this->once())->method('execute')
- ->with('sql')
- ->will($this->returnValue($statement));
- $table = $this->getMock('Cake\Database\Schema\Table', [], ['articles']);
- $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->getMock('Cake\Database\Connection', [], [], '', false);
- $statement = $this->getMock('\PDOStatement', ['closeCursor']);
- $statement->expects($this->once())->method('closeCursor');
- $db->expects($this->once())->method('execute')
- ->with('sql')
- ->will($this->returnValue($statement));
- $table = $this->getMock('Cake\Database\Schema\Table', [], ['articles']);
- $table->expects($this->once())
- ->method('truncateSql')
- ->with($db)
- ->will($this->returnValue(['sql']));
- $fixture->schema($table);
- $this->assertTrue($fixture->truncate($db));
- }
- }
|