TestFixtureTest.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553
  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\Database\Schema\Table;
  17. use Cake\Datasource\ConnectionManager;
  18. use Cake\Log\Log;
  19. use Cake\TestSuite\Fixture\TestFixture;
  20. use Cake\TestSuite\TestCase;
  21. use Exception;
  22. /**
  23. * ArticlesFixture class
  24. *
  25. */
  26. class ArticlesFixture extends TestFixture
  27. {
  28. /**
  29. * Table property
  30. *
  31. * @var string
  32. */
  33. public $table = 'articles';
  34. /**
  35. * Fields array
  36. *
  37. * @var array
  38. */
  39. public $fields = [
  40. 'id' => ['type' => 'integer'],
  41. 'name' => ['type' => 'string', 'length' => '255'],
  42. 'created' => ['type' => 'datetime'],
  43. '_constraints' => [
  44. 'primary' => ['type' => 'primary', 'columns' => ['id']]
  45. ]
  46. ];
  47. /**
  48. * Records property
  49. *
  50. * @var array
  51. */
  52. public $records = [
  53. ['name' => 'Gandalf', 'created' => '2009-04-28 19:20:00'],
  54. ['name' => 'Captain Picard', 'created' => '2009-04-28 19:20:00'],
  55. ['name' => 'Chewbacca', 'created' => '2009-04-28 19:20:00']
  56. ];
  57. }
  58. /**
  59. * StringsTestsFixture class
  60. *
  61. */
  62. class StringsTestsFixture extends TestFixture
  63. {
  64. /**
  65. * Table property
  66. *
  67. * @var string
  68. */
  69. public $table = 'strings';
  70. /**
  71. * Fields array
  72. *
  73. * @var array
  74. */
  75. public $fields = [
  76. 'id' => ['type' => 'integer'],
  77. 'name' => ['type' => 'string', 'length' => '255'],
  78. 'email' => ['type' => 'string', 'length' => '255'],
  79. 'age' => ['type' => 'integer', 'default' => 10]
  80. ];
  81. /**
  82. * Records property
  83. *
  84. * @var array
  85. */
  86. public $records = [
  87. ['name' => 'Mark Doe', 'email' => 'mark.doe@email.com'],
  88. ['name' => 'John Doe', 'email' => 'john.doe@email.com', 'age' => 20],
  89. ['email' => 'jane.doe@email.com', 'name' => 'Jane Doe', 'age' => 30]
  90. ];
  91. }
  92. /**
  93. * ImportsFixture class
  94. *
  95. */
  96. class ImportsFixture extends TestFixture
  97. {
  98. /**
  99. * Import property
  100. *
  101. * @var mixed
  102. */
  103. public $import = ['table' => 'posts', 'connection' => 'test'];
  104. /**
  105. * Records property
  106. *
  107. * @var array
  108. */
  109. public $records = [
  110. ['title' => 'Hello!', 'body' => 'Hello world!']
  111. ];
  112. }
  113. /**
  114. * This class allows testing the fixture data insertion when the properties
  115. * $fields and $import are not set
  116. *
  117. */
  118. class LettersFixture extends TestFixture
  119. {
  120. /**
  121. * records property
  122. *
  123. * @var array
  124. */
  125. public $records = [
  126. ['letter' => 'a'],
  127. ['letter' => 'b'],
  128. ['letter' => 'c']
  129. ];
  130. }
  131. /**
  132. * Test case for TestFixture
  133. *
  134. */
  135. class TestFixtureTest extends TestCase
  136. {
  137. /**
  138. * Fixtures for this test.
  139. *
  140. * @var array
  141. */
  142. public $fixtures = ['core.posts'];
  143. /**
  144. * Set up
  145. *
  146. * @return void
  147. */
  148. public function setUp()
  149. {
  150. parent::setUp();
  151. Log::reset();
  152. }
  153. /**
  154. * Tear down
  155. *
  156. * @return void
  157. */
  158. public function tearDown()
  159. {
  160. parent::tearDown();
  161. Log::reset();
  162. }
  163. /**
  164. * test initializing a static fixture
  165. *
  166. * @return void
  167. */
  168. public function testInitStaticFixture()
  169. {
  170. $Fixture = new ArticlesFixture();
  171. $this->assertEquals('articles', $Fixture->table);
  172. $Fixture = new ArticlesFixture();
  173. $Fixture->table = null;
  174. $Fixture->init();
  175. $this->assertEquals('articles', $Fixture->table);
  176. $schema = $Fixture->schema();
  177. $this->assertInstanceOf('Cake\Database\Schema\Table', $schema);
  178. $fields = $Fixture->fields;
  179. unset($fields['_constraints'], $fields['_indexes']);
  180. $this->assertEquals(
  181. array_keys($fields),
  182. $schema->columns(),
  183. 'Fields do not match'
  184. );
  185. $this->assertEquals(array_keys($Fixture->fields['_constraints']), $schema->constraints());
  186. $this->assertEmpty($schema->indexes());
  187. }
  188. /**
  189. * test import fixture initialization
  190. *
  191. * @return void
  192. */
  193. public function testInitImport()
  194. {
  195. $fixture = new ImportsFixture();
  196. $fixture->fields = $fixture->records = null;
  197. $fixture->import = [
  198. 'table' => 'posts',
  199. 'connection' => 'test',
  200. ];
  201. $fixture->init();
  202. $expected = [
  203. 'id',
  204. 'author_id',
  205. 'title',
  206. 'body',
  207. 'published',
  208. ];
  209. $this->assertEquals($expected, $fixture->schema()->columns());
  210. }
  211. /**
  212. * test import fixture initialization
  213. *
  214. * @return void
  215. */
  216. public function testInitImportModel()
  217. {
  218. $fixture = new ImportsFixture();
  219. $fixture->fields = $fixture->records = null;
  220. $fixture->import = [
  221. 'model' => 'Posts',
  222. 'connection' => 'test',
  223. ];
  224. $fixture->init();
  225. $expected = [
  226. 'id',
  227. 'author_id',
  228. 'title',
  229. 'body',
  230. 'published',
  231. ];
  232. $this->assertEquals($expected, $fixture->schema()->columns());
  233. }
  234. /**
  235. * test schema reflection without $import or $fields and without the table existing
  236. * it will throw an exception
  237. *
  238. * @expectedException \Cake\Core\Exception\Exception
  239. * @expectedExceptionMessage Cannot describe schema for table `letters` for fixture `Cake\Test\TestCase\TestSuite\LettersFixture` : the table does not exist.
  240. * @return void
  241. */
  242. public function testInitNoImportNoFieldsException()
  243. {
  244. $fixture = new LettersFixture();
  245. $fixture->init();
  246. }
  247. /**
  248. * test schema reflection without $import or $fields will reflect the schema
  249. *
  250. * @return void
  251. */
  252. public function testInitNoImportNoFields()
  253. {
  254. $db = ConnectionManager::get('test');
  255. $collection = $db->schemaCollection();
  256. if (!in_array('letters', $collection->listTables())) {
  257. $table = new Table('letters', [
  258. 'id' => ['type' => 'integer'],
  259. 'letter' => ['type' => 'string', 'length' => 1]
  260. ]);
  261. $table->addConstraint('primary', ['type' => 'primary', 'columns' => ['id']]);
  262. $sql = $table->createSql($db);
  263. foreach ($sql as $stmt) {
  264. $db->execute($stmt);
  265. }
  266. }
  267. $fixture = new LettersFixture();
  268. $fixture->init();
  269. $this->assertEquals(['id', 'letter'], $fixture->schema()->columns());
  270. $db = $this->getMock('Cake\Database\Connection', ['prepare', 'execute'], [], '', false);
  271. $db->expects($this->never())
  272. ->method('prepare');
  273. $db->expects($this->never())
  274. ->method('execute');
  275. $this->assertTrue($fixture->create($db));
  276. $this->assertTrue($fixture->drop($db));
  277. }
  278. /**
  279. * test create method
  280. *
  281. * @return void
  282. */
  283. public function testCreate()
  284. {
  285. $fixture = new ArticlesFixture();
  286. $db = $this->getMock('Cake\Database\Connection', [], [], '', false);
  287. $table = $this->getMock('Cake\Database\Schema\Table', [], ['articles']);
  288. $table->expects($this->once())
  289. ->method('createSql')
  290. ->with($db)
  291. ->will($this->returnValue(['sql', 'sql']));
  292. $fixture->schema($table);
  293. $statement = $this->getMock('\PDOStatement', ['execute', 'closeCursor']);
  294. $statement->expects($this->atLeastOnce())->method('closeCursor');
  295. $statement->expects($this->atLeastOnce())->method('execute');
  296. $db->expects($this->exactly(2))
  297. ->method('prepare')
  298. ->will($this->returnValue($statement));
  299. $this->assertTrue($fixture->create($db));
  300. }
  301. /**
  302. * test create method, trigger error
  303. *
  304. * @expectedException \PHPUnit_Framework_Error
  305. * @return void
  306. */
  307. public function testCreateError()
  308. {
  309. $fixture = new ArticlesFixture();
  310. $db = $this->getMock('Cake\Database\Connection', [], [], '', false);
  311. $table = $this->getMock('Cake\Database\Schema\Table', [], ['articles']);
  312. $table->expects($this->once())
  313. ->method('createSql')
  314. ->with($db)
  315. ->will($this->throwException(new Exception('oh noes')));
  316. $fixture->schema($table);
  317. $fixture->create($db);
  318. }
  319. /**
  320. * test the insert method
  321. *
  322. * @return void
  323. */
  324. public function testInsert()
  325. {
  326. $fixture = new ArticlesFixture();
  327. $db = $this->getMock('Cake\Database\Connection', [], [], '', false);
  328. $query = $this->getMock('Cake\Database\Query', [], [$db]);
  329. $db->expects($this->once())
  330. ->method('newQuery')
  331. ->will($this->returnValue($query));
  332. $query->expects($this->once())
  333. ->method('insert')
  334. ->with(['name', 'created'], ['name' => 'string', 'created' => 'datetime'])
  335. ->will($this->returnSelf());
  336. $query->expects($this->once())
  337. ->method('into')
  338. ->with('articles')
  339. ->will($this->returnSelf());
  340. $expected = [
  341. ['name' => 'Gandalf', 'created' => '2009-04-28 19:20:00'],
  342. ['name' => 'Captain Picard', 'created' => '2009-04-28 19:20:00'],
  343. ['name' => 'Chewbacca', 'created' => '2009-04-28 19:20:00']
  344. ];
  345. $query->expects($this->at(2))
  346. ->method('values')
  347. ->with($expected[0])
  348. ->will($this->returnSelf());
  349. $query->expects($this->at(3))
  350. ->method('values')
  351. ->with($expected[1])
  352. ->will($this->returnSelf());
  353. $query->expects($this->at(4))
  354. ->method('values')
  355. ->with($expected[2])
  356. ->will($this->returnSelf());
  357. $statement = $this->getMock('\PDOStatement', ['closeCursor']);
  358. $statement->expects($this->once())->method('closeCursor');
  359. $query->expects($this->once())
  360. ->method('execute')
  361. ->will($this->returnValue($statement));
  362. $this->assertSame($statement, $fixture->insert($db));
  363. }
  364. /**
  365. * test the insert method
  366. *
  367. * @return void
  368. */
  369. public function testInsertImport()
  370. {
  371. $fixture = new ImportsFixture();
  372. $db = $this->getMock('Cake\Database\Connection', [], [], '', false);
  373. $query = $this->getMock('Cake\Database\Query', [], [$db]);
  374. $db->expects($this->once())
  375. ->method('newQuery')
  376. ->will($this->returnValue($query));
  377. $query->expects($this->once())
  378. ->method('insert')
  379. ->with(['title', 'body'], ['title' => 'string', 'body' => 'text'])
  380. ->will($this->returnSelf());
  381. $query->expects($this->once())
  382. ->method('into')
  383. ->with('posts')
  384. ->will($this->returnSelf());
  385. $expected = [
  386. ['title' => 'Hello!', 'body' => 'Hello world!'],
  387. ];
  388. $query->expects($this->at(2))
  389. ->method('values')
  390. ->with($expected[0])
  391. ->will($this->returnSelf());
  392. $statement = $this->getMock('\PDOStatement', ['closeCursor']);
  393. $statement->expects($this->once())->method('closeCursor');
  394. $query->expects($this->once())
  395. ->method('execute')
  396. ->will($this->returnValue($statement));
  397. $this->assertSame($statement, $fixture->insert($db));
  398. }
  399. /**
  400. * test the insert method
  401. *
  402. * @return void
  403. */
  404. public function testInsertStrings()
  405. {
  406. $fixture = new StringsTestsFixture();
  407. $db = $this->getMock('Cake\Database\Connection', [], [], '', false);
  408. $query = $this->getMock('Cake\Database\Query', [], [$db]);
  409. $db->expects($this->once())
  410. ->method('newQuery')
  411. ->will($this->returnValue($query));
  412. $query->expects($this->once())
  413. ->method('insert')
  414. ->with(['name', 'email', 'age'], ['name' => 'string', 'email' => 'string', 'age' => 'integer'])
  415. ->will($this->returnSelf());
  416. $query->expects($this->once())
  417. ->method('into')
  418. ->with('strings')
  419. ->will($this->returnSelf());
  420. $expected = [
  421. ['name' => 'Mark Doe', 'email' => 'mark.doe@email.com', 'age' => null],
  422. ['name' => 'John Doe', 'email' => 'john.doe@email.com', 'age' => 20],
  423. ['name' => 'Jane Doe', 'email' => 'jane.doe@email.com', 'age' => 30],
  424. ];
  425. $query->expects($this->at(2))
  426. ->method('values')
  427. ->with($expected[0])
  428. ->will($this->returnSelf());
  429. $query->expects($this->at(3))
  430. ->method('values')
  431. ->with($expected[1])
  432. ->will($this->returnSelf());
  433. $query->expects($this->at(4))
  434. ->method('values')
  435. ->with($expected[2])
  436. ->will($this->returnSelf());
  437. $statement = $this->getMock('\PDOStatement', ['closeCursor']);
  438. $statement->expects($this->once())->method('closeCursor');
  439. $query->expects($this->once())
  440. ->method('execute')
  441. ->will($this->returnValue($statement));
  442. $this->assertSame($statement, $fixture->insert($db));
  443. }
  444. /**
  445. * Test the drop method
  446. *
  447. * @return void
  448. */
  449. public function testDrop()
  450. {
  451. $fixture = new ArticlesFixture();
  452. $db = $this->getMock('Cake\Database\Connection', [], [], '', false);
  453. $statement = $this->getMock('\PDOStatement', ['closeCursor']);
  454. $statement->expects($this->once())->method('closeCursor');
  455. $db->expects($this->once())->method('execute')
  456. ->with('sql')
  457. ->will($this->returnValue($statement));
  458. $table = $this->getMock('Cake\Database\Schema\Table', [], ['articles']);
  459. $table->expects($this->once())
  460. ->method('dropSql')
  461. ->with($db)
  462. ->will($this->returnValue(['sql']));
  463. $fixture->schema($table);
  464. $this->assertTrue($fixture->drop($db));
  465. }
  466. /**
  467. * Test the truncate method.
  468. *
  469. * @return void
  470. */
  471. public function testTruncate()
  472. {
  473. $fixture = new ArticlesFixture();
  474. $db = $this->getMock('Cake\Database\Connection', [], [], '', false);
  475. $statement = $this->getMock('\PDOStatement', ['closeCursor']);
  476. $statement->expects($this->once())->method('closeCursor');
  477. $db->expects($this->once())->method('execute')
  478. ->with('sql')
  479. ->will($this->returnValue($statement));
  480. $table = $this->getMock('Cake\Database\Schema\Table', [], ['articles']);
  481. $table->expects($this->once())
  482. ->method('truncateSql')
  483. ->with($db)
  484. ->will($this->returnValue(['sql']));
  485. $fixture->schema($table);
  486. $this->assertTrue($fixture->truncate($db));
  487. }
  488. }