DbConfigTaskTest.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <?php
  2. /**
  3. * DBConfigTask Test Case
  4. *
  5. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  6. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  7. *
  8. * Licensed under The MIT License
  9. * For full copyright and license information, please see the LICENSE.txt
  10. * Redistributions of files must retain the above copyright notice.
  11. *
  12. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  13. * @link http://cakephp.org CakePHP(tm) Project
  14. * @since 1.3.0
  15. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  16. */
  17. namespace Cake\Test\TestCase\Console\Command\Task;
  18. use Cake\Console\Command\Task\DbConfigTask;
  19. use Cake\Core\Configure;
  20. use Cake\TestSuite\TestCase;
  21. /**
  22. * DbConfigTest class
  23. *
  24. */
  25. class DbConfigTaskTest extends TestCase {
  26. /**
  27. * setUp method
  28. *
  29. * @return void
  30. */
  31. public function setUp() {
  32. parent::setUp();
  33. $out = $this->getMock('Cake\Console\ConsoleOutput', array(), array(), '', false);
  34. $in = $this->getMock('Cake\Console\ConsoleInput', array(), array(), '', false);
  35. $this->Task = $this->getMock('Cake\Console\Command\Task\DbConfigTask',
  36. array('in', 'out', 'err', 'hr', 'createFile', '_stop', '_checkUnitTest', '_verify'),
  37. array($out, $out, $in)
  38. );
  39. $this->Task->path = APP . 'Config/';
  40. }
  41. /**
  42. * tearDown method
  43. *
  44. * @return void
  45. */
  46. public function tearDown() {
  47. parent::tearDown();
  48. unset($this->Task);
  49. }
  50. /**
  51. * Test the getConfig method.
  52. *
  53. * @return void
  54. */
  55. public function testGetConfig() {
  56. $this->Task->expects($this->any())
  57. ->method('in')
  58. ->will($this->returnValue('test'));
  59. $result = $this->Task->getConfig();
  60. $this->assertEquals('test', $result);
  61. }
  62. /**
  63. * test that initialize sets the path up.
  64. *
  65. * @return void
  66. */
  67. public function testInitialize() {
  68. $this->Task->initialize();
  69. $this->assertFalse(empty($this->Task->path));
  70. $this->assertEquals(APP . 'Config/', $this->Task->path);
  71. }
  72. /**
  73. * test execute and by extension _interactive
  74. *
  75. * @return void
  76. */
  77. public function testExecuteIntoInteractive() {
  78. $this->Task->initialize();
  79. $out = $this->getMock('Cake\Console\ConsoleOutput', array(), array(), '', false);
  80. $in = $this->getMock('Cake\Console\ConsoleInput', array(), array(), '', false);
  81. $this->Task = $this->getMock(
  82. 'Cake\Console\Command\Task\DbConfigTask',
  83. array('in', '_stop', 'createFile'), array($out, $out, $in)
  84. );
  85. $this->Task->path = APP . 'Config' . DS;
  86. $expected = "<?php\n";
  87. $expected .= "namespace App\Config;\n";
  88. $expected .= "use Cake\Core\Configure;\n\n";
  89. $expected .= "Configure::write('Datasource.default', [\n";
  90. $expected .= "\t'datasource' => 'Database/mysql',\n";
  91. $expected .= "\t'persistent' => false,\n";
  92. $expected .= "\t'host' => 'localhost',\n";
  93. $expected .= "\t'login' => 'root',\n";
  94. $expected .= "\t'password' => 'password',\n";
  95. $expected .= "\t'database' => 'cake_test',\n";
  96. $expected .= "]);\n";
  97. $this->Task->expects($this->once())->method('_stop');
  98. $this->Task->expects($this->at(0))->method('in')->will($this->returnValue('default')); //name
  99. $this->Task->expects($this->at(1))->method('in')->will($this->returnValue('mysql')); //db type
  100. $this->Task->expects($this->at(2))->method('in')->will($this->returnValue('n')); //persistent
  101. $this->Task->expects($this->at(3))->method('in')->will($this->returnValue('localhost')); //server
  102. $this->Task->expects($this->at(4))->method('in')->will($this->returnValue('n')); //port
  103. $this->Task->expects($this->at(5))->method('in')->will($this->returnValue('root')); //user
  104. $this->Task->expects($this->at(6))->method('in')->will($this->returnValue('password')); //password
  105. $this->Task->expects($this->at(10))->method('in')->will($this->returnValue('cake_test')); //db
  106. $this->Task->expects($this->at(11))->method('in')->will($this->returnValue('n')); //prefix
  107. $this->Task->expects($this->at(12))->method('in')->will($this->returnValue('n')); //encoding
  108. $this->Task->expects($this->at(13))->method('in')->will($this->returnValue('y')); //looks good
  109. $this->Task->expects($this->at(14))->method('in')->will($this->returnValue('n')); //another
  110. $this->Task->expects($this->at(15))->method('createFile')
  111. ->with(
  112. $this->equalTo($this->Task->path . 'datasources.php'),
  113. $this->equalTo($expected));
  114. Configure::write('Datasource', array());
  115. $this->Task->execute();
  116. }
  117. }