Advertisement
Guest User

Untitled

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