TestFixtureTest.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  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://cakephp.org CakePHP(tm) Project
  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 import fixture initialization
  193. *
  194. * @return void
  195. */
  196. public function testInitImportModel()
  197. {
  198. $fixture = new ImportsFixture();
  199. $fixture->fields = $fixture->records = null;
  200. $fixture->import = [
  201. 'model' => 'Posts',
  202. 'connection' => 'test',
  203. ];
  204. $fixture->init();
  205. $expected = [
  206. 'id',
  207. 'author_id',
  208. 'title',
  209. 'body',
  210. 'published',
  211. ];
  212. $this->assertEquals($expected, $fixture->schema()->columns());
  213. }
  214. /**
  215. * test create method
  216. *
  217. * @return void
  218. */
  219. public function testCreate()
  220. {
  221. $fixture = new ArticlesFixture();
  222. $db = $this->getMock('Cake\Database\Connection', [], [], '', false);
  223. $table = $this->getMock('Cake\Database\Schema\Table', [], ['articles']);
  224. $table->expects($this->once())
  225. ->method('createSql')
  226. ->with($db)
  227. ->will($this->returnValue(['sql', 'sql']));
  228. $fixture->schema($table);
  229. $statement = $this->getMock('\PDOStatement', ['execute', 'closeCursor']);
  230. $statement->expects($this->atLeastOnce())->method('closeCursor');
  231. $statement->expects($this->atLeastOnce())->method('execute');
  232. $db->expects($this->exactly(2))
  233. ->method('prepare')
  234. ->will($this->returnValue($statement));
  235. $this->assertTrue($fixture->create($db));
  236. }
  237. /**
  238. * test create method, trigger error
  239. *
  240. * @expectedException \PHPUnit_Framework_Error
  241. * @return void
  242. */
  243. public function testCreateError()
  244. {
  245. $fixture = new ArticlesFixture();
  246. $db = $this->getMock('Cake\Database\Connection', [], [], '', false);
  247. $table = $this->getMock('Cake\Database\Schema\Table', [], ['articles']);
  248. $table->expects($this->once())
  249. ->method('createSql')
  250. ->with($db)
  251. ->will($this->throwException(new Exception('oh noes')));
  252. $fixture->schema($table);
  253. $fixture->create($db);
  254. }
  255. /**
  256. * test the insert method
  257. *
  258. * @return void
  259. */
  260. public function testInsert()
  261. {
  262. $fixture = new ArticlesFixture();
  263. $db = $this->getMock('Cake\Database\Connection', [], [], '', false);
  264. $query = $this->getMock('Cake\Database\Query', [], [$db]);
  265. $db->expects($this->once())
  266. ->method('newQuery')
  267. ->will($this->returnValue($query));
  268. $query->expects($this->once())
  269. ->method('insert')
  270. ->with(['name', 'created'], ['name' => 'string', 'created' => 'datetime'])
  271. ->will($this->returnSelf());
  272. $query->expects($this->once())
  273. ->method('into')
  274. ->with('articles')
  275. ->will($this->returnSelf());
  276. $expected = [
  277. ['name' => 'Gandalf', 'created' => '2009-04-28 19:20:00'],
  278. ['name' => 'Captain Picard', 'created' => '2009-04-28 19:20:00'],
  279. ['name' => 'Chewbacca', 'created' => '2009-04-28 19:20:00']
  280. ];
  281. $query->expects($this->at(2))
  282. ->method('values')
  283. ->with($expected[0])
  284. ->will($this->returnSelf());
  285. $query->expects($this->at(3))
  286. ->method('values')
  287. ->with($expected[1])
  288. ->will($this->returnSelf());
  289. $query->expects($this->at(4))
  290. ->method('values')
  291. ->with($expected[2])
  292. ->will($this->returnSelf());
  293. $statement = $this->getMock('\PDOStatement', ['closeCursor']);
  294. $statement->expects($this->once())->method('closeCursor');
  295. $query->expects($this->once())
  296. ->method('execute')
  297. ->will($this->returnValue($statement));
  298. $this->assertSame($statement, $fixture->insert($db));
  299. }
  300. /**
  301. * test the insert method
  302. *
  303. * @return void
  304. */
  305. public function testInsertImport()
  306. {
  307. $fixture = new ImportsFixture();
  308. $db = $this->getMock('Cake\Database\Connection', [], [], '', false);
  309. $query = $this->getMock('Cake\Database\Query', [], [$db]);
  310. $db->expects($this->once())
  311. ->method('newQuery')
  312. ->will($this->returnValue($query));
  313. $query->expects($this->once())
  314. ->method('insert')
  315. ->with(['title', 'body'], ['title' => 'string', 'body' => 'text'])
  316. ->will($this->returnSelf());
  317. $query->expects($this->once())
  318. ->method('into')
  319. ->with('posts')
  320. ->will($this->returnSelf());
  321. $expected = [
  322. ['title' => 'Hello!', 'body' => 'Hello world!'],
  323. ];
  324. $query->expects($this->at(2))
  325. ->method('values')
  326. ->with($expected[0])
  327. ->will($this->returnSelf());
  328. $statement = $this->getMock('\PDOStatement', ['closeCursor']);
  329. $statement->expects($this->once())->method('closeCursor');
  330. $query->expects($this->once())
  331. ->method('execute')
  332. ->will($this->returnValue($statement));
  333. $this->assertSame($statement, $fixture->insert($db));
  334. }
  335. /**
  336. * test the insert method
  337. *
  338. * @return void
  339. */
  340. public function testInsertStrings()
  341. {
  342. $fixture = new StringsTestsFixture();
  343. $db = $this->getMock('Cake\Database\Connection', [], [], '', false);
  344. $query = $this->getMock('Cake\Database\Query', [], [$db]);
  345. $db->expects($this->once())
  346. ->method('newQuery')
  347. ->will($this->returnValue($query));
  348. $query->expects($this->once())
  349. ->method('insert')
  350. ->with(['name', 'email', 'age'], ['name' => 'string', 'email' => 'string', 'age' => 'integer'])
  351. ->will($this->returnSelf());
  352. $query->expects($this->once())
  353. ->method('into')
  354. ->with('strings')
  355. ->will($this->returnSelf());
  356. $expected = [
  357. ['name' => 'Mark Doe', 'email' => 'mark.doe@email.com', 'age' => null],
  358. ['name' => 'John Doe', 'email' => 'john.doe@email.com', 'age' => 20],
  359. ['name' => 'Jane Doe', 'email' => 'jane.doe@email.com', 'age' => 30],
  360. ];
  361. $query->expects($this->at(2))
  362. ->method('values')
  363. ->with($expected[0])
  364. ->will($this->returnSelf());
  365. $query->expects($this->at(3))
  366. ->method('values')
  367. ->with($expected[1])
  368. ->will($this->returnSelf());
  369. $query->expects($this->at(4))
  370. ->method('values')
  371. ->with($expected[2])
  372. ->will($this->returnSelf());
  373. $statement = $this->getMock('\PDOStatement', ['closeCursor']);
  374. $statement->expects($this->once())->method('closeCursor');
  375. $query->expects($this->once())
  376. ->method('execute')
  377. ->will($this->returnValue($statement));
  378. $this->assertSame($statement, $fixture->insert($db));
  379. }
  380. /**
  381. * Test the drop method
  382. *
  383. * @return void
  384. */
  385. public function testDrop()
  386. {
  387. $fixture = new ArticlesFixture();
  388. $db = $this->getMock('Cake\Database\Connection', [], [], '', false);
  389. $statement = $this->getMock('\PDOStatement', ['closeCursor']);
  390. $statement->expects($this->once())->method('closeCursor');
  391. $db->expects($this->once())->method('execute')
  392. ->with('sql')
  393. ->will($this->returnValue($statement));
  394. $table = $this->getMock('Cake\Database\Schema\Table', [], ['articles']);
  395. $table->expects($this->once())
  396. ->method('dropSql')
  397. ->with($db)
  398. ->will($this->returnValue(['sql']));
  399. $fixture->schema($table);
  400. $this->assertTrue($fixture->drop($db));
  401. }
  402. /**
  403. * Test the truncate method.
  404. *
  405. * @return void
  406. */
  407. public function testTruncate()
  408. {
  409. $fixture = new ArticlesFixture();
  410. $db = $this->getMock('Cake\Database\Connection', [], [], '', false);
  411. $statement = $this->getMock('\PDOStatement', ['closeCursor']);
  412. $statement->expects($this->once())->method('closeCursor');
  413. $db->expects($this->once())->method('execute')
  414. ->with('sql')
  415. ->will($this->returnValue($statement));
  416. $table = $this->getMock('Cake\Database\Schema\Table', [], ['articles']);
  417. $table->expects($this->once())
  418. ->method('truncateSql')
  419. ->with($db)
  420. ->will($this->returnValue(['sql']));
  421. $fixture->schema($table);
  422. $this->assertTrue($fixture->truncate($db));
  423. }
  424. }