TestCase.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. <?php declare(strict_types=1);
  2. namespace mikemadisonweb\rabbitmq\tests;
  3. use mikemadisonweb\rabbitmq\DependencyInjection;
  4. use Yii;
  5. use yii\helpers\ArrayHelper;
  6. abstract class TestCase extends \PHPUnit\Framework\TestCase
  7. {
  8. public static $params;
  9. /**
  10. * Clean up after test.
  11. * By default the application created with [[mockApplication]] will be destroyed.
  12. */
  13. protected function tearDown(): void
  14. {
  15. parent::tearDown();
  16. $this->destroyApplication();
  17. }
  18. /**
  19. * Populates Yii::$app with a new application
  20. * The application will be destroyed on tearDown() automatically.
  21. * @param array $config The application configuration, if needed
  22. * @param string $appClass name of the application class to create
  23. */
  24. protected function mockApplication($config = [], $appClass = '\yii\console\Application')
  25. {
  26. new $appClass(ArrayHelper::merge([
  27. 'id' => 'testapp',
  28. 'basePath' => __DIR__,
  29. 'vendorPath' => $this->getVendorPath(),
  30. ], $config));
  31. }
  32. protected function getVendorPath()
  33. {
  34. $vendor = dirname(__DIR__, 2) . '/vendor';
  35. if (!is_dir($vendor)) {
  36. $vendor = dirname(__DIR__, 4);
  37. }
  38. return $vendor;
  39. }
  40. /**
  41. * Destroys application in Yii::$app by setting it to null.
  42. */
  43. protected function destroyApplication()
  44. {
  45. if (\Yii::$app && \Yii::$app->has('session', true)) {
  46. \Yii::$app->session->close();
  47. }
  48. \Yii::$app = null;
  49. }
  50. /**
  51. * Invokes a inaccessible method.
  52. * @param $object
  53. * @param $method
  54. * @param array $args
  55. * @param bool $revoke whether to make method inaccessible after execution
  56. * @return mixed
  57. * @since 2.0.11
  58. */
  59. protected function invokeMethod($object, $method, $args = [], $revoke = true)
  60. {
  61. $reflection = new \ReflectionObject($object);
  62. $method = $reflection->getMethod($method);
  63. $method->setAccessible(true);
  64. $result = $method->invokeArgs($object, $args);
  65. if ($revoke) {
  66. $method->setAccessible(false);
  67. }
  68. return $result;
  69. }
  70. /**
  71. * Sets an inaccessible object property to a designated value.
  72. * @param $object
  73. * @param $propertyName
  74. * @param $value
  75. * @param bool $revoke whether to make property inaccessible after setting
  76. * @since 2.0.11
  77. */
  78. protected function setInaccessibleProperty($object, $propertyName, $value, $revoke = true)
  79. {
  80. $class = new \ReflectionClass($object);
  81. while (!$class->hasProperty($propertyName)) {
  82. $class = $class->getParentClass();
  83. }
  84. $property = $class->getProperty($propertyName);
  85. $property->setAccessible(true);
  86. $property->setValue($object, $value);
  87. if ($revoke) {
  88. $property->setAccessible(false);
  89. }
  90. }
  91. /**
  92. * Gets an inaccessible object property.
  93. * @param $object
  94. * @param $propertyName
  95. * @param bool $revoke whether to make property inaccessible after getting
  96. * @return mixed
  97. */
  98. protected function getInaccessibleProperty($object, $propertyName, $revoke = true)
  99. {
  100. $class = new \ReflectionClass($object);
  101. while (!$class->hasProperty($propertyName)) {
  102. $class = $class->getParentClass();
  103. }
  104. $property = $class->getProperty($propertyName);
  105. $property->setAccessible(true);
  106. $result = $property->getValue($object);
  107. if ($revoke) {
  108. $property->setAccessible(false);
  109. }
  110. return $result;
  111. }
  112. /**
  113. * Load extension to test app instance
  114. * @param array $config
  115. * @throws \mikemadisonweb\rabbitmq\exceptions\InvalidConfigException
  116. */
  117. protected function loadExtension(array $config)
  118. {
  119. $this->mockApplication($config);
  120. $di = new DependencyInjection();
  121. $di->bootstrap(\Yii::$app);
  122. }
  123. }