TestFixtureTest.php 13 KB

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