SqlserverSchemaTest.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783
  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 3.0.0
  13. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Test\TestCase\Database\Schema;
  16. use Cake\Core\Configure;
  17. use Cake\Database\Schema\Collection as SchemaCollection;
  18. use Cake\Database\Schema\SqlserverSchema;
  19. use Cake\Database\Schema\Table;
  20. use Cake\Datasource\ConnectionManager;
  21. use Cake\TestSuite\TestCase;
  22. /**
  23. * SQL Server schema test case.
  24. */
  25. class SqlserverSchemaTest extends TestCase
  26. {
  27. /**
  28. * Set up
  29. *
  30. * @return void
  31. */
  32. public function setUp()
  33. {
  34. parent::setUp();
  35. $this->skipUnless(defined('PDO::SQLSRV_ENCODING_UTF8'), 'SQL Server extension not present');
  36. }
  37. /**
  38. * Helper method for skipping tests that need a real connection.
  39. *
  40. * @return void
  41. */
  42. protected function _needsConnection()
  43. {
  44. $config = ConnectionManager::config('test');
  45. $this->skipIf(strpos($config['driver'], 'Sqlserver') === false, 'Not using Sqlserver for test config');
  46. }
  47. /**
  48. * Helper method for testing methods.
  49. *
  50. * @return void
  51. */
  52. protected function _createTables($connection)
  53. {
  54. $this->_needsConnection();
  55. $connection->execute("IF OBJECT_ID('schema_articles', 'U') IS NOT NULL DROP TABLE schema_articles");
  56. $connection->execute("IF OBJECT_ID('schema_authors', 'U') IS NOT NULL DROP TABLE schema_authors");
  57. $table = <<<SQL
  58. CREATE TABLE schema_authors (
  59. id int IDENTITY(1,1) PRIMARY KEY,
  60. name VARCHAR(50),
  61. bio DATE,
  62. created DATETIME
  63. )
  64. SQL;
  65. $connection->execute($table);
  66. $table = <<<SQL
  67. CREATE TABLE schema_articles (
  68. id BIGINT PRIMARY KEY,
  69. title VARCHAR(20),
  70. body VARCHAR(1000),
  71. author_id INTEGER NOT NULL,
  72. published BIT DEFAULT 0,
  73. views SMALLINT DEFAULT 0,
  74. created DATETIME,
  75. CONSTRAINT [content_idx] UNIQUE ([title], [body]),
  76. CONSTRAINT [author_idx] FOREIGN KEY ([author_id]) REFERENCES [schema_authors] ([id]) ON DELETE CASCADE ON UPDATE CASCADE
  77. )
  78. SQL;
  79. $connection->execute($table);
  80. $connection->execute('CREATE INDEX [author_idx] ON [schema_articles] ([author_id])');
  81. }
  82. /**
  83. * Data provider for convert column testing
  84. *
  85. * @return array
  86. */
  87. public static function convertColumnProvider()
  88. {
  89. return [
  90. [
  91. 'DATETIME',
  92. null,
  93. null,
  94. null,
  95. ['type' => 'timestamp', 'length' => null]
  96. ],
  97. [
  98. 'DATE',
  99. null,
  100. null,
  101. null,
  102. ['type' => 'date', 'length' => null]
  103. ],
  104. [
  105. 'TIME',
  106. null,
  107. null,
  108. null,
  109. ['type' => 'time', 'length' => null]
  110. ],
  111. [
  112. 'SMALLINT',
  113. null,
  114. 4,
  115. null,
  116. ['type' => 'integer', 'length' => 4]
  117. ],
  118. [
  119. 'INTEGER',
  120. null,
  121. null,
  122. null,
  123. ['type' => 'integer', 'length' => 10]
  124. ],
  125. [
  126. 'INTEGER',
  127. null,
  128. 8,
  129. null,
  130. ['type' => 'integer', 'length' => 8]
  131. ],
  132. [
  133. 'BIGINT',
  134. null,
  135. null,
  136. null,
  137. ['type' => 'biginteger', 'length' => 20]
  138. ],
  139. [
  140. 'NUMERIC',
  141. null,
  142. 15,
  143. 5,
  144. ['type' => 'decimal', 'length' => 15, 'precision' => 5]
  145. ],
  146. [
  147. 'DECIMAL',
  148. null,
  149. 11,
  150. 3,
  151. ['type' => 'decimal', 'length' => 11, 'precision' => 3]
  152. ],
  153. [
  154. 'MONEY',
  155. null,
  156. null,
  157. null,
  158. ['type' => 'decimal', 'length' => null, 'precision' => null]
  159. ],
  160. [
  161. 'VARCHAR',
  162. null,
  163. null,
  164. null,
  165. ['type' => 'string', 'length' => 255]
  166. ],
  167. [
  168. 'VARCHAR',
  169. 10,
  170. null,
  171. null,
  172. ['type' => 'string', 'length' => 10]
  173. ],
  174. [
  175. 'NVARCHAR',
  176. 50,
  177. null,
  178. null,
  179. ['type' => 'string', 'length' => 50]
  180. ],
  181. [
  182. 'CHAR',
  183. 10,
  184. null,
  185. null,
  186. ['type' => 'string', 'fixed' => true, 'length' => 10]
  187. ],
  188. [
  189. 'NCHAR',
  190. 10,
  191. null,
  192. null,
  193. ['type' => 'string', 'fixed' => true, 'length' => 10]
  194. ],
  195. [
  196. 'UNIQUEIDENTIFIER',
  197. null,
  198. null,
  199. null,
  200. ['type' => 'uuid']
  201. ],
  202. [
  203. 'TEXT',
  204. null,
  205. null,
  206. null,
  207. ['type' => 'text', 'length' => null]
  208. ],
  209. [
  210. 'REAL',
  211. null,
  212. null,
  213. null,
  214. ['type' => 'float', 'length' => null]
  215. ],
  216. [
  217. 'VARCHAR',
  218. -1,
  219. null,
  220. null,
  221. ['type' => 'text', 'length' => null]
  222. ],
  223. ];
  224. }
  225. /**
  226. * Test parsing Sqlserver column types from field description.
  227. *
  228. * @dataProvider convertColumnProvider
  229. * @return void
  230. */
  231. public function testConvertColumn($type, $length, $precision, $scale, $expected)
  232. {
  233. $field = [
  234. 'name' => 'field',
  235. 'type' => $type,
  236. 'null' => '1',
  237. 'default' => 'Default value',
  238. 'char_length' => $length,
  239. 'precision' => $precision,
  240. 'scale' => $scale
  241. ];
  242. $expected += [
  243. 'null' => true,
  244. 'default' => 'Default value',
  245. ];
  246. $driver = $this->getMock('Cake\Database\Driver\Sqlserver');
  247. $dialect = new SqlserverSchema($driver);
  248. $table = $this->getMock('Cake\Database\Schema\Table', [], ['table']);
  249. $table->expects($this->at(0))->method('addColumn')->with('field', $expected);
  250. $dialect->convertColumnDescription($table, $field);
  251. }
  252. /**
  253. * Test listing tables with Sqlserver
  254. *
  255. * @return void
  256. */
  257. public function testListTables()
  258. {
  259. $connection = ConnectionManager::get('test');
  260. $this->_createTables($connection);
  261. $schema = new SchemaCollection($connection);
  262. $result = $schema->listTables();
  263. $this->assertInternalType('array', $result);
  264. $this->assertContains('schema_articles', $result);
  265. $this->assertContains('schema_authors', $result);
  266. }
  267. /**
  268. * Test describing a table with Sqlserver
  269. *
  270. * @return void
  271. */
  272. public function testDescribeTable()
  273. {
  274. $connection = ConnectionManager::get('test');
  275. $this->_createTables($connection);
  276. $schema = new SchemaCollection($connection);
  277. $result = $schema->describe('schema_articles');
  278. $expected = [
  279. 'id' => [
  280. 'type' => 'biginteger',
  281. 'null' => false,
  282. 'default' => null,
  283. 'length' => 19,
  284. 'precision' => null,
  285. 'unsigned' => null,
  286. 'autoIncrement' => null,
  287. 'comment' => null,
  288. ],
  289. 'title' => [
  290. 'type' => 'string',
  291. 'null' => true,
  292. 'default' => null,
  293. 'length' => 20,
  294. 'precision' => null,
  295. 'comment' => null,
  296. 'fixed' => null,
  297. ],
  298. 'body' => [
  299. 'type' => 'string',
  300. 'null' => true,
  301. 'default' => null,
  302. 'length' => 1000,
  303. 'precision' => null,
  304. 'fixed' => null,
  305. 'comment' => null,
  306. ],
  307. 'author_id' => [
  308. 'type' => 'integer',
  309. 'null' => false,
  310. 'default' => null,
  311. 'length' => 10,
  312. 'precision' => null,
  313. 'unsigned' => null,
  314. 'autoIncrement' => null,
  315. 'comment' => null,
  316. ],
  317. 'published' => [
  318. 'type' => 'boolean',
  319. 'null' => true,
  320. 'default' => 0,
  321. 'length' => null,
  322. 'precision' => null,
  323. 'comment' => null,
  324. ],
  325. 'views' => [
  326. 'type' => 'integer',
  327. 'null' => true,
  328. 'default' => 0,
  329. 'length' => 5,
  330. 'precision' => null,
  331. 'unsigned' => null,
  332. 'autoIncrement' => null,
  333. 'comment' => null,
  334. ],
  335. 'created' => [
  336. 'type' => 'timestamp',
  337. 'null' => true,
  338. 'default' => null,
  339. 'length' => null,
  340. 'precision' => null,
  341. 'comment' => null,
  342. ],
  343. ];
  344. $this->assertEquals(['id'], $result->primaryKey());
  345. foreach ($expected as $field => $definition) {
  346. $this->assertEquals($definition, $result->column($field), 'Failed to match field ' . $field);
  347. }
  348. }
  349. /**
  350. * Test describing a table with postgres and composite keys
  351. *
  352. * @return void
  353. */
  354. public function testDescribeTableCompositeKey()
  355. {
  356. $this->_needsConnection();
  357. $connection = ConnectionManager::get('test');
  358. $sql = <<<SQL
  359. CREATE TABLE schema_composite (
  360. [id] INTEGER IDENTITY(1, 1),
  361. [site_id] INTEGER NOT NULL,
  362. [name] VARCHAR(255),
  363. PRIMARY KEY([id], [site_id])
  364. );
  365. SQL;
  366. $connection->execute($sql);
  367. $schema = new SchemaCollection($connection);
  368. $result = $schema->describe('schema_composite');
  369. $connection->execute('DROP TABLE schema_composite');
  370. $this->assertEquals(['id', 'site_id'], $result->primaryKey());
  371. $this->assertNull($result->column('site_id')['autoIncrement'], 'site_id should not be autoincrement');
  372. $this->assertTrue($result->column('id')['autoIncrement'], 'id should be autoincrement');
  373. }
  374. /**
  375. * Test that describe accepts tablenames containing `schema.table`.
  376. *
  377. * @return void
  378. */
  379. public function testDescribeWithSchemaName()
  380. {
  381. $connection = ConnectionManager::get('test');
  382. $this->_createTables($connection);
  383. $schema = new SchemaCollection($connection);
  384. $result = $schema->describe('dbo.schema_articles');
  385. $this->assertEquals(['id'], $result->primaryKey());
  386. $this->assertEquals('schema_articles', $result->name());
  387. }
  388. /**
  389. * Test describing a table with indexes
  390. *
  391. * @return void
  392. */
  393. public function testDescribeTableIndexes()
  394. {
  395. $connection = ConnectionManager::get('test');
  396. $this->_createTables($connection);
  397. $schema = new SchemaCollection($connection);
  398. $result = $schema->describe('schema_articles');
  399. $this->assertInstanceOf('Cake\Database\Schema\Table', $result);
  400. $this->assertCount(3, $result->constraints());
  401. $expected = [
  402. 'primary' => [
  403. 'type' => 'primary',
  404. 'columns' => ['id'],
  405. 'length' => []
  406. ],
  407. 'content_idx' => [
  408. 'type' => 'unique',
  409. 'columns' => ['title', 'body'],
  410. 'length' => []
  411. ],
  412. 'author_idx' => [
  413. 'type' => 'foreign',
  414. 'columns' => ['author_id'],
  415. 'references' => ['schema_authors', 'id'],
  416. 'length' => [],
  417. 'update' => 'cascade',
  418. 'delete' => 'cascade',
  419. ]
  420. ];
  421. $this->assertEquals($expected['primary'], $result->constraint('primary'));
  422. $this->assertEquals($expected['content_idx'], $result->constraint('content_idx'));
  423. $this->assertEquals($expected['author_idx'], $result->constraint('author_idx'));
  424. $this->assertCount(1, $result->indexes());
  425. $expected = [
  426. 'type' => 'index',
  427. 'columns' => ['author_id'],
  428. 'length' => []
  429. ];
  430. $this->assertEquals($expected, $result->index('author_idx'));
  431. }
  432. /**
  433. * Column provider for creating column sql
  434. *
  435. * @return array
  436. */
  437. public static function columnSqlProvider()
  438. {
  439. return [
  440. // strings
  441. [
  442. 'title',
  443. ['type' => 'string', 'length' => 25, 'null' => false],
  444. '[title] NVARCHAR(25) NOT NULL'
  445. ],
  446. [
  447. 'title',
  448. ['type' => 'string', 'length' => 25, 'null' => true, 'default' => 'ignored'],
  449. '[title] NVARCHAR(25) DEFAULT NULL'
  450. ],
  451. [
  452. 'id',
  453. ['type' => 'string', 'length' => 32, 'fixed' => true, 'null' => false],
  454. '[id] NCHAR(32) NOT NULL'
  455. ],
  456. [
  457. 'id',
  458. ['type' => 'uuid', 'null' => false],
  459. '[id] UNIQUEIDENTIFIER NOT NULL'
  460. ],
  461. [
  462. 'role',
  463. ['type' => 'string', 'length' => 10, 'null' => false, 'default' => 'admin'],
  464. "[role] NVARCHAR(10) NOT NULL DEFAULT [admin]"
  465. ],
  466. [
  467. 'title',
  468. ['type' => 'string'],
  469. '[title] NVARCHAR(255)'
  470. ],
  471. // Text
  472. [
  473. 'body',
  474. ['type' => 'text', 'null' => false],
  475. '[body] NVARCHAR(MAX) NOT NULL'
  476. ],
  477. // Integers
  478. [
  479. 'post_id',
  480. ['type' => 'integer', 'length' => 11],
  481. '[post_id] INTEGER'
  482. ],
  483. [
  484. 'post_id',
  485. ['type' => 'biginteger', 'length' => 20],
  486. '[post_id] BIGINT'
  487. ],
  488. // Decimal
  489. [
  490. 'value',
  491. ['type' => 'decimal'],
  492. '[value] DECIMAL'
  493. ],
  494. [
  495. 'value',
  496. ['type' => 'decimal', 'length' => 11],
  497. '[value] DECIMAL(11,0)'
  498. ],
  499. [
  500. 'value',
  501. ['type' => 'decimal', 'length' => 12, 'precision' => 5],
  502. '[value] DECIMAL(12,5)'
  503. ],
  504. // Float
  505. [
  506. 'value',
  507. ['type' => 'float'],
  508. '[value] FLOAT'
  509. ],
  510. [
  511. 'value',
  512. ['type' => 'float', 'length' => 11, 'precision' => 3],
  513. '[value] FLOAT(3)'
  514. ],
  515. // Binary
  516. [
  517. 'img',
  518. ['type' => 'binary'],
  519. '[img] VARBINARY(MAX)'
  520. ],
  521. // Boolean
  522. [
  523. 'checked',
  524. ['type' => 'boolean', 'default' => false],
  525. '[checked] BIT DEFAULT 0'
  526. ],
  527. [
  528. 'checked',
  529. ['type' => 'boolean', 'default' => true, 'null' => false],
  530. '[checked] BIT NOT NULL DEFAULT 1'
  531. ],
  532. // datetimes
  533. [
  534. 'created',
  535. ['type' => 'datetime'],
  536. '[created] DATETIME'
  537. ],
  538. // Date & Time
  539. [
  540. 'start_date',
  541. ['type' => 'date'],
  542. '[start_date] DATE'
  543. ],
  544. [
  545. 'start_time',
  546. ['type' => 'time'],
  547. '[start_time] TIME'
  548. ],
  549. // timestamps
  550. [
  551. 'created',
  552. ['type' => 'timestamp', 'null' => true],
  553. '[created] DATETIME DEFAULT NULL'
  554. ],
  555. ];
  556. }
  557. /**
  558. * Test generating column definitions
  559. *
  560. * @dataProvider columnSqlProvider
  561. * @return void
  562. */
  563. public function testColumnSql($name, $data, $expected)
  564. {
  565. $driver = $this->_getMockedDriver();
  566. $schema = new SqlserverSchema($driver);
  567. $table = (new Table('schema_articles'))->addColumn($name, $data);
  568. $this->assertEquals($expected, $schema->columnSql($table, $name));
  569. }
  570. /**
  571. * Provide data for testing constraintSql
  572. *
  573. * @return array
  574. */
  575. public static function constraintSqlProvider()
  576. {
  577. return [
  578. [
  579. 'primary',
  580. ['type' => 'primary', 'columns' => ['title']],
  581. 'PRIMARY KEY ([title])'
  582. ],
  583. [
  584. 'unique_idx',
  585. ['type' => 'unique', 'columns' => ['title', 'author_id']],
  586. 'CONSTRAINT [unique_idx] UNIQUE ([title], [author_id])'
  587. ],
  588. [
  589. 'author_id_idx',
  590. ['type' => 'foreign', 'columns' => ['author_id'], 'references' => ['authors', 'id']],
  591. 'CONSTRAINT [author_id_idx] FOREIGN KEY ([author_id]) ' .
  592. 'REFERENCES [authors] ([id]) ON UPDATE SET NULL ON DELETE SET NULL'
  593. ],
  594. [
  595. 'author_id_idx',
  596. ['type' => 'foreign', 'columns' => ['author_id'], 'references' => ['authors', 'id'], 'update' => 'cascade'],
  597. 'CONSTRAINT [author_id_idx] FOREIGN KEY ([author_id]) ' .
  598. 'REFERENCES [authors] ([id]) ON UPDATE CASCADE ON DELETE SET NULL'
  599. ],
  600. [
  601. 'author_id_idx',
  602. ['type' => 'foreign', 'columns' => ['author_id'], 'references' => ['authors', 'id'], 'update' => 'setDefault'],
  603. 'CONSTRAINT [author_id_idx] FOREIGN KEY ([author_id]) ' .
  604. 'REFERENCES [authors] ([id]) ON UPDATE SET DEFAULT ON DELETE SET NULL'
  605. ],
  606. [
  607. 'author_id_idx',
  608. ['type' => 'foreign', 'columns' => ['author_id'], 'references' => ['authors', 'id'], 'update' => 'setNull'],
  609. 'CONSTRAINT [author_id_idx] FOREIGN KEY ([author_id]) ' .
  610. 'REFERENCES [authors] ([id]) ON UPDATE SET NULL ON DELETE SET NULL'
  611. ],
  612. [
  613. 'author_id_idx',
  614. ['type' => 'foreign', 'columns' => ['author_id'], 'references' => ['authors', 'id'], 'update' => 'noAction'],
  615. 'CONSTRAINT [author_id_idx] FOREIGN KEY ([author_id]) ' .
  616. 'REFERENCES [authors] ([id]) ON UPDATE NO ACTION ON DELETE SET NULL'
  617. ],
  618. ];
  619. }
  620. /**
  621. * Test the constraintSql method.
  622. *
  623. * @dataProvider constraintSqlProvider
  624. */
  625. public function testConstraintSql($name, $data, $expected)
  626. {
  627. $driver = $this->_getMockedDriver();
  628. $schema = new SqlserverSchema($driver);
  629. $table = (new Table('schema_articles'))->addColumn('title', [
  630. 'type' => 'string',
  631. 'length' => 255
  632. ])->addColumn('author_id', [
  633. 'type' => 'integer',
  634. ])->addConstraint($name, $data);
  635. $this->assertEquals($expected, $schema->constraintSql($table, $name));
  636. }
  637. /**
  638. * Integration test for converting a Schema\Table into MySQL table creates.
  639. *
  640. * @return void
  641. */
  642. public function testCreateSql()
  643. {
  644. $driver = $this->_getMockedDriver();
  645. $connection = $this->getMock('Cake\Database\Connection', [], [], '', false);
  646. $connection->expects($this->any())->method('driver')
  647. ->will($this->returnValue($driver));
  648. $table = (new Table('schema_articles'))->addColumn('id', [
  649. 'type' => 'integer',
  650. 'null' => false
  651. ])
  652. ->addColumn('title', [
  653. 'type' => 'string',
  654. 'null' => false,
  655. ])
  656. ->addColumn('body', ['type' => 'text'])
  657. ->addColumn('created', 'datetime')
  658. ->addConstraint('primary', [
  659. 'type' => 'primary',
  660. 'columns' => ['id'],
  661. ])
  662. ->addIndex('title_idx', [
  663. 'type' => 'index',
  664. 'columns' => ['title'],
  665. ]);
  666. $expected = <<<SQL
  667. CREATE TABLE [schema_articles] (
  668. [id] INTEGER IDENTITY(1, 1),
  669. [title] NVARCHAR(255) NOT NULL,
  670. [body] NVARCHAR(MAX),
  671. [created] DATETIME,
  672. PRIMARY KEY ([id])
  673. )
  674. SQL;
  675. $result = $table->createSql($connection);
  676. $this->assertCount(2, $result);
  677. $this->assertEquals(str_replace("\r\n", "\n", $expected), str_replace("\r\n", "\n", $result[0]));
  678. $this->assertEquals(
  679. 'CREATE INDEX [title_idx] ON [schema_articles] ([title])',
  680. $result[1]
  681. );
  682. }
  683. /**
  684. * test dropSql
  685. *
  686. * @return void
  687. */
  688. public function testDropSql()
  689. {
  690. $driver = $this->_getMockedDriver();
  691. $connection = $this->getMock('Cake\Database\Connection', [], [], '', false);
  692. $connection->expects($this->any())->method('driver')
  693. ->will($this->returnValue($driver));
  694. $table = new Table('schema_articles');
  695. $result = $table->dropSql($connection);
  696. $this->assertCount(1, $result);
  697. $this->assertEquals('DROP TABLE [schema_articles]', $result[0]);
  698. }
  699. /**
  700. * Test truncateSql()
  701. *
  702. * @return void
  703. */
  704. public function testTruncateSql()
  705. {
  706. $driver = $this->_getMockedDriver();
  707. $connection = $this->getMock('Cake\Database\Connection', [], [], '', false);
  708. $connection->expects($this->any())->method('driver')
  709. ->will($this->returnValue($driver));
  710. $table = new Table('schema_articles');
  711. $table->addColumn('id', 'integer')
  712. ->addConstraint('primary', [
  713. 'type' => 'primary',
  714. 'columns' => ['id']
  715. ]);
  716. $result = $table->truncateSql($connection);
  717. $this->assertCount(2, $result);
  718. $this->assertEquals('DELETE FROM [schema_articles]', $result[0]);
  719. $this->assertEquals('DBCC CHECKIDENT([schema_articles], RESEED, 0)', $result[1]);
  720. }
  721. /**
  722. * Get a schema instance with a mocked driver/pdo instances
  723. *
  724. * @return Driver
  725. */
  726. protected function _getMockedDriver()
  727. {
  728. $driver = new \Cake\Database\Driver\Sqlserver();
  729. $mock = $this->getMock('FakePdo', ['quote', 'quoteIdentifier']);
  730. $mock->expects($this->any())
  731. ->method('quote')
  732. ->will($this->returnCallback(function ($value) {
  733. return '[' . $value . ']';
  734. }));
  735. $mock->expects($this->any())
  736. ->method('quoteIdentifier')
  737. ->will($this->returnCallback(function ($value) {
  738. return '[' . $value . ']';
  739. }));
  740. $driver->connection($mock);
  741. return $driver;
  742. }
  743. }