daily pastebin goal
45%
SHARE
TWEET

Untitled

a guest Sep 14th, 2018 57 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.  * Laravel + PHPUnit assert that blade files are being loaded.
  3.  *
  4.  * Trait AssertView
  5.  */
  6. trait ViewAssertions
  7. {
  8.     protected $__loadedViews;
  9.  
  10.     protected function captureLoadedViews()
  11.     {
  12.         if (!isset($this->__loadedViews)) {
  13.             $this->__loadedViews = [];
  14.             $this->app['events']->listen('composing:*', function ($view, $data = []) {
  15.                 if ($data) {
  16.                     $view = $data[0]; // For Laravel >= 5.4
  17.                 }
  18.                 $this->__loadedViews[] = $view->getName();
  19.             }
  20.             );
  21.         }
  22.     }
  23.  
  24.     /**
  25.      * Assert that all of the given views are loaded.
  26.      *  - expectViewFiles('path.to.view')
  27.      *  - expectViewFiles(['path.to.view', 'path.to.other.view'])
  28.      *  - expectViewFiles('path.to.view', 'path.to.other.view')
  29.      *
  30.      * @param string|array $paths
  31.      */
  32.     public function expectViewFiles($paths)
  33.     {
  34.         $paths = is_array($paths) ? $paths : func_get_args();
  35.  
  36.         $this->captureLoadedViews();
  37.  
  38.         $this->beforeApplicationDestroyed(function () use ($paths) {
  39.             $this->assertEmpty(
  40.                 $viewsLoaded = array_diff($paths, $this->__loadedViews),
  41.                 'These expected view files were not loaded: [' . implode(', ', $viewsLoaded) . ']'
  42.             );
  43.         });
  44.     }
  45.  
  46.     /**
  47.      * Assert that none of the given views are loaded.
  48.      *  - doesntExpectViewFiles('path.to.view')
  49.      *  - doesntExpectViewFiles(['path.to.view', 'path.to.other.view'])
  50.      *  - doesntExpectViewFiles('path.to.view', 'path.to.other.view')
  51.      *
  52.      * @param string|array $paths
  53.      */
  54.     public function doesntExpectViewFiles($paths)
  55.     {
  56.         $paths = is_array($paths) ? $paths : func_get_args();
  57.  
  58.         $this->captureLoadedViews();
  59.  
  60.         $this->beforeApplicationDestroyed(function () use ($paths) {
  61.             $this->assertEmpty(
  62.                 $viewsLoaded = array_intersect($this->__loadedViews, $paths),
  63.                 'These unexpected view files were loaded: ['.implode(', ', $viewsLoaded).']'
  64.             );
  65.         });
  66.     }
  67. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top