Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Artisan::call('migrate');
- Artisan::call('db:seed');
- /**
- * Creates the application.
- *
- * @return IlluminateFoundationApplication
- */
- public function createApplication()
- {
- return self::initialize();
- }
- private static $configurationApp = null;
- public static function initialize(){
- if(is_null(self::$configurationApp)){
- $app = require __DIR__.'/../bootstrap/app.php';
- $app->loadEnvironmentFrom('.env.testing');
- $app->make(IlluminateContractsConsoleKernel::class)->bootstrap();
- if (config('database.default') == 'sqlite') {
- $db = app()->make('db');
- $db->connection()->getPdo()->exec("pragma foreign_keys=1");
- }
- Artisan::call('migrate');
- Artisan::call('db:seed');
- self::$configurationApp = $app;
- return $app;
- }
- return self::$configurationApp;
- }
- public function tearDown()
- {
- if ($this->app) {
- foreach ($this->beforeApplicationDestroyedCallbacks as $callback) {
- call_user_func($callback);
- }
- }
- $this->setUpHasRun = false;
- if (property_exists($this, 'serverVariables')) {
- $this->serverVariables = [];
- }
- if (class_exists('Mockery')) {
- Mockery::close();
- }
- $this->afterApplicationCreatedCallbacks = [];
- $this->beforeApplicationDestroyedCallbacks = [];
- }
- protected static $applicationRefreshed = false;
- /**
- * Refresh the application instance.
- *
- * @return void
- */
- protected function forceRefreshApplication() {
- if (!is_null($this->app)) {
- $this->app->flush();
- }
- $this->app = null;
- self::$configurationApp = null;
- self::$applicationRefreshed = true;
- parent::refreshApplication();
- }
- if (self::$applicationRefreshed) {
- self::$applicationRefreshed = false;
- $this->app->flush();
- $this->app = null;
- self::$configurationApp = null;
- }
- php artisan migrate:rollback
- php artisan migrate --seed
- vendor/bin/phpunit
- $ php artisan migrate --database=mysql_testing
- $ php artisan db:seed --database=mysql_testing
- 'connections' => [
- 'mysql' => [
- 'driver' => 'mysql',
- 'host' => env('DB_HOST', 'localhost'),
- 'database' => env('DB_DATABASE', 'forge'),
- 'username' => env('DB_USERNAME', 'forge'),
- 'password' => env('DB_PASSWORD', ''),
- 'charset' => 'utf8',
- 'collation' => 'utf8_unicode_ci',
- 'prefix' => '',
- 'strict' => false,
- ],
- 'mysql_testing' => [
- 'driver' => 'mysql',
- 'host' => env('DB_HOST', 'localhost'),
- 'database' => env('DB_TEST_DATABASE'),
- 'username' => env('DB_USERNAME', 'forge'),
- 'password' => env('DB_PASSWORD', ''),
- 'charset' => 'utf8',
- 'collation' => 'utf8_unicode_ci',
- 'prefix' => '',
- 'strict' => false,
- ],
- ],
- DB_DATABASE=abc
- DB_TEST_DATABASE=abc_test
- <?xml version="1.0" encoding="UTF-8"?>
- <phpunit>
- ...
- <php>
- ...
- <env name="DB_CONNECTION" value="mysql_testing"/>
- </php>
- class MyTest extends TestCase
- {
- use IlluminateFoundationTestingDatabaseTransactions;
- public function testSomething()
- {
- <?php
- class TestCase extends IlluminateFoundationTestingTestCase
- {
- /**
- * The base URL to use while testing the application.
- *
- * @var string
- */
- protected $baseUrl = 'http://localhost';
- /**
- * Creates the application.
- *
- * @return IlluminateFoundationApplication
- */
- public function createApplication()
- {
- /** @var $app IlluminateFoundationApplication */
- $app = require __DIR__.'/../bootstrap/app.php';
- $app->loadEnvironmentFrom('.env.testing');
- $app->make(IlluminateContractsConsoleKernel::class)->bootstrap();
- return $app;
- }
- }
- APP_ENV=testing
- APP_DEBUG=true
- APP_KEY=xxx
- DB_CONNECTION=mysql
- DB_HOST=127.0.0.1
- DB_DATABASE=abc_testing
- DB_USERNAME=xxx
- DB_PASSWORD=xxx
- class MyTest extends TestCase
- {
- public static function setUpBeforeClass()
- {
- $config = parse_ini_file(".env.testing");
- $username = $config['DB_USERNAME'];
- $password = $config['DB_PASSWORD'];
- $database = $config['DB_DATABASE'];
- $host = $config['DB_HOST'];
- // Create test database
- $connection = new PDO("mysql:host={$host}", $username, $password);
- $connection->query("DROP DATABASE IF EXISTS " . $database);
- $connection->query("CREATE DATABASE " . $database);
- }
- public function testHomePage()
- {
- Artisan::call('migrate');
- Artisan::call('db:seed');
- $this->visit('/')
- ->see('Home')
- ->see('Please sign in')
- ->dontSee('Logout');
- }
- /**
- * Runs migrations for individual tests
- *
- * @param array $migrations
- * @return void
- */
- public function migrate(array $migrations = [])
- {
- $path = database_path('migrations');
- $migrator = app()->make('migrator');
- $migrator->getRepository()->createRepository();
- $files = $migrator->getMigrationFiles($path);
- if (!empty($migrations)) {
- $files = collect($files)->filter(
- function ($value, $key) use ($migrations) {
- if (in_array($key, $migrations)) {
- return [$key => $value];
- }
- }
- )->all();
- }
- $migrator->requireFiles($files);
- $migrator->runPending($files);
- }
- /**
- * Runs some or all seeds
- *
- * @param string $seed
- * @return void
- */
- public function seed(string $seed = '')
- {
- $command = "db:seed";
- if (empty($seed)) {
- Artisan::call($command);
- } else {
- Artisan::call($command, ['--class' => $seed]);
- }
- }
- $this->migrate(
- [
- '2013_10_11_081829_create_users_table',
- ]
- );
- $this->seed(UserTableSeeder::class);
Add Comment
Please, Sign In to add comment