AbstractTransformerTestBase.php 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. <?php
  2. /*
  3. * This file is part of the PHP CS utility.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * This source file is subject to the MIT license that is bundled
  8. * with this source code in the file LICENSE.
  9. */
  10. namespace Symfony\CS\Tests\Tokenizer;
  11. use Symfony\CS\Tokenizer\Transformers;
  12. /**
  13. * @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
  14. */
  15. abstract class AbstractTransformerTestBase extends \PHPUnit_Framework_TestCase
  16. {
  17. protected static $transformer;
  18. protected static $transformers;
  19. public static function setUpBeforeClass()
  20. {
  21. static::$transformers = static::getTransformers();
  22. static::$transformer = static::getTransformer();
  23. }
  24. public static function tearDownAfterClass()
  25. {
  26. static::$transformer = null;
  27. static::$transformers = null;
  28. }
  29. protected static function getTransformer()
  30. {
  31. $transformerClass = 'Symfony\CS\Tokenizer'.substr(get_called_class(), strlen(__NAMESPACE__), -strlen('Test'));
  32. $transformersReflection = new \ReflectionClass(static::$transformers);
  33. $propertyReflection = $transformersReflection->getProperty('items');
  34. $propertyReflection->setAccessible(true);
  35. $items = $propertyReflection->getValue(static::$transformers);
  36. foreach ($items as $item) {
  37. if ($item instanceof $transformerClass) {
  38. return $item;
  39. }
  40. }
  41. throw new \RuntimeException(sprintf('Transformer class "%s" not found.', $transformerClass));
  42. }
  43. protected static function getTransformers()
  44. {
  45. return Transformers::create();
  46. }
  47. }