Skip to content

Instantly share code, notes, and snippets.

@kaphert
Created September 14, 2018 23:04
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save kaphert/6845c03472b1d0c52ce5782219363cb5 to your computer and use it in GitHub Desktop.
Save kaphert/6845c03472b1d0c52ce5782219363cb5 to your computer and use it in GitHub Desktop.
Assert that blade view files are loaded with PHPUnit in Laravel
/**
* Laravel + PHPUnit assert that blade files are being loaded.
*
* Trait AssertView
*/
trait ViewAssertions
{
protected $__loadedViews;
protected function captureLoadedViews()
{
if (!isset($this->__loadedViews)) {
$this->__loadedViews = [];
$this->app['events']->listen('composing:*', function ($view, $data = []) {
if ($data) {
$view = $data[0]; // For Laravel >= 5.4
}
$this->__loadedViews[] = $view->getName();
}
);
}
}
/**
* Assert that all of the given views are loaded.
* - expectViewFiles('path.to.view')
* - expectViewFiles(['path.to.view', 'path.to.other.view'])
* - expectViewFiles('path.to.view', 'path.to.other.view')
*
* @param string|array $paths
*/
public function expectViewFiles($paths)
{
$paths = is_array($paths) ? $paths : func_get_args();
$this->captureLoadedViews();
$this->beforeApplicationDestroyed(function () use ($paths) {
$this->assertEmpty(
$viewsLoaded = array_diff($paths, $this->__loadedViews),
'These expected view files were not loaded: [' . implode(', ', $viewsLoaded) . ']'
);
});
}
/**
* Assert that none of the given views are loaded.
* - doesntExpectViewFiles('path.to.view')
* - doesntExpectViewFiles(['path.to.view', 'path.to.other.view'])
* - doesntExpectViewFiles('path.to.view', 'path.to.other.view')
*
* @param string|array $paths
*/
public function doesntExpectViewFiles($paths)
{
$paths = is_array($paths) ? $paths : func_get_args();
$this->captureLoadedViews();
$this->beforeApplicationDestroyed(function () use ($paths) {
$this->assertEmpty(
$viewsLoaded = array_intersect($this->__loadedViews, $paths),
'These unexpected view files were loaded: ['.implode(', ', $viewsLoaded).']'
);
});
}
}
@kaphert
Copy link
Author

kaphert commented Sep 14, 2018

Usage:

/** @test */
public function should_load_some_view()
{
    $this->expectViewFiles('path.to.view');
    $this->get('http://yoursite.test/some-page');
}

/** @test */
public function should_not_load_some_view()
{
    $this->doesntExpectViewFiles('path.to.view');
    $this->get('http://yoursite.test/some-page');
}

/** @test */
public function should_load_all_this_views()
{
    $this->expectViewFiles('path.to.view', 'path.to.other.view');
    // or: $this->expectViewFiles(['path.to.view', 'path.to.other.view']);
    $this->get('http://yoursite.test/some-page');
}

/** @test */
public function should_not_load_any_of_these_views()
{
    $this->doesntExpectViewFiles('path.to.view', 'path.to.other.view');
    // or: $this->doesntExpectViewFiles(['path.to.view', 'path.to.other.view']);
    $this->get('http://yoursite.test/some-page');
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment