TestFixtureTest.php 10 KB

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