XliffValidatorTestCase.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. <?php
  2. /*
  3. * This file is part of the Sonata Project package.
  4. *
  5. * (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Sonata\CoreBundle\Test;
  11. use Symfony\Component\Translation\Exception\InvalidResourceException;
  12. use Symfony\Component\Translation\Loader\XliffFileLoader;
  13. abstract class XliffValidatorTestCase extends \PHPUnit_Framework_TestCase
  14. {
  15. /**
  16. * @var XliffFileLoader
  17. */
  18. protected $loader;
  19. /**
  20. * @var string[]
  21. */
  22. protected $errors = array();
  23. public function setUp()
  24. {
  25. $this->loader = new XliffFileLoader();
  26. }
  27. /**
  28. * @dataProvider getXliffPaths
  29. */
  30. public function testXliff($path)
  31. {
  32. $this->validatePath($path);
  33. if (count($this->errors) > 0) {
  34. $this->fail(sprintf('Unable to parse xliff files: %s', implode(', ', $this->errors)));
  35. }
  36. }
  37. /**
  38. * @return array List all path to validate xliff
  39. */
  40. abstract public function getXliffPaths();
  41. /**
  42. * @param string $file The path to the xliff file
  43. */
  44. protected function validateXliff($file)
  45. {
  46. try {
  47. $this->loader->load($file, 'en');
  48. $this->assertTrue(true, sprintf('Successful loading file: %s', $file));
  49. } catch (InvalidResourceException $e) {
  50. $this->errors[] = sprintf('%s => %s', $file, $e->getMessage());
  51. }
  52. }
  53. /**
  54. * @param string $path The path to lookup for Xliff file
  55. */
  56. protected function validatePath($path)
  57. {
  58. $files = glob(sprintf('%s/*.xliff', $path));
  59. foreach ($files as $file) {
  60. $this->validateXliff($file);
  61. }
  62. }
  63. }