TestFixtureTest.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. <?php
  2. /**
  3. * CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
  4. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * For full copyright and license information, please see the LICENSE.txt
  8. * Redistributions of files must retain the above copyright notice
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  11. * @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
  12. * @since 1.2.0
  13. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Test\TestCase\TestSuite;
  16. use Cake\Core\Configure;
  17. use Cake\Datasource\ConnectionManager;
  18. use Cake\Model\Model;
  19. use Cake\TestSuite\Fixture\TestFixture;
  20. use Cake\TestSuite\TestCase;
  21. use Cake\Utility\ClassRegistry;
  22. /**
  23. * ArticleFixture class
  24. *
  25. */
  26. class ArticleFixture extends TestFixture {
  27. /**
  28. * Table property
  29. *
  30. * @var string
  31. */
  32. public $table = 'articles';
  33. /**
  34. * Fields array
  35. *
  36. * @var array
  37. */
  38. public $fields = [
  39. 'id' => ['type' => 'integer'],
  40. 'name' => ['type' => 'string', 'length' => '255'],
  41. 'created' => ['type' => 'datetime'],
  42. '_constraints' => [
  43. 'primary' => ['type' => 'primary', 'columns' => ['id']]
  44. ]
  45. ];
  46. /**
  47. * Records property
  48. *
  49. * @var array
  50. */
  51. public $records = array(
  52. array('name' => 'Gandalf', 'created' => '2009-04-28 19:20:00'),
  53. array('name' => 'Captain Picard', 'created' => '2009-04-28 19:20:00'),
  54. array('name' => 'Chewbacca', 'created' => '2009-04-28 19:20:00')
  55. );
  56. }
  57. /**
  58. * StringFieldsTestFixture class
  59. *
  60. */
  61. class StringsTestFixture extends TestFixture {
  62. /**
  63. * Table property
  64. *
  65. * @var string
  66. */
  67. public $table = 'strings';
  68. /**
  69. * Fields array
  70. *
  71. * @var array
  72. */
  73. public $fields = array(
  74. 'id' => array('type' => 'integer'),
  75. 'name' => array('type' => 'string', 'length' => '255'),
  76. 'email' => array('type' => 'string', 'length' => '255'),
  77. 'age' => array('type' => 'integer', 'default' => 10)
  78. );
  79. /**
  80. * Records property
  81. *
  82. * @var array
  83. */
  84. public $records = array(
  85. array('name' => 'Mark Doe', 'email' => 'mark.doe@email.com'),
  86. array('name' => 'John Doe', 'email' => 'john.doe@email.com', 'age' => 20),
  87. array('email' => 'jane.doe@email.com', 'name' => 'Jane Doe', 'age' => 30)
  88. );
  89. }
  90. /**
  91. * ImportFixture class
  92. *
  93. */
  94. class ImportFixture extends TestFixture {
  95. /**
  96. * Import property
  97. *
  98. * @var mixed
  99. */
  100. public $import = ['table' => 'posts', 'connection' => 'test'];
  101. }
  102. /**
  103. * Test case for TestFixture
  104. *
  105. */
  106. class TestFixtureTest extends TestCase {
  107. /**
  108. * Fixtures for this test.
  109. *
  110. * @var array
  111. */
  112. public $fixtures = ['core.post'];
  113. /**
  114. * test initializing a static fixture
  115. *
  116. * @return void
  117. */
  118. public function testInitStaticFixture() {
  119. $Fixture = new ArticleFixture();
  120. $this->assertEquals('articles', $Fixture->table);
  121. $Fixture = new ArticleFixture();
  122. $Fixture->table = null;
  123. $Fixture->init();
  124. $this->assertEquals('articles', $Fixture->table);
  125. $schema = $Fixture->schema();
  126. $this->assertInstanceOf('Cake\Database\Schema\Table', $schema);
  127. $fields = $Fixture->fields;
  128. unset($fields['_constraints'], $fields['_indexes']);
  129. $this->assertEquals(
  130. array_keys($fields),
  131. $schema->columns(),
  132. 'Fields do not match'
  133. );
  134. $this->assertEquals(array_keys($Fixture->fields['_constraints']), $schema->constraints());
  135. $this->assertEmpty($schema->indexes());
  136. }
  137. /**
  138. * test import fixture initialization
  139. *
  140. * @return void
  141. */
  142. public function testInitImport() {
  143. $fixture = new ImportFixture();
  144. $fixture->fields = $fixture->records = null;
  145. $fixture->import = [
  146. 'table' => 'posts',
  147. 'connection' => 'test',
  148. ];
  149. $fixture->init();
  150. $expected = [
  151. 'id',
  152. 'author_id',
  153. 'title',
  154. 'body',
  155. 'published',
  156. ];
  157. $this->assertEquals($expected, $fixture->schema()->columns());
  158. }
  159. /**
  160. * test create method
  161. *
  162. * @return void
  163. */
  164. public function testCreate() {
  165. $fixture = new ArticleFixture();
  166. $db = $this->getMock('Cake\Database\Connection', [], [], '', false);
  167. $table = $this->getMock('Cake\Database\Schema\Table', [], ['articles']);
  168. $table->expects($this->once())
  169. ->method('createSql')
  170. ->with($db)
  171. ->will($this->returnValue(['sql', 'sql']));
  172. $fixture->schema($table);
  173. $statement = $this->getMock('\PDOStatement', ['closeCursor']);
  174. $statement->expects($this->atLeastOnce())->method('closeCursor');
  175. $db->expects($this->exactly(2))->method('execute')
  176. ->will($this->returnValue($statement));
  177. $this->assertTrue($fixture->create($db));
  178. }
  179. /**
  180. * test create method, trigger error
  181. *
  182. * @expectedException PHPUnit_Framework_Error
  183. * @return void
  184. */
  185. public function testCreateError() {
  186. $fixture = new ArticleFixture();
  187. $db = $this->getMock('Cake\Database\Connection', [], [], '', false);
  188. $table = $this->getMock('Cake\Database\Schema\Table', [], ['articles']);
  189. $table->expects($this->once())
  190. ->method('createSql')
  191. ->with($db)
  192. ->will($this->throwException(new \Exception('oh noes')));
  193. $fixture->schema($table);
  194. $fixture->create($db);
  195. }
  196. /**
  197. * test the insert method
  198. *
  199. * @return void
  200. */
  201. public function testInsert() {
  202. $fixture = new ArticleFixture();
  203. $db = $this->getMock('Cake\Database\Connection', [], [], '', false);
  204. $query = $this->getMock('Cake\Database\Query', [], [$db]);
  205. $db->expects($this->once())
  206. ->method('newQuery')
  207. ->will($this->returnValue($query));
  208. $query->expects($this->once())
  209. ->method('insert')
  210. ->with(['name', 'created'], ['string', 'datetime'])
  211. ->will($this->returnSelf());
  212. $query->expects($this->once())
  213. ->method('into')
  214. ->with('articles')
  215. ->will($this->returnSelf());
  216. $expected = [
  217. ['name' => 'Gandalf', 'created' => '2009-04-28 19:20:00'],
  218. ['name' => 'Captain Picard', 'created' => '2009-04-28 19:20:00'],
  219. ['name' => 'Chewbacca', 'created' => '2009-04-28 19:20:00']
  220. ];
  221. $query->expects($this->at(2))
  222. ->method('values')
  223. ->with($expected[0])
  224. ->will($this->returnSelf());
  225. $query->expects($this->at(3))
  226. ->method('values')
  227. ->with($expected[1])
  228. ->will($this->returnSelf());
  229. $query->expects($this->at(4))
  230. ->method('values')
  231. ->with($expected[2])
  232. ->will($this->returnSelf());
  233. $statement = $this->getMock('\PDOStatement', ['closeCursor']);
  234. $statement->expects($this->once())->method('closeCursor');
  235. $query->expects($this->once())
  236. ->method('execute')
  237. ->will($this->returnValue($statement));
  238. $this->assertSame($statement, $fixture->insert($db));
  239. }
  240. /**
  241. * test the insert method
  242. *
  243. * @return void
  244. */
  245. public function testInsertStrings() {
  246. $fixture = new StringsTestFixture();
  247. $db = $this->getMock('Cake\Database\Connection', [], [], '', false);
  248. $query = $this->getMock('Cake\Database\Query', [], [$db]);
  249. $db->expects($this->once())
  250. ->method('newQuery')
  251. ->will($this->returnValue($query));
  252. $query->expects($this->once())
  253. ->method('insert')
  254. ->with(['name', 'email', 'age'], ['string', 'string', 'integer'])
  255. ->will($this->returnSelf());
  256. $query->expects($this->once())
  257. ->method('into')
  258. ->with('strings')
  259. ->will($this->returnSelf());
  260. $expected = [
  261. ['name' => 'Mark Doe', 'email' => 'mark.doe@email.com', 'age' => null],
  262. ['name' => 'John Doe', 'email' => 'john.doe@email.com', 'age' => 20],
  263. ['name' => 'Jane Doe', 'email' => 'jane.doe@email.com', 'age' => 30],
  264. ];
  265. $query->expects($this->at(2))
  266. ->method('values')
  267. ->with($expected[0])
  268. ->will($this->returnSelf());
  269. $query->expects($this->at(3))
  270. ->method('values')
  271. ->with($expected[1])
  272. ->will($this->returnSelf());
  273. $query->expects($this->at(4))
  274. ->method('values')
  275. ->with($expected[2])
  276. ->will($this->returnSelf());
  277. $statement = $this->getMock('\PDOStatement', ['closeCursor']);
  278. $statement->expects($this->once())->method('closeCursor');
  279. $query->expects($this->once())
  280. ->method('execute')
  281. ->will($this->returnValue($statement));
  282. $this->assertSame($statement, $fixture->insert($db));
  283. }
  284. /**
  285. * Test the drop method
  286. *
  287. * @return void
  288. */
  289. public function testDrop() {
  290. $fixture = new ArticleFixture();
  291. $db = $this->getMock('Cake\Database\Connection', [], [], '', false);
  292. $statement = $this->getMock('\PDOStatement', ['closeCursor']);
  293. $statement->expects($this->once())->method('closeCursor');
  294. $db->expects($this->once())->method('execute')
  295. ->with('sql')
  296. ->will($this->returnValue($statement));
  297. $table = $this->getMock('Cake\Database\Schema\Table', [], ['articles']);
  298. $table->expects($this->once())
  299. ->method('dropSql')
  300. ->with($db)
  301. ->will($this->returnValue(['sql']));
  302. $fixture->schema($table);
  303. $this->assertTrue($fixture->drop($db));
  304. }
  305. /**
  306. * Test the truncate method.
  307. *
  308. * @return void
  309. */
  310. public function testTruncate() {
  311. $fixture = new ArticleFixture();
  312. $db = $this->getMock('Cake\Database\Connection', [], [], '', false);
  313. $statement = $this->getMock('\PDOStatement', ['closeCursor']);
  314. $statement->expects($this->once())->method('closeCursor');
  315. $db->expects($this->once())->method('execute')
  316. ->with('sql')
  317. ->will($this->returnValue($statement));
  318. $table = $this->getMock('Cake\Database\Schema\Table', [], ['articles']);
  319. $table->expects($this->once())
  320. ->method('truncateSql')
  321. ->with($db)
  322. ->will($this->returnValue(['sql']));
  323. $fixture->schema($table);
  324. $this->assertTrue($fixture->truncate($db));
  325. }
  326. }