So we are building a package for laravel, and would like to have test on our Service Provider to make sure that we are interacting with the Laravel Service Provider as expected.
We are registering configs with the app, and just want to make sure that we are calling the correct methods. In the provider, we are using the global "config" function to get the path to the config files, which cannot be easly mocked out.
Here is what the service provider looks like...
<?php
namespace Package\GreatThing;
use Illuminate\Support\ServiceProvider;
/**
* Class GreatThingServiceProvider
*
* @package Package\GreatThing
*/
class GreatThingServiceProvider extends ServiceProvider
{
/**
* Perform post-registration booting of services.
*
* @return void
*/
public function boot()
{
$config_file = realpath(__DIR__ . '/config/greatthing.php');
$this->publishes([
$config_file => config_path('greatthing.php'),
]);
$this->mergeConfigFrom($config_file, 'greatthing');
}
/**
* Register the service provider.
*
* @return void
*/
public function register()
{
//
}
}
Here is what the test looks like...
<?php
namespace Tests\Package\GreatThing;
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Support\ServiceProvider;
use Mockery;
use Package\GreatThing\GreatThingServiceProvider;
class GreatThingServiceProviderTest extends TestCase
{
/**
* @var Mockery\Mock
*/
protected $application_mock;
/**
* @var ServiceProvider
*/
protected $service_provider;
protected function setUp()
{
$this->setUpMocks();
$this->service_provider = new GreatThingServiceProvider($this->application_mock);
parent::setUp();
}
protected function setUpMocks()
{
$this->application_mock = Mockery::mock(Application::class);
}
/**
* @test
*/
public function it_can_be_constructed()
{
$this->assertInstanceOf(ServiceProvider::class, $this->service_provider);
}
/**
* @test
*/
public function it_does_nothing_in_the_register_method()
{
$this->assertNull($this->service_provider->register());
}
/**
* @test
*/
public function it_performs_a_boot_method()
{
$this->application_mock->shouldReceive('publishes')
->once()
->with([
'/some/path/greatthing.php',
])
->andReturnNull();
$this->application_mock->shouldReceive('mergeConfigFrom')
->once()
->withArgs([
'/config/greatthing.php',
'greatthing',
])
->andReturnNull();
$this->service_provider->boot();
}
}
Does anyone have any good ways to get to the config path?