ExtractTest.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <?php
  2. /**
  3. * Lithium: the most rad php framework
  4. *
  5. * @copyright Copyright 2013, Union of RAD (http://union-of-rad.org)
  6. * @license http://opensource.org/licenses/bsd-license.php The BSD License
  7. */
  8. namespace lithium\tests\cases\console\command\g11n;
  9. use lithium\core\Libraries;
  10. use lithium\console\Request;
  11. use lithium\console\command\g11n\Extract;
  12. use lithium\g11n\Catalog;
  13. class ExtractTest extends \lithium\test\Unit {
  14. protected $_backup = array();
  15. protected $_path;
  16. public $command;
  17. public function skip() {
  18. $this->_path = Libraries::get(true, 'resources') . '/tmp/tests';
  19. $this->skipIf(!is_writable($this->_path), "Path `{$this->_path}` is not writable.");
  20. }
  21. public function setUp() {
  22. $this->_backup['catalogConfig'] = Catalog::config();
  23. Catalog::reset();
  24. $this->command = new Extract(array(
  25. 'request' => new Request(array('input' => fopen('php://temp', 'w+'))),
  26. 'classes' => array('response' => 'lithium\tests\mocks\console\MockResponse')
  27. ));
  28. mkdir($this->command->source = "{$this->_path}/source");
  29. mkdir($this->command->destination = "{$this->_path}/destination");
  30. }
  31. public function tearDown() {
  32. Catalog::config($this->_backup['catalogConfig']);
  33. $this->_cleanUp();
  34. }
  35. protected function _writeInput(array $input = array()) {
  36. foreach ($input as $input) {
  37. fwrite($this->command->request->input, $input . "\n");
  38. }
  39. rewind($this->command->request->input);
  40. }
  41. public function testInit() {
  42. $command = new Extract();
  43. $this->assertEqual(realpath(Libraries::get(true, 'path')), $command->source);
  44. $this->assertEqual(Libraries::get(true, 'resources') . '/g11n', $command->destination);
  45. }
  46. public function testFailRead() {
  47. $this->_writeInput(array('', '', '', ''));
  48. $result = $this->command->run();
  49. $expected = 1;
  50. $this->assertIdentical($expected, $result);
  51. $expected = "Yielded no items.\n";
  52. $result = $this->command->response->error;
  53. $this->assertEqual($expected, $result);
  54. }
  55. public function testFailWrite() {
  56. rmdir($this->command->destination);
  57. $file = "{$this->_path}/source/a.html.php";
  58. $data = <<<EOD
  59. <h2>Flowers</h2>
  60. <?=\$t('Apples are green.'); ?>
  61. EOD;
  62. file_put_contents($file, $data);
  63. $configs = Catalog::config();
  64. $configKey = key($configs);
  65. $this->_writeInput(array($configKey, '', '', '', '', 'y'));
  66. $result = $this->command->run();
  67. $expected = 1;
  68. $this->assertIdentical($expected, $result);
  69. $expected = "Failed to write template.\n";
  70. $result = $this->command->response->error;
  71. $this->assertEqual($expected, $result);
  72. }
  73. public function testDefaultConfiguration() {
  74. $file = "{$this->_path}/source/a.html.php";
  75. $data = <<<EOD
  76. <h2>Flowers</h2>
  77. <?=\$t('Apples are green.'); ?>
  78. EOD;
  79. file_put_contents($file, $data);
  80. $configs = Catalog::config();
  81. $configKey1 = key($configs);
  82. next($configs);
  83. $configKey2 = key($configs);
  84. $this->_writeInput(array($configKey1, $configKey2, '', 'y'));
  85. $result = $this->command->run();
  86. $expected = 0;
  87. $this->assertIdentical($expected, $result);
  88. $expected = '/.*Yielded 1 item.*/';
  89. $result = $this->command->response->output;
  90. $this->assertPattern($expected, $result);
  91. $file = "{$this->_path}/destination/message_default.pot";
  92. $result = file_exists($file);
  93. $this->assertTrue($result);
  94. $result = file_get_contents($file);
  95. $expected = '/msgid "Apples are green\."/';
  96. $this->assertPattern($expected, $result);
  97. $expected = '#/tmp/tests/source(/|\\\\)a.html.php:2#';
  98. $this->assertPattern($expected, $result);
  99. $result = $this->command->response->error;
  100. $this->assertFalse($result);
  101. }
  102. }
  103. ?>