If you lookup the class you mention here you see that it has a find() method where you can search for the view name. I think you can use that method.
Sep 14, 2018
10
Level 33
Test if a view partial is loaded in a phpunit test
Is there a way to access a segment of a View response object? I'm trying to test if a view partial has been loaded in a less brittle way than just using see.
when I dd the response object I can find the view list
#finder: Illuminate\View\FileViewFinder {#120
#files: Illuminate\Filesystem\Filesystem {#121}
#paths: array:1 [
0 => "/Users/roni/code/php/laravel/5.7/munroe/resources/views"
]
#views: array:13 [
"static.new-front" => "/Users/roni/code/php/laravel/5.7/munroe/resources/views/static/new-front.blade.php"
"segments.front.v2.events.upcomming-conferences" => "/Users/roni/code/php/laravel/5.7/munroe/resources/views/segments/front/v2/events/upcomming-conferences.blade.php"
"segments.front.v2.weight-management" => "/Users/roni/code/php/laravel/5.7/munroe/resources/views/segments/front/v2/weight-management.blade.php"
"segments.front.v2.notifications.sale-free-registration" => "/Users/roni/code/php/laravel/5.7/munroe/resources/views/segments/front/v2/notifications/sale-free-registration.php"
"segments.front.v2.compression-therapy" => "/Users/roni/code/php/laravel/5.7/munroe/resources/views/segments/front/v2/compression-therapy.blade.php"
"layouts.new-front-with-nav" => "/Users/roni/code/php/laravel/5.7/munroe/resources/views/layouts/new-front-with-nav.blade.php"
"segments.front.v2.navbar" => "/Users/roni/code/php/laravel/5.7/munroe/resources/views/segments/front/v2/navbar.blade.php"
"segments.front.v2.reviews.div" => "/Users/roni/code/php/laravel/5.7/munroe/resources/views/segments/front/v2/reviews/div.blade.php"
"segments.front.v2.newsletter" => "/Users/roni/code/php/laravel/5.7/munroe/resources/views/segments/front/v2/newsletter.blade.php"
"segments.front.map" => "/Users/roni/code/php/laravel/5.7/munroe/resources/views/segments/front/map.blade.php"
"segments.front.v2.footer" => "/Users/roni/code/php/laravel/5.7/munroe/resources/views/segments/front/v2/footer.blade.php"
"segments.scripts.front.v2.navbar" => "/Users/roni/code/php/laravel/5.7/munroe/resources/views/segments/scripts/front/v2/navbar.blade.php"
"segments.scripts.front.v2.newsletter" => "/Users/roni/code/php/laravel/5.7/munroe/resources/views/segments/scripts/front/v2/newsletter.blade.php"
]
#hints: array:2 [
"notifications" => array:1 [
0 => "/Users/roni/code/php/laravel/5.7/munroe/vendor/laravel/framework/src/Illuminate/Notifications/resources/views"
]
"pagination" => array:1 [
0 => "/Users/roni/code/php/laravel/5.7/munroe/vendor/laravel/framework/src/Illuminate/Pagination/resources/views"
]
]
but I can't figure out how to isolate that portion of the view object and test against it. Any advice is welcome.
Level 35
@Roni ah shit, shame on me. But I think I've got a solution now:
Update You know what, here a gist for the trait: https://gist.github.com/kaphert/6845c03472b1d0c52ce5782219363cb5
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');
}
Trait file: ViewAssertions.php
/**
* 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).']'
);
});
}
}
3 likes
Please or to participate in this conversation.