TestFixtureTest.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  5. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  6. *
  7. * Licensed under The MIT License
  8. * For full copyright and license information, please see the LICENSE.txt
  9. * Redistributions of files must retain the above copyright notice
  10. *
  11. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  12. * @link https://cakephp.org CakePHP(tm) Project
  13. * @since 1.2.0
  14. * @license https://opensource.org/licenses/mit-license.php MIT License
  15. */
  16. namespace Cake\Test\TestCase\TestSuite;
  17. use Cake\Database\Schema\TableSchema;
  18. use Cake\Database\StatementInterface;
  19. use Cake\Datasource\ConnectionManager;
  20. use Cake\Log\Log;
  21. use Cake\TestSuite\TestCase;
  22. use Exception;
  23. use TestApp\Fixture\ArticlesFixture;
  24. use TestApp\Fixture\ImportsFixture;
  25. use TestApp\Fixture\LettersFixture;
  26. use TestApp\Fixture\StringsTestsFixture;
  27. /**
  28. * Test case for TestFixture
  29. */
  30. class TestFixtureTest extends TestCase
  31. {
  32. /**
  33. * Fixtures for this test.
  34. *
  35. * @var array
  36. */
  37. protected $fixtures = ['core.Posts'];
  38. /**
  39. * Set up
  40. *
  41. * @return void
  42. */
  43. public function setUp(): void
  44. {
  45. parent::setUp();
  46. Log::reset();
  47. }
  48. /**
  49. * Tear down
  50. *
  51. * @return void
  52. */
  53. public function tearDown(): void
  54. {
  55. parent::tearDown();
  56. Log::reset();
  57. }
  58. /**
  59. * test initializing a static fixture
  60. *
  61. * @return void
  62. */
  63. public function testInitStaticFixture()
  64. {
  65. $Fixture = new ArticlesFixture();
  66. $this->assertSame('articles', $Fixture->table);
  67. $Fixture = new ArticlesFixture();
  68. $Fixture->table = null;
  69. $Fixture->init();
  70. $this->assertSame('articles', $Fixture->table);
  71. $schema = $Fixture->getTableSchema();
  72. $this->assertInstanceOf('Cake\Database\Schema\TableSchema', $schema);
  73. $fields = $Fixture->fields;
  74. unset($fields['_constraints'], $fields['_indexes']);
  75. $this->assertSame(
  76. array_keys($fields),
  77. $schema->columns(),
  78. 'Fields do not match'
  79. );
  80. $this->assertSame(array_keys($Fixture->fields['_constraints']), $schema->constraints());
  81. $this->assertEmpty($schema->indexes());
  82. }
  83. /**
  84. * test import fixture initialization
  85. *
  86. * @return void
  87. */
  88. public function testInitImport()
  89. {
  90. $fixture = new ImportsFixture();
  91. $fixture->fields = $fixture->records = null;
  92. $fixture->import = [
  93. 'table' => 'posts',
  94. 'connection' => 'test',
  95. ];
  96. $fixture->init();
  97. $expected = [
  98. 'id',
  99. 'author_id',
  100. 'title',
  101. 'body',
  102. 'published',
  103. ];
  104. $this->assertSame($expected, $fixture->getTableSchema()->columns());
  105. }
  106. /**
  107. * test import fixture initialization
  108. *
  109. * @return void
  110. */
  111. public function testInitImportModel()
  112. {
  113. $fixture = new ImportsFixture();
  114. $fixture->fields = $fixture->records = null;
  115. $fixture->import = [
  116. 'model' => 'Posts',
  117. 'connection' => 'test',
  118. ];
  119. $fixture->init();
  120. $expected = [
  121. 'id',
  122. 'author_id',
  123. 'title',
  124. 'body',
  125. 'published',
  126. ];
  127. $this->assertSame($expected, $fixture->getTableSchema()->columns());
  128. }
  129. /**
  130. * test schema reflection without $import or $fields and without the table existing
  131. * it will throw an exception
  132. *
  133. * @return void
  134. */
  135. public function testInitNoImportNoFieldsException()
  136. {
  137. $this->expectException(\Cake\Core\Exception\Exception::class);
  138. $this->expectExceptionMessage('Cannot describe schema for table `letters` for fixture `' . LettersFixture::class . '`: the table does not exist.');
  139. $fixture = new LettersFixture();
  140. $fixture->init();
  141. }
  142. /**
  143. * test schema reflection without $import or $fields will reflect the schema
  144. *
  145. * @return void
  146. */
  147. public function testInitNoImportNoFields()
  148. {
  149. $db = ConnectionManager::get('test');
  150. $table = new TableSchema('letters', [
  151. 'id' => ['type' => 'integer'],
  152. 'letter' => ['type' => 'string', 'length' => 1],
  153. ]);
  154. $table->addConstraint('primary', ['type' => 'primary', 'columns' => ['id']]);
  155. $sql = $table->createSql($db);
  156. foreach ($sql as $stmt) {
  157. $db->execute($stmt);
  158. }
  159. $fixture = new LettersFixture();
  160. $fixture->init();
  161. $this->assertSame(['id', 'letter'], $fixture->getTableSchema()->columns());
  162. $db = $this->getMockBuilder('Cake\Database\Connection')
  163. ->onlyMethods(['prepare', 'execute'])
  164. ->disableOriginalConstructor()
  165. ->getMock();
  166. $db->expects($this->never())
  167. ->method('prepare');
  168. $db->expects($this->never())
  169. ->method('execute');
  170. $this->assertTrue($fixture->create($db));
  171. $this->assertTrue($fixture->drop($db));
  172. // Cleanup.
  173. $db = ConnectionManager::get('test');
  174. $db->execute('DROP TABLE letters');
  175. }
  176. /**
  177. * test create method
  178. *
  179. * @return void
  180. */
  181. public function testCreate()
  182. {
  183. $fixture = new ArticlesFixture();
  184. $db = $this->getMockBuilder('Cake\Database\Connection')
  185. ->disableOriginalConstructor()
  186. ->getMock();
  187. $table = $this->getMockBuilder('Cake\Database\Schema\TableSchema')
  188. ->setConstructorArgs(['articles'])
  189. ->getMock();
  190. $table->expects($this->once())
  191. ->method('createSql')
  192. ->with($db)
  193. ->will($this->returnValue(['sql', 'sql']));
  194. $fixture->setTableSchema($table);
  195. $statement = $this->createMock(StatementInterface::class);
  196. $statement->expects($this->atLeastOnce())->method('closeCursor');
  197. $statement->expects($this->atLeastOnce())->method('execute');
  198. $db->expects($this->exactly(2))
  199. ->method('prepare')
  200. ->will($this->returnValue($statement));
  201. $this->assertTrue($fixture->create($db));
  202. }
  203. /**
  204. * test create method, trigger error
  205. *
  206. * @return void
  207. */
  208. public function testCreateError()
  209. {
  210. $this->expectError();
  211. $fixture = new ArticlesFixture();
  212. $db = $this->getMockBuilder('Cake\Database\Connection')
  213. ->disableOriginalConstructor()
  214. ->getMock();
  215. $table = $this->getMockBuilder('Cake\Database\Schema\TableSchema')
  216. ->setConstructorArgs(['articles'])
  217. ->getMock();
  218. $table->expects($this->once())
  219. ->method('createSql')
  220. ->with($db)
  221. ->will($this->throwException(new Exception('oh noes')));
  222. $fixture->setTableSchema($table);
  223. $fixture->create($db);
  224. }
  225. /**
  226. * test the insert method
  227. *
  228. * @return void
  229. */
  230. public function testInsert()
  231. {
  232. $fixture = new ArticlesFixture();
  233. $db = $this->getMockBuilder('Cake\Database\Connection')
  234. ->disableOriginalConstructor()
  235. ->getMock();
  236. $query = $this->getMockBuilder('Cake\Database\Query')
  237. ->setConstructorArgs([$db])
  238. ->getMock();
  239. $db->expects($this->once())
  240. ->method('newQuery')
  241. ->will($this->returnValue($query));
  242. $query->expects($this->once())
  243. ->method('insert')
  244. ->with(['name', 'created'], ['name' => 'string', 'created' => 'datetime'])
  245. ->will($this->returnSelf());
  246. $query->expects($this->once())
  247. ->method('into')
  248. ->with('articles')
  249. ->will($this->returnSelf());
  250. $expected = [
  251. ['name' => 'Gandalf', 'created' => '2009-04-28 19:20:00'],
  252. ['name' => 'Captain Picard', 'created' => '2009-04-28 19:20:00'],
  253. ['name' => 'Chewbacca', 'created' => '2009-04-28 19:20:00'],
  254. ];
  255. $query->expects($this->at(2))
  256. ->method('values')
  257. ->with($expected[0])
  258. ->will($this->returnSelf());
  259. $query->expects($this->at(3))
  260. ->method('values')
  261. ->with($expected[1])
  262. ->will($this->returnSelf());
  263. $query->expects($this->at(4))
  264. ->method('values')
  265. ->with($expected[2])
  266. ->will($this->returnSelf());
  267. $statement = $this->createMock(StatementInterface::class);
  268. $statement->expects($this->once())->method('closeCursor');
  269. $query->expects($this->once())
  270. ->method('execute')
  271. ->will($this->returnValue($statement));
  272. $this->assertSame($statement, $fixture->insert($db));
  273. }
  274. /**
  275. * test the insert method
  276. *
  277. * @return void
  278. */
  279. public function testInsertImport()
  280. {
  281. $fixture = new ImportsFixture();
  282. $db = $this->getMockBuilder('Cake\Database\Connection')
  283. ->disableOriginalConstructor()
  284. ->getMock();
  285. $query = $this->getMockBuilder('Cake\Database\Query')
  286. ->setConstructorArgs([$db])
  287. ->getMock();
  288. $db->expects($this->once())
  289. ->method('newQuery')
  290. ->will($this->returnValue($query));
  291. $query->expects($this->once())
  292. ->method('insert')
  293. ->with(['title', 'body'], ['title' => 'string', 'body' => 'text'])
  294. ->will($this->returnSelf());
  295. $query->expects($this->once())
  296. ->method('into')
  297. ->with('posts')
  298. ->will($this->returnSelf());
  299. $expected = [
  300. ['title' => 'Hello!', 'body' => 'Hello world!'],
  301. ];
  302. $query->expects($this->at(2))
  303. ->method('values')
  304. ->with($expected[0])
  305. ->will($this->returnSelf());
  306. $statement = $this->createMock(StatementInterface::class);
  307. $statement->expects($this->once())->method('closeCursor');
  308. $query->expects($this->once())
  309. ->method('execute')
  310. ->will($this->returnValue($statement));
  311. $this->assertSame($statement, $fixture->insert($db));
  312. }
  313. /**
  314. * test the insert method
  315. *
  316. * @return void
  317. */
  318. public function testInsertStrings()
  319. {
  320. $fixture = new StringsTestsFixture();
  321. $db = $this->getMockBuilder('Cake\Database\Connection')
  322. ->disableOriginalConstructor()
  323. ->getMock();
  324. $query = $this->getMockBuilder('Cake\Database\Query')
  325. ->setConstructorArgs([$db])
  326. ->getMock();
  327. $db->expects($this->once())
  328. ->method('newQuery')
  329. ->will($this->returnValue($query));
  330. $query->expects($this->once())
  331. ->method('insert')
  332. ->with(['name', 'email', 'age'], ['name' => 'string', 'email' => 'string', 'age' => 'integer'])
  333. ->will($this->returnSelf());
  334. $query->expects($this->once())
  335. ->method('into')
  336. ->with('strings')
  337. ->will($this->returnSelf());
  338. $expected = [
  339. ['name' => 'Mark Doe', 'email' => 'mark.doe@email.com', 'age' => null],
  340. ['name' => 'John Doe', 'email' => 'john.doe@email.com', 'age' => 20],
  341. ['name' => 'Jane Doe', 'email' => 'jane.doe@email.com', 'age' => 30],
  342. ];
  343. $query->expects($this->at(2))
  344. ->method('values')
  345. ->with($expected[0])
  346. ->will($this->returnSelf());
  347. $query->expects($this->at(3))
  348. ->method('values')
  349. ->with($expected[1])
  350. ->will($this->returnSelf());
  351. $query->expects($this->at(4))
  352. ->method('values')
  353. ->with($expected[2])
  354. ->will($this->returnSelf());
  355. $statement = $this->createMock(StatementInterface::class);
  356. $statement->expects($this->once())->method('closeCursor');
  357. $query->expects($this->once())
  358. ->method('execute')
  359. ->will($this->returnValue($statement));
  360. $this->assertSame($statement, $fixture->insert($db));
  361. }
  362. /**
  363. * Test the drop method
  364. *
  365. * @return void
  366. */
  367. public function testDrop()
  368. {
  369. $fixture = new ArticlesFixture();
  370. $db = $this->getMockBuilder('Cake\Database\Connection')
  371. ->disableOriginalConstructor()
  372. ->getMock();
  373. $statement = $this->createMock(StatementInterface::class);
  374. $statement->expects($this->once())->method('closeCursor');
  375. $db->expects($this->once())->method('execute')
  376. ->with('sql')
  377. ->will($this->returnValue($statement));
  378. $table = $this->getMockBuilder('Cake\Database\Schema\TableSchema')
  379. ->setConstructorArgs(['articles'])
  380. ->getMock();
  381. $table->expects($this->once())
  382. ->method('dropSql')
  383. ->with($db)
  384. ->will($this->returnValue(['sql']));
  385. $fixture->setTableSchema($table);
  386. $this->assertTrue($fixture->drop($db));
  387. }
  388. /**
  389. * Test the truncate method.
  390. *
  391. * @return void
  392. */
  393. public function testTruncate()
  394. {
  395. $fixture = new ArticlesFixture();
  396. $db = $this->getMockBuilder('Cake\Database\Connection')
  397. ->disableOriginalConstructor()
  398. ->getMock();
  399. $statement = $this->createMock(StatementInterface::class);
  400. $statement->expects($this->once())->method('closeCursor');
  401. $db->expects($this->once())->method('execute')
  402. ->with('sql')
  403. ->will($this->returnValue($statement));
  404. $table = $this->getMockBuilder('Cake\Database\Schema\TableSchema')
  405. ->setConstructorArgs(['articles'])
  406. ->getMock();
  407. $table->expects($this->once())
  408. ->method('truncateSql')
  409. ->with($db)
  410. ->will($this->returnValue(['sql']));
  411. $fixture->setTableSchema($table);
  412. $this->assertTrue($fixture->truncate($db));
  413. }
  414. }