Guest User

Untitled

a guest
Mar 13th, 2018
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.85 KB | None | 0 0
  1. Artisan::call('migrate');
  2. Artisan::call('db:seed');
  3.  
  4. /**
  5. * Creates the application.
  6. *
  7. * @return IlluminateFoundationApplication
  8. */
  9. public function createApplication()
  10. {
  11. return self::initialize();
  12. }
  13.  
  14. private static $configurationApp = null;
  15. public static function initialize(){
  16.  
  17. if(is_null(self::$configurationApp)){
  18. $app = require __DIR__.'/../bootstrap/app.php';
  19.  
  20. $app->loadEnvironmentFrom('.env.testing');
  21.  
  22. $app->make(IlluminateContractsConsoleKernel::class)->bootstrap();
  23.  
  24. if (config('database.default') == 'sqlite') {
  25. $db = app()->make('db');
  26. $db->connection()->getPdo()->exec("pragma foreign_keys=1");
  27. }
  28.  
  29. Artisan::call('migrate');
  30. Artisan::call('db:seed');
  31.  
  32. self::$configurationApp = $app;
  33. return $app;
  34. }
  35.  
  36. return self::$configurationApp;
  37. }
  38.  
  39. public function tearDown()
  40. {
  41. if ($this->app) {
  42. foreach ($this->beforeApplicationDestroyedCallbacks as $callback) {
  43. call_user_func($callback);
  44. }
  45.  
  46. }
  47.  
  48. $this->setUpHasRun = false;
  49.  
  50. if (property_exists($this, 'serverVariables')) {
  51. $this->serverVariables = [];
  52. }
  53.  
  54. if (class_exists('Mockery')) {
  55. Mockery::close();
  56. }
  57.  
  58. $this->afterApplicationCreatedCallbacks = [];
  59. $this->beforeApplicationDestroyedCallbacks = [];
  60. }
  61.  
  62. protected static $applicationRefreshed = false;
  63.  
  64. /**
  65. * Refresh the application instance.
  66. *
  67. * @return void
  68. */
  69. protected function forceRefreshApplication() {
  70. if (!is_null($this->app)) {
  71. $this->app->flush();
  72. }
  73. $this->app = null;
  74. self::$configurationApp = null;
  75. self::$applicationRefreshed = true;
  76. parent::refreshApplication();
  77. }
  78.  
  79. if (self::$applicationRefreshed) {
  80. self::$applicationRefreshed = false;
  81. $this->app->flush();
  82. $this->app = null;
  83. self::$configurationApp = null;
  84. }
  85.  
  86. php artisan migrate:rollback
  87. php artisan migrate --seed
  88. vendor/bin/phpunit
  89.  
  90. $ php artisan migrate --database=mysql_testing
  91. $ php artisan db:seed --database=mysql_testing
  92.  
  93. 'connections' => [
  94. 'mysql' => [
  95. 'driver' => 'mysql',
  96. 'host' => env('DB_HOST', 'localhost'),
  97. 'database' => env('DB_DATABASE', 'forge'),
  98. 'username' => env('DB_USERNAME', 'forge'),
  99. 'password' => env('DB_PASSWORD', ''),
  100. 'charset' => 'utf8',
  101. 'collation' => 'utf8_unicode_ci',
  102. 'prefix' => '',
  103. 'strict' => false,
  104. ],
  105. 'mysql_testing' => [
  106. 'driver' => 'mysql',
  107. 'host' => env('DB_HOST', 'localhost'),
  108. 'database' => env('DB_TEST_DATABASE'),
  109. 'username' => env('DB_USERNAME', 'forge'),
  110. 'password' => env('DB_PASSWORD', ''),
  111. 'charset' => 'utf8',
  112. 'collation' => 'utf8_unicode_ci',
  113. 'prefix' => '',
  114. 'strict' => false,
  115. ],
  116. ],
  117.  
  118. DB_DATABASE=abc
  119. DB_TEST_DATABASE=abc_test
  120.  
  121. <?xml version="1.0" encoding="UTF-8"?>
  122. <phpunit>
  123. ...
  124. <php>
  125. ...
  126. <env name="DB_CONNECTION" value="mysql_testing"/>
  127. </php>
  128.  
  129. class MyTest extends TestCase
  130. {
  131. use IlluminateFoundationTestingDatabaseTransactions;
  132.  
  133. public function testSomething()
  134. {
  135.  
  136. <?php
  137.  
  138. class TestCase extends IlluminateFoundationTestingTestCase
  139. {
  140. /**
  141. * The base URL to use while testing the application.
  142. *
  143. * @var string
  144. */
  145. protected $baseUrl = 'http://localhost';
  146.  
  147. /**
  148. * Creates the application.
  149. *
  150. * @return IlluminateFoundationApplication
  151. */
  152. public function createApplication()
  153. {
  154. /** @var $app IlluminateFoundationApplication */
  155. $app = require __DIR__.'/../bootstrap/app.php';
  156. $app->loadEnvironmentFrom('.env.testing');
  157.  
  158. $app->make(IlluminateContractsConsoleKernel::class)->bootstrap();
  159.  
  160. return $app;
  161. }
  162. }
  163.  
  164. APP_ENV=testing
  165. APP_DEBUG=true
  166. APP_KEY=xxx
  167.  
  168. DB_CONNECTION=mysql
  169. DB_HOST=127.0.0.1
  170. DB_DATABASE=abc_testing
  171. DB_USERNAME=xxx
  172. DB_PASSWORD=xxx
  173.  
  174. class MyTest extends TestCase
  175. {
  176. public static function setUpBeforeClass()
  177. {
  178. $config = parse_ini_file(".env.testing");
  179. $username = $config['DB_USERNAME'];
  180. $password = $config['DB_PASSWORD'];
  181. $database = $config['DB_DATABASE'];
  182. $host = $config['DB_HOST'];
  183.  
  184. // Create test database
  185. $connection = new PDO("mysql:host={$host}", $username, $password);
  186. $connection->query("DROP DATABASE IF EXISTS " . $database);
  187. $connection->query("CREATE DATABASE " . $database);
  188. }
  189.  
  190. public function testHomePage()
  191. {
  192. Artisan::call('migrate');
  193. Artisan::call('db:seed');
  194.  
  195. $this->visit('/')
  196. ->see('Home')
  197. ->see('Please sign in')
  198. ->dontSee('Logout');
  199. }
  200.  
  201. /**
  202. * Runs migrations for individual tests
  203. *
  204. * @param array $migrations
  205. * @return void
  206. */
  207. public function migrate(array $migrations = [])
  208. {
  209. $path = database_path('migrations');
  210. $migrator = app()->make('migrator');
  211. $migrator->getRepository()->createRepository();
  212. $files = $migrator->getMigrationFiles($path);
  213.  
  214. if (!empty($migrations)) {
  215. $files = collect($files)->filter(
  216. function ($value, $key) use ($migrations) {
  217. if (in_array($key, $migrations)) {
  218. return [$key => $value];
  219. }
  220. }
  221. )->all();
  222. }
  223.  
  224. $migrator->requireFiles($files);
  225. $migrator->runPending($files);
  226. }
  227.  
  228. /**
  229. * Runs some or all seeds
  230. *
  231. * @param string $seed
  232. * @return void
  233. */
  234. public function seed(string $seed = '')
  235. {
  236. $command = "db:seed";
  237.  
  238. if (empty($seed)) {
  239. Artisan::call($command);
  240. } else {
  241. Artisan::call($command, ['--class' => $seed]);
  242. }
  243. }
  244.  
  245. $this->migrate(
  246. [
  247. '2013_10_11_081829_create_users_table',
  248. ]
  249. );
  250. $this->seed(UserTableSeeder::class);
Add Comment
Please, Sign In to add comment