From b976cf85d156dd08df4fba7b091bca7f8bde7462 Mon Sep 17 00:00:00 2001 From: Joe Lambert Date: Tue, 12 Nov 2024 16:23:51 +0000 Subject: [PATCH] Migrate Timber instantiation --- src/Providers/TimberServiceProvider.php | 5 +--- .../Providers/TimberServiceProviderTest.php | 26 ++++++++++++------- 2 files changed, 17 insertions(+), 14 deletions(-) diff --git a/src/Providers/TimberServiceProvider.php b/src/Providers/TimberServiceProvider.php index 29fd12c..837aa63 100644 --- a/src/Providers/TimberServiceProvider.php +++ b/src/Providers/TimberServiceProvider.php @@ -9,10 +9,7 @@ class TimberServiceProvider extends ServiceProvider { public function register() { - $timber = new Timber(); - - $this->app->bind('timber', $timber); - $this->app->bind(Timber::class, $timber); + Timber::init(); } public function boot(Config $config) diff --git a/tests/Unit/Providers/TimberServiceProviderTest.php b/tests/Unit/Providers/TimberServiceProviderTest.php index 960576a..734470d 100644 --- a/tests/Unit/Providers/TimberServiceProviderTest.php +++ b/tests/Unit/Providers/TimberServiceProviderTest.php @@ -2,18 +2,25 @@ namespace Rareloop\Lumberjack\Test\Providers; +use Mockery; use Brain\Monkey; +use Timber\Timber; use Brain\Monkey\Functions; use PHPUnit\Framework\TestCase; +use Rareloop\Lumberjack\Config; use Rareloop\Lumberjack\Application; +use Rareloop\Lumberjack\Http\Lumberjack; use Rareloop\Lumberjack\Bootstrappers\BootProviders; use Rareloop\Lumberjack\Bootstrappers\RegisterProviders; -use Rareloop\Lumberjack\Config; -use Rareloop\Lumberjack\Http\Lumberjack; use Rareloop\Lumberjack\Providers\TimberServiceProvider; use Rareloop\Lumberjack\Test\Unit\BrainMonkeyPHPUnitIntegration; -use Timber\Timber; +/** + * @runTestsInSeparateProcesses + * @preserveGlobalState disabled + * The above is required as we're using alias mocks which persist between tests + * https://laracasts.com/discuss/channels/testing/mocking-a-class-persists-over-tests/replies/103075 + */ class TimberServiceProviderTest extends TestCase { use BrainMonkeyPHPUnitIntegration; @@ -23,20 +30,20 @@ public function timber_plugin_is_initialiased() { Functions\expect('is_admin')->once()->andReturn(false); - $app = new Application(__DIR__.'/../'); + $timber = Mockery::mock('alias:' . Timber::class); + $timber->shouldReceive('init')->once(); + + $app = new Application(__DIR__ . '/../'); $lumberjack = new Lumberjack($app); $app->register(new TimberServiceProvider($app)); $lumberjack->bootstrap(); - - $this->assertTrue($app->has('timber')); - $this->assertSame($app->get('timber'), $app->get(Timber::class)); } /** @test */ public function dirname_variable_is_set_from_config() { - $app = new Application(__DIR__.'/../'); + $app = new Application(__DIR__ . '/../'); $config = new Config; $config->set('timber.paths', [ @@ -55,11 +62,10 @@ public function dirname_variable_is_set_from_config() $app->register(new TimberServiceProvider($app)); - $this->assertTrue($app->has('timber')); $this->assertSame([ 'path/one', 'path/two', 'path/three', - ], $app->get('timber')::$dirname); + ], Timber::$dirname); } }