Skip to content

Commit

Permalink
formatting, rename method to first, support arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
taylorotwell committed Aug 25, 2017
1 parent 1672a71 commit f18318b
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 20 deletions.
38 changes: 21 additions & 17 deletions src/Illuminate/View/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,27 @@ public function make($view, $data = [], $mergeData = [])
});
}

/**
* 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
*/
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 Expand Up @@ -244,23 +265,6 @@ public function exists($view)
return true;
}

/**
* Get the evaluated view contents for the given view, or for a fallback
* view if the given view doesn't exist.
*
* @param string $view
* @param string $fallback
* @param array $data
* @param array $mergeData
* @return \Illuminate\Contracts\View\View
*/
public function either($view, $fallback, $data = [], $mergeData = [])
{
$view = $this->exists($view) ? $view : $fallback;

return $this->make($view, $data, $mergeData);
}

/**
* Get the appropriate view engine for the given path.
*
Expand Down
20 changes: 17 additions & 3 deletions tests/View/ViewFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,12 @@ public function testExistsPassesAndFailsViews()
$this->assertTrue($factory->exists('bar'));
}

public function testEitherCreatesNewViewInstanceWithProperPath()
public function testFirstCreatesNewViewInstanceWithProperPath()
{
unset($_SERVER['__test.view']);

$factory = $this->getFactory();
$factory->getFinder()->shouldReceive('find')->once()->with('view')->andReturn('path.php');
$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');
Expand All @@ -59,14 +59,28 @@ public function testEitherCreatesNewViewInstanceWithProperPath()
$_SERVER['__test.view'] = $view;
});
$factory->addExtension('php', 'php');
$view = $factory->either('bar', 'view', ['foo' => 'bar'], ['baz' => 'boom']);
$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('Illuminate\View\Factory[make]', $this->getFactoryArgs());
Expand Down

0 comments on commit f18318b

Please sign in to comment.