mkdir -p src/Models src/database/migrations
.
├── composer.json
├── phpunit.xml
├── src
│ ├── database
│ │ └── migrations
│ └── Models
└── tests
├── BasicTest.php
└── OrchestraTest.php
cd laravel
php artisan make:model Everlasting -m
php artisan make:model Duration -m
mv app/Duration.php src/Models/.
mv app/Everlasting.php src/Models/.
mv database/migrations/*everlasting* src/database/migrations/.
mv database/migrations/*duration* src/database/migrations/.
.
├── composer.json
├── phpunit.xml
├── src
│ ├── database
│ │ └── migrations
│ │ ├── 0000_00_00_000000_create_durations_table.php
│ │ └── 0000_00_00_000000_create_everlastings_table.php
│ └── Models
│ ├── Duration.php
│ └── Everlasting.php
└── tests
├── BasicTest.php
└── OrchestraTest.php
<?php
- namespace App;
+ namespace Poing\Eviternity\Models;
use Illuminate\Database\Eloquent\Model;
class Duration extends Model
{
- //
+ /**
+ * Used to test access to this class
+ */
+ public static function probe()
+ {
+ return true;
+ }
}
php artisan make:provider EviternityServiceProvider
mv app/Providers/EviternityServiceProvider.php src/.
<?php
- namespace App\Providers;
+ namespace Poing\Eviternity;
use Illuminate\Support\ServiceProvider;
class EviternityServiceProvider extends ServiceProvider
{
...
{
"name": "poing/eviternity",
"require": {},
"require-dev": {
"orchestra/testbench": "3.7"
},
"autoload": {
"psr-4": {
"Poing\\Eviternity\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"Poing\\Eviternity\\Tests\\": "tests/"
}
- }
+ },
+ "extra": {
+ "laravel": {
+ "providers": [
+ "Poing\\Eviternity\\EviternityServiceProvider"
+ ]
+ }
+ }
}
We'll add loadMigrationsFrom()
in the boot()
section of our Service Provider.
<?php
namespace Poing\Eviternity;
use Illuminate\Support\ServiceProvider;
class EviternityServiceProvider extends ServiceProvider
{
/**
* Bootstrap services.
*
* @return void
*/
public function boot()
{
- //
+ // Load Migrations
+ $this->loadMigrationsFrom(__DIR__.'/database/migrations');
}
There are three 3
tasks necessary to use orchestra/testbench
in our tests.
- Set up the database used for testing
- Run the migrations
- Load the Laravel Service Provider
We'll use sqlite
for our testing.
<?php
namespace Poing\Eviternity\Tests\Models;
use Orchestra\Testbench\TestCase;
use Poing\Eviternity\Models\Duration as Duration;
class DurationTest extends TestCase
{
+ protected function getEnvironmentSetUp($app)
+ {
+ $app['config']->set('database.default', 'testbench');
+ $app['config']->set('database.connections.testbench', [
+ 'driver' => 'sqlite',
+ 'database' => ':memory:',
+ 'prefix' => '',
+ ]);
+ }
...
Will call setUp()
to initialize the orchestra/testbench
and run artisan migrate
.
<?php
namespace Poing\Eviternity\Tests\Models;
use Orchestra\Testbench\TestCase;
use Poing\Eviternity\Models\Duration as Duration;
class DurationTest extends TestCase
{
+ protected function setUp(): void
+ {
+ parent::setUp();
+ $this->artisan('migrate');
+ }
...
We'll add our Service Provider and any others necessary for the package to run properly.
<?php
namespace Poing\Eviternity\Tests\Models;
use Orchestra\Testbench\TestCase;
use Poing\Eviternity\Models\Duration as Duration;
class DurationTest extends TestCase
{
+ protected function getPackageProviders($app)
+ {
+ return [
+ \Poing\Eviternity\EviternityServiceProvider::class,
+ ];
+ }
...