PageRenderTime 25ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/test/classes/plugin/export/ExportYamlTest.php

https://gitlab.com/trungthao379/phpmyadmin
PHP | 268 lines | 177 code | 34 blank | 57 comment | 0 complexity | 6fd8095a5569219689f0ee26dd3ee1c6 MD5 | raw file
  1. <?php
  2. /* vim: set expandtab sw=4 ts=4 sts=4: */
  3. /**
  4. * tests for PMA\libraries\plugins\export\ExportYaml class
  5. *
  6. * @package PhpMyAdmin-test
  7. */
  8. use PMA\libraries\plugins\export\ExportYaml;
  9. require_once 'libraries/export.lib.php';
  10. require_once 'libraries/config.default.php';
  11. require_once 'libraries/database_interface.inc.php';
  12. require_once 'export.php';
  13. require_once 'test/PMATestCase.php';
  14. /**
  15. * tests for PMA\libraries\plugins\export\ExportYaml class
  16. *
  17. * @package PhpMyAdmin-test
  18. * @group medium
  19. */
  20. class ExportYamlTest extends PMATestCase
  21. {
  22. protected $object;
  23. /**
  24. * Configures global environment.
  25. *
  26. * @return void
  27. */
  28. function setup()
  29. {
  30. $GLOBALS['server'] = 0;
  31. $GLOBALS['output_kanji_conversion'] = false;
  32. $GLOBALS['buffer_needed'] = false;
  33. $GLOBALS['asfile'] = false;
  34. $GLOBALS['save_on_server'] = false;
  35. $GLOBALS['crlf'] = "\n";
  36. $GLOBALS['cfgRelation']['relation'] = true;
  37. $this->object = new ExportYaml();
  38. }
  39. /**
  40. * tearDown for test cases
  41. *
  42. * @return void
  43. */
  44. public function tearDown()
  45. {
  46. unset($this->object);
  47. }
  48. /**
  49. * Test for PMA\libraries\plugins\export\ExportYaml::setProperties
  50. *
  51. * @return void
  52. */
  53. public function testSetProperties()
  54. {
  55. $method = new ReflectionMethod('PMA\libraries\plugins\export\ExportYaml', 'setProperties');
  56. $method->setAccessible(true);
  57. $method->invoke($this->object, null);
  58. $attrProperties = new ReflectionProperty('PMA\libraries\plugins\export\ExportYaml', 'properties');
  59. $attrProperties->setAccessible(true);
  60. $properties = $attrProperties->getValue($this->object);
  61. $this->assertInstanceOf(
  62. 'PMA\libraries\properties\plugins\ExportPluginProperties',
  63. $properties
  64. );
  65. $this->assertEquals(
  66. 'YAML',
  67. $properties->getText()
  68. );
  69. $this->assertEquals(
  70. 'yml',
  71. $properties->getExtension()
  72. );
  73. $this->assertEquals(
  74. 'text/yaml',
  75. $properties->getMimeType()
  76. );
  77. $options = $properties->getOptions();
  78. $this->assertInstanceOf(
  79. 'PMA\libraries\properties\options\groups\OptionsPropertyRootGroup',
  80. $options
  81. );
  82. $this->assertEquals(
  83. 'Format Specific Options',
  84. $options->getName()
  85. );
  86. $generalOptionsArray = $options->getProperties();
  87. $generalOptions = array_shift($generalOptionsArray);
  88. $this->assertInstanceOf(
  89. 'PMA\libraries\properties\options\groups\OptionsPropertyMainGroup',
  90. $generalOptions
  91. );
  92. $this->assertEquals(
  93. 'general_opts',
  94. $generalOptions->getName()
  95. );
  96. $generalProperties = $generalOptions->getProperties();
  97. $property = array_shift($generalProperties);
  98. $this->assertInstanceOf(
  99. 'PMA\libraries\properties\options\items\HiddenPropertyItem',
  100. $property
  101. );
  102. }
  103. /**
  104. * Test for PMA\libraries\plugins\export\ExportYaml::exportHeader
  105. *
  106. * @return void
  107. */
  108. public function testExportHeader()
  109. {
  110. ob_start();
  111. $this->assertTrue(
  112. $this->object->exportHeader()
  113. );
  114. $result = ob_get_clean();
  115. $this->assertContains(
  116. "%YAML 1.1\n---\n",
  117. $result
  118. );
  119. }
  120. /**
  121. * Test for PMA\libraries\plugins\export\ExportYaml::exportFooter
  122. *
  123. * @return void
  124. */
  125. public function testExportFooter()
  126. {
  127. $this->expectOutputString(
  128. "...\n"
  129. );
  130. $this->assertTrue(
  131. $this->object->exportFooter()
  132. );
  133. }
  134. /**
  135. * Test for PMA\libraries\plugins\export\ExportYaml::exportDBHeader
  136. *
  137. * @return void
  138. */
  139. public function testExportDBHeader()
  140. {
  141. $this->assertTrue(
  142. $this->object->exportDBHeader('&db')
  143. );
  144. }
  145. /**
  146. * Test for PMA\libraries\plugins\export\ExportYaml::exportDBFooter
  147. *
  148. * @return void
  149. */
  150. public function testExportDBFooter()
  151. {
  152. $this->assertTrue(
  153. $this->object->exportDBFooter('&db')
  154. );
  155. }
  156. /**
  157. * Test for PMA\libraries\plugins\export\ExportYaml::exportDBCreate
  158. *
  159. * @return void
  160. */
  161. public function testExportDBCreate()
  162. {
  163. $this->assertTrue(
  164. $this->object->exportDBCreate('testDB', 'database')
  165. );
  166. }
  167. /**
  168. * Test for PMA\libraries\plugins\export\ExportYaml::exportData
  169. *
  170. * @return void
  171. */
  172. public function testExportData()
  173. {
  174. $dbi = $this->getMockBuilder('PMA\libraries\DatabaseInterface')
  175. ->disableOriginalConstructor()
  176. ->getMock();
  177. $dbi->expects($this->once())
  178. ->method('query')
  179. ->with('SELECT', null, PMA\libraries\DatabaseInterface::QUERY_UNBUFFERED)
  180. ->will($this->returnValue(true));
  181. $dbi->expects($this->once())
  182. ->method('numFields')
  183. ->with(true)
  184. ->will($this->returnValue(4));
  185. $dbi->expects($this->at(2))
  186. ->method('fieldName')
  187. ->will($this->returnValue('fName1'));
  188. $dbi->expects($this->at(3))
  189. ->method('fieldName')
  190. ->will($this->returnValue('fNa"me2'));
  191. $dbi->expects($this->at(4))
  192. ->method('fieldName')
  193. ->will($this->returnValue('fNa\\me3'));
  194. $dbi->expects($this->at(5))
  195. ->method('fieldName')
  196. ->will($this->returnValue('fName4'));
  197. $dbi->expects($this->at(6))
  198. ->method('fetchRow')
  199. ->with(true)
  200. ->will(
  201. $this->returnValue(
  202. array(null, '123', "\"c\\a\nb\r")
  203. )
  204. );
  205. $dbi->expects($this->at(7))
  206. ->method('fetchRow')
  207. ->with(true)
  208. ->will(
  209. $this->returnValue(
  210. array(null)
  211. )
  212. );
  213. $GLOBALS['dbi'] = $dbi;
  214. ob_start();
  215. $this->assertTrue(
  216. $this->object->exportData(
  217. 'db', 'ta<ble', "\n", "example.com", "SELECT"
  218. )
  219. );
  220. $result = ob_get_clean();
  221. $this->assertEquals(
  222. '# db.ta&lt;ble' . "\n" .
  223. '-' . "\n" .
  224. ' fNa&quot;me2: 123' . "\n" .
  225. ' fName3: &quot;\&quot;c\\\\a\nb\r&quot;' . "\n" .
  226. '-' . "\n",
  227. $result
  228. );
  229. }
  230. }