TestFixtureTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  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\Log\Log;
  17. use Cake\TestSuite\Fixture\TestFixture;
  18. use Cake\TestSuite\TestCase;
  19. use Exception;
  20. /**
  21. * ArticlesFixture class
  22. *
  23. */
  24. class ArticlesFixture extends TestFixture
  25. {
  26. /**
  27. * Table property
  28. *
  29. * @var string
  30. */
  31. public $table = 'articles';
  32. /**
  33. * Fields array
  34. *
  35. * @var array
  36. */
  37. public $fields = [
  38. 'id' => ['type' => 'integer'],
  39. 'name' => ['type' => 'string', 'length' => '255'],
  40. 'created' => ['type' => 'datetime'],
  41. '_constraints' => [
  42. 'primary' => ['type' => 'primary', 'columns' => ['id']]
  43. ]
  44. ];
  45. /**
  46. * Records property
  47. *
  48. * @var array
  49. */
  50. public $records = [
  51. ['name' => 'Gandalf', 'created' => '2009-04-28 19:20:00'],
  52. ['name' => 'Captain Picard', 'created' => '2009-04-28 19:20:00'],
  53. ['name' => 'Chewbacca', 'created' => '2009-04-28 19:20:00']
  54. ];
  55. }
  56. /**
  57. * StringsTestsFixture class
  58. *
  59. */
  60. class StringsTestsFixture extends TestFixture
  61. {
  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 = [
  74. 'id' => ['type' => 'integer'],
  75. 'name' => ['type' => 'string', 'length' => '255'],
  76. 'email' => ['type' => 'string', 'length' => '255'],
  77. 'age' => ['type' => 'integer', 'default' => 10]
  78. ];
  79. /**
  80. * Records property
  81. *
  82. * @var array
  83. */
  84. public $records = [
  85. ['name' => 'Mark Doe', 'email' => 'mark.doe@email.com'],
  86. ['name' => 'John Doe', 'email' => 'john.doe@email.com', 'age' => 20],
  87. ['email' => 'jane.doe@email.com', 'name' => 'Jane Doe', 'age' => 30]
  88. ];
  89. }
  90. /**
  91. * ImportsFixture class
  92. *
  93. */
  94. class ImportsFixture extends TestFixture
  95. {
  96. /**
  97. * Import property
  98. *
  99. * @var mixed
  100. */
  101. public $import = ['table' => 'posts', 'connection' => 'test'];
  102. /**
  103. * Records property
  104. *
  105. * @var array
  106. */
  107. public $records = [
  108. ['title' => 'Hello!', 'body' => 'Hello world!']
  109. ];
  110. }
  111. /**
  112. * Test case for TestFixture
  113. *
  114. */
  115. class TestFixtureTest extends TestCase
  116. {
  117. /**
  118. * Fixtures for this test.
  119. *
  120. * @var array
  121. */
  122. public $fixtures = ['core.posts'];
  123. /**
  124. * Set up
  125. *
  126. * @return void
  127. */
  128. public function setUp()
  129. {
  130. parent::setUp();
  131. Log::reset();
  132. }
  133. /**
  134. * Tear down
  135. *
  136. * @return void
  137. */
  138. public function tearDown()
  139. {
  140. parent::tearDown();
  141. Log::reset();
  142. }
  143. /**
  144. * test initializing a static fixture
  145. *
  146. * @return void
  147. */
  148. public function testInitStaticFixture()
  149. {
  150. $Fixture = new ArticlesFixture();
  151. $this->assertEquals('articles', $Fixture->table);
  152. $Fixture = new ArticlesFixture();
  153. $Fixture->table = null;
  154. $Fixture->init();
  155. $this->assertEquals('articles', $Fixture->table);
  156. $schema = $Fixture->schema();
  157. $this->assertInstanceOf('Cake\Database\Schema\Table', $schema);
  158. $fields = $Fixture->fields;
  159. unset($fields['_constraints'], $fields['_indexes']);
  160. $this->assertEquals(
  161. array_keys($fields),
  162. $schema->columns(),
  163. 'Fields do not match'
  164. );
  165. $this->assertEquals(array_keys($Fixture->fields['_constraints']), $schema->constraints());
  166. $this->assertEmpty($schema->indexes());
  167. }
  168. /**
  169. * test import fixture initialization
  170. *
  171. * @return void
  172. */
  173. public function testInitImport()
  174. {
  175. $fixture = new ImportsFixture();
  176. $fixture->fields = $fixture->records = null;
  177. $fixture->import = [
  178. 'table' => 'posts',
  179. 'connection' => 'test',
  180. ];
  181. $fixture->init();
  182. $expected = [
  183. 'id',
  184. 'author_id',
  185. 'title',
  186. 'body',
  187. 'published',
  188. ];
  189. $this->assertEquals($expected, $fixture->schema()->columns());
  190. }
  191. /**
  192. * test create method
  193. *
  194. * @return void
  195. */
  196. public function testCreate()
  197. {
  198. $fixture = new ArticlesFixture();
  199. $db = $this->getMock('Cake\Database\Connection', [], [], '', false);
  200. $table = $this->getMock('Cake\Database\Schema\Table', [], ['articles']);
  201. $table->expects($this->once())
  202. ->method('createSql')
  203. ->with($db)
  204. ->will($this->returnValue(['sql', 'sql']));
  205. $fixture->schema($table);
  206. $statement = $this->getMock('\PDOStatement', ['closeCursor']);
  207. $statement->expects($this->atLeastOnce())->method('closeCursor');
  208. $db->expects($this->exactly(2))->method('execute')
  209. ->will($this->returnValue($statement));
  210. $this->assertTrue($fixture->create($db));
  211. }
  212. /**
  213. * test create method, trigger error
  214. *
  215. * @expectedException \PHPUnit_Framework_Error
  216. * @return void
  217. */
  218. public function testCreateError()
  219. {
  220. $fixture = new ArticlesFixture();
  221. $db = $this->getMock('Cake\Database\Connection', [], [], '', false);
  222. $table = $this->getMock('Cake\Database\Schema\Table', [], ['articles']);
  223. $table->expects($this->once())
  224. ->method('createSql')
  225. ->with($db)
  226. ->will($this->throwException(new Exception('oh noes')));
  227. $fixture->schema($table);
  228. $fixture->create($db);
  229. }
  230. /**
  231. * test the insert method
  232. *
  233. * @return void
  234. */
  235. public function testInsert()
  236. {
  237. $fixture = new ArticlesFixture();
  238. $db = $this->getMock('Cake\Database\Connection', [], [], '', false);
  239. $query = $this->getMock('Cake\Database\Query', [], [$db]);
  240. $db->expects($this->once())
  241. ->method('newQuery')
  242. ->will($this->returnValue($query));
  243. $query->expects($this->once())
  244. ->method('insert')
  245. ->with(['name', 'created'], ['name' => 'string', 'created' => 'datetime'])
  246. ->will($this->returnSelf());
  247. $query->expects($this->once())
  248. ->method('into')
  249. ->with('articles')
  250. ->will($this->returnSelf());
  251. $expected = [
  252. ['name' => 'Gandalf', 'created' => '2009-04-28 19:20:00'],
  253. ['name' => 'Captain Picard', 'created' => '2009-04-28 19:20:00'],
  254. ['name' => 'Chewbacca', 'created' => '2009-04-28 19:20:00']
  255. ];
  256. $query->expects($this->at(2))
  257. ->method('values')
  258. ->with($expected[0])
  259. ->will($this->returnSelf());
  260. $query->expects($this->at(3))
  261. ->method('values')
  262. ->with($expected[1])
  263. ->will($this->returnSelf());
  264. $query->expects($this->at(4))
  265. ->method('values')
  266. ->with($expected[2])
  267. ->will($this->returnSelf());
  268. $statement = $this->getMock('\PDOStatement', ['closeCursor']);
  269. $statement->expects($this->once())->method('closeCursor');
  270. $query->expects($this->once())
  271. ->method('execute')
  272. ->will($this->returnValue($statement));
  273. $this->assertSame($statement, $fixture->insert($db));
  274. }
  275. /**
  276. * test the insert method
  277. *
  278. * @return void
  279. */
  280. public function testInsertImport()
  281. {
  282. $fixture = new ImportsFixture();
  283. $db = $this->getMock('Cake\Database\Connection', [], [], '', false);
  284. $query = $this->getMock('Cake\Database\Query', [], [$db]);
  285. $db->expects($this->once())
  286. ->method('newQuery')
  287. ->will($this->returnValue($query));
  288. $query->expects($this->once())
  289. ->method('insert')
  290. ->with(['title', 'body'], ['title' => 'string', 'body' => 'text'])
  291. ->will($this->returnSelf());
  292. $query->expects($this->once())
  293. ->method('into')
  294. ->with('posts')
  295. ->will($this->returnSelf());
  296. $expected = [
  297. ['title' => 'Hello!', 'body' => 'Hello world!'],
  298. ];
  299. $query->expects($this->at(2))
  300. ->method('values')
  301. ->with($expected[0])
  302. ->will($this->returnSelf());
  303. $statement = $this->getMock('\PDOStatement', ['closeCursor']);
  304. $statement->expects($this->once())->method('closeCursor');
  305. $query->expects($this->once())
  306. ->method('execute')
  307. ->will($this->returnValue($statement));
  308. $this->assertSame($statement, $fixture->insert($db));
  309. }
  310. /**
  311. * test the insert method
  312. *
  313. * @return void
  314. */
  315. public function testInsertStrings()
  316. {
  317. $fixture = new StringsTestsFixture();
  318. $db = $this->getMock('Cake\Database\Connection', [], [], '', false);
  319. $query = $this->getMock('Cake\Database\Query', [], [$db]);
  320. $db->expects($this->once())
  321. ->method('newQuery')
  322. ->will($this->returnValue($query));
  323. $query->expects($this->once())
  324. ->method('insert')
  325. ->with(['name', 'email', 'age'], ['name' => 'string', 'email' => 'string', 'age' => 'integer'])
  326. ->will($this->returnSelf());
  327. $query->expects($this->once())
  328. ->method('into')
  329. ->with('strings')
  330. ->will($this->returnSelf());
  331. $expected = [
  332. ['name' => 'Mark Doe', 'email' => 'mark.doe@email.com', 'age' => null],
  333. ['name' => 'John Doe', 'email' => 'john.doe@email.com', 'age' => 20],
  334. ['name' => 'Jane Doe', 'email' => 'jane.doe@email.com', 'age' => 30],
  335. ];
  336. $query->expects($this->at(2))
  337. ->method('values')
  338. ->with($expected[0])
  339. ->will($this->returnSelf());
  340. $query->expects($this->at(3))
  341. ->method('values')
  342. ->with($expected[1])
  343. ->will($this->returnSelf());
  344. $query->expects($this->at(4))
  345. ->method('values')
  346. ->with($expected[2])
  347. ->will($this->returnSelf());
  348. $statement = $this->getMock('\PDOStatement', ['closeCursor']);
  349. $statement->expects($this->once())->method('closeCursor');
  350. $query->expects($this->once())
  351. ->method('execute')
  352. ->will($this->returnValue($statement));
  353. $this->assertSame($statement, $fixture->insert($db));
  354. }
  355. /**
  356. * Test the drop method
  357. *
  358. * @return void
  359. */
  360. public function testDrop()
  361. {
  362. $fixture = new ArticlesFixture();
  363. $db = $this->getMock('Cake\Database\Connection', [], [], '', false);
  364. $statement = $this->getMock('\PDOStatement', ['closeCursor']);
  365. $statement->expects($this->once())->method('closeCursor');
  366. $db->expects($this->once())->method('execute')
  367. ->with('sql')
  368. ->will($this->returnValue($statement));
  369. $table = $this->getMock('Cake\Database\Schema\Table', [], ['articles']);
  370. $table->expects($this->once())
  371. ->method('dropSql')
  372. ->with($db)
  373. ->will($this->returnValue(['sql']));
  374. $fixture->schema($table);
  375. $this->assertTrue($fixture->drop($db));
  376. }
  377. /**
  378. * Test the truncate method.
  379. *
  380. * @return void
  381. */
  382. public function testTruncate()
  383. {
  384. $fixture = new ArticlesFixture();
  385. $db = $this->getMock('Cake\Database\Connection', [], [], '', false);
  386. $statement = $this->getMock('\PDOStatement', ['closeCursor']);
  387. $statement->expects($this->once())->method('closeCursor');
  388. $db->expects($this->once())->method('execute')
  389. ->with('sql')
  390. ->will($this->returnValue($statement));
  391. $table = $this->getMock('Cake\Database\Schema\Table', [], ['articles']);
  392. $table->expects($this->once())
  393. ->method('truncateSql')
  394. ->with($db)
  395. ->will($this->returnValue(['sql']));
  396. $fixture->schema($table);
  397. $this->assertTrue($fixture->truncate($db));
  398. }
  399. }