TestFixtureTest.php 11 KB

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