/TemplateTranslationTiein/tests/provider.php

https://github.com/F5/zetacomponents · PHP · 243 lines · 180 code · 34 blank · 29 comment · 0 complexity · 537d8f8c433ef28290372655ab890e94 MD5 · raw file

  1. <?php
  2. /**
  3. *
  4. * Licensed to the Apache Software Foundation (ASF) under one
  5. * or more contributor license agreements. See the NOTICE file
  6. * distributed with this work for additional information
  7. * regarding copyright ownership. The ASF licenses this file
  8. * to you under the Apache License, Version 2.0 (the
  9. * "License"); you may not use this file except in compliance
  10. * with the License. You may obtain a copy of the License at
  11. *
  12. * http://www.apache.org/licenses/LICENSE-2.0
  13. *
  14. * Unless required by applicable law or agreed to in writing,
  15. * software distributed under the License is distributed on an
  16. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  17. * KIND, either express or implied. See the License for the
  18. * specific language governing permissions and limitations
  19. * under the License.
  20. *
  21. * @license http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0
  22. * @version //autogentag//
  23. * @filesource
  24. * @package TemplateTranslationTiein
  25. * @subpackage Tests
  26. */
  27. /**
  28. * @package TemplateTranslationTiein
  29. * @subpackage Tests
  30. */
  31. class ezcTemplateTranslationProviderTest extends ezcTestCase
  32. {
  33. public function setUp()
  34. {
  35. $ttc = ezcTemplateTranslationConfiguration::getInstance();
  36. $ttc->manager = null;
  37. }
  38. public function testTranslateManagerNotConfigured()
  39. {
  40. try
  41. {
  42. ezcTemplateTranslationProvider::translate( "Test", "test", array() );
  43. self::fail( "Expected exception not thrown." );
  44. }
  45. catch ( ezcTemplateTranslationManagerNotConfiguredException $e )
  46. {
  47. self::assertEquals( "The manager property of the ezcTemplateTranslationConfiguration has not been configured.", $e->getMessage() );
  48. }
  49. }
  50. public function testTranslate()
  51. {
  52. $ttc = ezcTemplateTranslationConfiguration::getInstance();
  53. $ttc->manager = new ezcTranslationManager( new ezcTranslationTsBackend( dirname( __FILE__ ) . '/translations' ) );
  54. self::assertEquals( 'Test Eén', ezcTemplateTranslationProvider::translate( "Test 1", "test", array() ) );
  55. }
  56. public function testTranslateWithNumericParams()
  57. {
  58. $ttc = ezcTemplateTranslationConfiguration::getInstance();
  59. $ttc->manager = new ezcTranslationManager( new ezcTranslationTsBackend( dirname( __FILE__ ) . '/translations' ) );
  60. self::assertEquals( 'Test Eén (a, c, b)', ezcTemplateTranslationProvider::translate( "Test 1 %1 %2 %3", "test", array( 1 => 'a', 'b', 'c' ) ) );
  61. }
  62. public function testTranslateWithNumericParamsWithMissingParam()
  63. {
  64. $ttc = ezcTemplateTranslationConfiguration::getInstance();
  65. $ttc->manager = new ezcTranslationManager( new ezcTranslationTsBackend( dirname( __FILE__ ) . '/translations' ) );
  66. try
  67. {
  68. ezcTemplateTranslationProvider::translate( "Test 1 %1 %2 %3", "test", array( 1 => 'a', 'b' ) );
  69. self::fail( 'Expected exception not thrown.' );
  70. }
  71. catch ( ezcTranslationParameterMissingException $e )
  72. {
  73. self::assertEquals( "The parameter '%3' does not exist.", $e->getMessage() );
  74. }
  75. }
  76. public function testTranslateWithNamedParams()
  77. {
  78. $ttc = ezcTemplateTranslationConfiguration::getInstance();
  79. $ttc->manager = new ezcTranslationManager( new ezcTranslationTsBackend( dirname( __FILE__ ) . '/translations' ) );
  80. self::assertEquals( 'Test Eén (a, c, b)', ezcTemplateTranslationProvider::translate( "Test 1 %un %deux %trois", "test", array( 'un' => 'a', 'deux' => 'b', 'trois' => 'c', 'quatre' => 'd' ) ) );
  81. }
  82. public function testTranslateWithNamedParamsWithMissingParam()
  83. {
  84. $ttc = ezcTemplateTranslationConfiguration::getInstance();
  85. $ttc->manager = new ezcTranslationManager( new ezcTranslationTsBackend( dirname( __FILE__ ) . '/translations' ) );
  86. try
  87. {
  88. ezcTemplateTranslationProvider::translate( "Test 1 %un %deux %trois", "test", array( 'un' => 'a', 'trois' => 'b' ) );
  89. self::fail( 'Expected exception not thrown.' );
  90. }
  91. catch ( ezcTranslationParameterMissingException $e )
  92. {
  93. self::assertEquals( "The parameter '%deux' does not exist.", $e->getMessage() );
  94. }
  95. }
  96. public function testTranslateWithWrongKey()
  97. {
  98. $ttc = ezcTemplateTranslationConfiguration::getInstance();
  99. $ttc->manager = new ezcTranslationManager( new ezcTranslationTsBackend( dirname( __FILE__ ) . '/translations' ) );
  100. try
  101. {
  102. ezcTemplateTranslationProvider::translate( "Test 2", "test", array() );
  103. self::fail( 'Expected exception not thrown.' );
  104. }
  105. catch ( ezcTranslationKeyNotAvailableException $e )
  106. {
  107. self::assertEquals( "The key 'Test 2' does not exist in the translation map.", $e->getMessage() );
  108. }
  109. }
  110. public function testTranslateWithWrongContext()
  111. {
  112. $ttc = ezcTemplateTranslationConfiguration::getInstance();
  113. $ttc->manager = new ezcTranslationManager( new ezcTranslationTsBackend( dirname( __FILE__ ) . '/translations' ) );
  114. try
  115. {
  116. ezcTemplateTranslationProvider::translate( "Test 3", "test2", array() );
  117. self::fail( 'Expected exception not thrown.' );
  118. }
  119. catch ( ezcTranslationContextNotAvailableException $e )
  120. {
  121. self::assertEquals( "The context 'test2' does not exist.", $e->getMessage() );
  122. }
  123. }
  124. public function testCompile()
  125. {
  126. $ttc = ezcTemplateTranslationConfiguration::getInstance();
  127. $ttc->manager = new ezcTranslationManager( new ezcTranslationTsBackend( dirname( __FILE__ ) . '/translations' ) );
  128. self::assertEquals( "'Test Eén'", ezcTemplateTranslationProvider::compile( "Test 1", "test", array() ) );
  129. }
  130. public function testCompileWithNumericParams()
  131. {
  132. $ttc = ezcTemplateTranslationConfiguration::getInstance();
  133. $ttc->manager = new ezcTranslationManager( new ezcTranslationTsBackend( dirname( __FILE__ ) . '/translations' ) );
  134. self::assertEquals( "'Test Eén (' . a . ', ' . c . ', ' . b . ')'", ezcTemplateTranslationProvider::compile( "Test 1 %1 %2 %3", "test", array( 1 => 'a', 'b', 'c' ) ) );
  135. }
  136. public function testCompileWithNumericParamsWithMissingParam()
  137. {
  138. $ttc = ezcTemplateTranslationConfiguration::getInstance();
  139. $ttc->manager = new ezcTranslationManager( new ezcTranslationTsBackend( dirname( __FILE__ ) . '/translations' ) );
  140. try
  141. {
  142. ezcTemplateTranslationProvider::compile( "Test 1 %1 %2 %3", "test", array( 1 => 'a', 'b' ) );
  143. self::fail( 'Expected exception not thrown.' );
  144. }
  145. catch ( ezcTranslationParameterMissingException $e )
  146. {
  147. self::assertEquals( "The parameter '%3' does not exist.", $e->getMessage() );
  148. }
  149. }
  150. public function testCompileWithQuote()
  151. {
  152. $ttc = ezcTemplateTranslationConfiguration::getInstance();
  153. $ttc->manager = new ezcTranslationManager( new ezcTranslationTsBackend( dirname( __FILE__ ) . '/translations' ) );
  154. self::assertEquals( "'He that loves to be flattered is worthy o\'' . the flatterer . '.'", ezcTemplateTranslationProvider::compile( "Test with quotes in result", "test", array( 'hvem' => ' the flatterer' ) ) );
  155. }
  156. public function testCompileWithNamedParams()
  157. {
  158. $ttc = ezcTemplateTranslationConfiguration::getInstance();
  159. $ttc->manager = new ezcTranslationManager( new ezcTranslationTsBackend( dirname( __FILE__ ) . '/translations' ) );
  160. self::assertEquals( "'Test Eén (' . a . ', ' . c . ', ' . b . ')'", ezcTemplateTranslationProvider::compile( "Test 1 %un %deux %trois", "test", array( 'un' => 'a', 'deux' => 'b', 'trois' => 'c', 'quatre' => 'd' ) ) );
  161. }
  162. public function testCompileWithNamedParamsWithMissingParam()
  163. {
  164. $ttc = ezcTemplateTranslationConfiguration::getInstance();
  165. $ttc->manager = new ezcTranslationManager( new ezcTranslationTsBackend( dirname( __FILE__ ) . '/translations' ) );
  166. try
  167. {
  168. ezcTemplateTranslationProvider::compile( "Test 1 %un %deux %trois", "test", array( 'un' => 'a', 'trois' => 'b' ) );
  169. self::fail( 'Expected exception not thrown.' );
  170. }
  171. catch ( ezcTranslationParameterMissingException $e )
  172. {
  173. self::assertEquals( "The parameter '%deux' does not exist.", $e->getMessage() );
  174. }
  175. }
  176. public function testCompileWithWrongKey()
  177. {
  178. $ttc = ezcTemplateTranslationConfiguration::getInstance();
  179. $ttc->manager = new ezcTranslationManager( new ezcTranslationTsBackend( dirname( __FILE__ ) . '/translations' ) );
  180. try
  181. {
  182. ezcTemplateTranslationProvider::compile( "Test 2", "test", array() );
  183. self::fail( 'Expected exception not thrown.' );
  184. }
  185. catch ( ezcTranslationKeyNotAvailableException $e )
  186. {
  187. self::assertEquals( "The key 'Test 2' does not exist in the translation map.", $e->getMessage() );
  188. }
  189. }
  190. public function testCompileWithWrongContext()
  191. {
  192. $ttc = ezcTemplateTranslationConfiguration::getInstance();
  193. $ttc->manager = new ezcTranslationManager( new ezcTranslationTsBackend( dirname( __FILE__ ) . '/translations' ) );
  194. try
  195. {
  196. ezcTemplateTranslationProvider::compile( "Test 3", "test2", array() );
  197. self::fail( 'Expected exception not thrown.' );
  198. }
  199. catch ( ezcTranslationContextNotAvailableException $e )
  200. {
  201. self::assertEquals( "The context 'test2' does not exist.", $e->getMessage() );
  202. }
  203. }
  204. public static function suite()
  205. {
  206. return new PHPUnit_Framework_TestSuite( 'ezcTemplateTranslationProviderTest' );
  207. }
  208. }
  209. ?>