Skip to content

Commit

Permalink
Add first to Factory. (#31)
Browse files Browse the repository at this point in the history
Add tests to ViewFactoryTest.
  • Loading branch information
Luukvdo authored and TerrePorter committed Feb 3, 2018
1 parent fe55417 commit 8004515
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 1 deletion.
22 changes: 21 additions & 1 deletion src/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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.
*
Expand Down
36 changes: 36 additions & 0 deletions tests/string_blade_compiler/ViewFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down

0 comments on commit 8004515

Please sign in to comment.