From 8004515b673cd803efcc295c17c4b373007bf1a1 Mon Sep 17 00:00:00 2001 From: Luuk van den Ouweland Date: Sat, 3 Feb 2018 18:08:38 +0100 Subject: [PATCH] Add first to Factory. (#31) Add tests to ViewFactoryTest. --- src/Factory.php | 22 +++++++++++- .../string_blade_compiler/ViewFactoryTest.php | 36 +++++++++++++++++++ 2 files changed, 57 insertions(+), 1 deletion(-) diff --git a/src/Factory.php b/src/Factory.php index 3ed535c..0744164 100644 --- a/src/Factory.php +++ b/src/Factory.php @@ -11,7 +11,6 @@ use Illuminate\Contracts\View\Factory as FactoryContract; use Illuminate\View\ViewName; use Illuminate\View\Engines\EngineResolver; -use Wpb\String_Blade_Compiler\StringView; class Factory implements FactoryContract { @@ -160,6 +159,27 @@ protected function makeStringView(array $view, $data=[]) }); } + /** + * Get the first view that actually exists from the given list. + * + * @param array $views + * @param array $data + * @param array $mergeData + * @return \Illuminate\Contracts\View\View|\Wpb\String_Blade_Compiler\StringView + */ + public function first(array $views, $data = [], $mergeData = []) + { + $view = collect($views)->first(function ($view) { + return $this->exists($view); + }); + + if (! $view) { + throw new InvalidArgumentException('None of the views in the given array exist.'); + } + + return $this->make($view, $data, $mergeData); + } + /** * Get the rendered content of the view based on a given condition. * diff --git a/tests/string_blade_compiler/ViewFactoryTest.php b/tests/string_blade_compiler/ViewFactoryTest.php index c70c036..bc78b64 100644 --- a/tests/string_blade_compiler/ViewFactoryTest.php +++ b/tests/string_blade_compiler/ViewFactoryTest.php @@ -39,6 +39,42 @@ public function testExistsPassesAndFailsViews() $this->assertTrue($factory->exists('bar')); } + public function testFirstCreatesNewViewInstanceWithProperPath() + { + unset($_SERVER['__test.view']); + + $factory = $this->getFactory(); + $factory->getFinder()->shouldReceive('find')->twice()->with('view')->andReturn('path.php'); + $factory->getFinder()->shouldReceive('find')->once()->with('bar')->andThrow('InvalidArgumentException'); + $factory->getEngineResolver()->shouldReceive('resolve')->once()->with('php')->andReturn($engine = m::mock(\Illuminate\Contracts\View\Engine::class)); + $factory->getFinder()->shouldReceive('addExtension')->once()->with('php'); + $factory->setDispatcher(new \Illuminate\Events\Dispatcher); + $factory->creator('view', function ($view) { + $_SERVER['__test.view'] = $view; + }); + $factory->addExtension('php', 'php'); + $view = $factory->first(['bar', 'view'], ['foo' => 'bar'], ['baz' => 'boom']); + + $this->assertSame($engine, $view->getEngine()); + $this->assertSame($_SERVER['__test.view'], $view); + + unset($_SERVER['__test.view']); + } + + /** + * @expectedException InvalidArgumentException + */ + public function testFirstThrowsInvalidArgumentExceptionIfNoneFound() + { + $factory = $this->getFactory(); + $factory->getFinder()->shouldReceive('find')->once()->with('view')->andThrow('InvalidArgumentException'); + $factory->getFinder()->shouldReceive('find')->once()->with('bar')->andThrow('InvalidArgumentException'); + $factory->getEngineResolver()->shouldReceive('resolve')->with('php')->andReturn($engine = m::mock(\Illuminate\Contracts\View\Engine::class)); + $factory->getFinder()->shouldReceive('addExtension')->with('php'); + $factory->addExtension('php', 'php'); + $view = $factory->first(['bar', 'view'], ['foo' => 'bar'], ['baz' => 'boom']); + } + public function testRenderEachCreatesViewForEachItemInArray() { $factory = m::mock('Wpb\String_Blade_Compiler\Factory[make]', $this->getFactoryArgs());