TestCase.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. <?php
  2. namespace yiiunit\extensions\mongodb;
  3. use yii\helpers\ArrayHelper;
  4. use yii\helpers\FileHelper;
  5. use yii\mongodb\Connection;
  6. use Yii;
  7. use yii\mongodb\Exception;
  8. abstract class TestCase extends \PHPUnit\Framework\TestCase
  9. {
  10. public static $params;
  11. /**
  12. * @var array Mongo connection configuration.
  13. */
  14. protected $mongoDbConfig = [
  15. 'dsn' => 'mongodb://localhost:27017',
  16. 'defaultDatabaseName' => 'yii2test',
  17. 'options' => [],
  18. ];
  19. /**
  20. * @var Connection Mongo connection instance.
  21. */
  22. protected $mongodb;
  23. protected function setUp()
  24. {
  25. parent::setUp();
  26. if (!extension_loaded('mongodb')) {
  27. $this->markTestSkipped('mongodb extension required.');
  28. }
  29. $config = self::getParam('mongodb');
  30. if (!empty($config)) {
  31. $this->mongoDbConfig = $config;
  32. }
  33. //$this->mockApplication();
  34. }
  35. protected function tearDown()
  36. {
  37. if ($this->mongodb) {
  38. $this->mongodb->close();
  39. }
  40. $this->destroyApplication();
  41. $this->removeTestFilePath();
  42. }
  43. /**
  44. * Returns a test configuration param from /data/config.php
  45. * @param string $name params name
  46. * @param mixed $default default value to use when param is not set.
  47. * @return mixed the value of the configuration param
  48. */
  49. public static function getParam($name, $default = null)
  50. {
  51. if (static::$params === null) {
  52. static::$params = require(__DIR__ . '/data/config.php');
  53. }
  54. return isset(static::$params[$name]) ? static::$params[$name] : $default;
  55. }
  56. /**
  57. * Populates Yii::$app with a new application
  58. * The application will be destroyed on tearDown() automatically.
  59. * @param array $config The application configuration, if needed
  60. * @param string $appClass name of the application class to create
  61. */
  62. protected function mockApplication($config = [], $appClass = '\yii\console\Application')
  63. {
  64. new $appClass(ArrayHelper::merge([
  65. 'id' => 'testapp',
  66. 'basePath' => __DIR__,
  67. 'vendorPath' => $this->getVendorPath(),
  68. 'runtimePath' => dirname(__DIR__) . '/runtime',
  69. ], $config));
  70. }
  71. protected function getVendorPath()
  72. {
  73. $vendor = dirname(dirname(__DIR__)) . '/vendor';
  74. if (!is_dir($vendor)) {
  75. $vendor = dirname(dirname(dirname(dirname(__DIR__))));
  76. }
  77. return $vendor;
  78. }
  79. /**
  80. * Destroys application in Yii::$app by setting it to null.
  81. */
  82. protected function destroyApplication()
  83. {
  84. Yii::$app = null;
  85. }
  86. /**
  87. * @param bool $reset whether to clean up the test database
  88. * @param bool $open whether to open test database
  89. * @return \yii\mongodb\Connection
  90. */
  91. public function getConnection($reset = false, $open = true)
  92. {
  93. if (!$reset && $this->mongodb) {
  94. return $this->mongodb;
  95. }
  96. $db = new Connection();
  97. $db->dsn = $this->mongoDbConfig['dsn'];
  98. if (isset($this->mongoDbConfig['defaultDatabaseName'])) {
  99. $db->defaultDatabaseName = $this->mongoDbConfig['defaultDatabaseName'];
  100. }
  101. if (isset($this->mongoDbConfig['options'])) {
  102. $db->options = $this->mongoDbConfig['options'];
  103. }
  104. $db->enableLogging = true;
  105. $db->enableProfiling = true;
  106. if ($open) {
  107. $db->open();
  108. }
  109. $this->mongodb = $db;
  110. return $db;
  111. }
  112. /**
  113. * Drops the specified collection.
  114. * @param string $name collection name.
  115. */
  116. protected function dropCollection($name)
  117. {
  118. if ($this->mongodb) {
  119. try {
  120. $this->mongodb->createCommand()->dropCollection($name);
  121. } catch (Exception $e) {
  122. // shut down exception
  123. }
  124. }
  125. }
  126. /**
  127. * Drops the specified file collection.
  128. * @param string $name file collection name.
  129. */
  130. protected function dropFileCollection($name = 'fs')
  131. {
  132. if ($this->mongodb) {
  133. try {
  134. $this->mongodb->getFileCollection($name)->drop();
  135. } catch (Exception $e) {
  136. // shut down exception
  137. }
  138. }
  139. }
  140. /**
  141. * Finds all records in collection.
  142. * @param \yii\mongodb\Collection $collection
  143. * @param array $condition
  144. * @param array $fields
  145. * @return array rows
  146. */
  147. protected function findAll($collection, $condition = [], $fields = [])
  148. {
  149. $cursor = $collection->find($condition, $fields);
  150. $result = [];
  151. foreach ($cursor as $data) {
  152. $result[] = $data;
  153. }
  154. return $result;
  155. }
  156. /**
  157. * @return string test file path
  158. */
  159. protected function getTestFilePath()
  160. {
  161. return dirname(__DIR__) . '/runtime/test-tmp';
  162. }
  163. /**
  164. * Ensures test file path exists.
  165. * @return string test file path
  166. */
  167. protected function ensureTestFilePath()
  168. {
  169. $path = $this->getTestFilePath();
  170. FileHelper::createDirectory($path);
  171. return $path;
  172. }
  173. /**
  174. * Removes the test file path.
  175. */
  176. protected function removeTestFilePath()
  177. {
  178. $path = $this->getTestFilePath();
  179. FileHelper::removeDirectory($path);
  180. }
  181. /**
  182. * Invokes a inaccessible method
  183. * @param object $object
  184. * @param string $method
  185. * @param array $args
  186. * @return mixed
  187. * @since 2.1.3
  188. */
  189. protected function invokeMethod($object, $method, $args = [])
  190. {
  191. $reflection = new \ReflectionClass($object->className());
  192. $method = $reflection->getMethod($method);
  193. $method->setAccessible(true);
  194. return $method->invokeArgs($object, $args);
  195. }
  196. }