Created
November 19, 2018 18:37
-
-
Save roni-estein/3461a79094716cddb9d351624475c978 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Tests; | |
/** | |
* Laravel + PHPUnit assert that blade files are being loaded. | |
* | |
* Trait AssertView | |
*/ | |
trait Viewable | |
{ | |
protected $__loadedViews; | |
/** | |
* Will capture a list of loaded views based on the composing event | |
*/ | |
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).']' | |
); | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment