Skip to content

Instantly share code, notes, and snippets.

@mercuryseries
Created August 7, 2016 03:42
Show Gist options
  • Save mercuryseries/75d81bba77e07de66598c0ceae467421 to your computer and use it in GitHub Desktop.
Save mercuryseries/75d81bba77e07de66598c0ceae467421 to your computer and use it in GitHub Desktop.
Some View Assertions Utils for the Laravel Framework
<?php
trait ViewAssertionsUtils
{
/**
* Tests to see whether the view template provided is the one
* used in the rendered view.
*
* @param string
* @return \Illuminate\Foundation\Testing\TestCase
*/
protected function assertViewIs($view_name)
{
$this->assertEquals($view_name, $this->response->original->getName());
return $this;
}
/**
* Tests to see whether the view template provided is the one
* used in the rendered view.
*
* @param string
* @return \Illuminate\Foundation\Testing\TestCase
*/
protected function assertTemplate($view_name)
{
return $this->assertViewIs($view_name);
}
/**
* Tests to see whether the view template provided is the one
* used in the rendered view.
*
* @param string
* @return \Illuminate\Foundation\Testing\TestCase
*/
protected function assertTemplateUsedIs($view_name)
{
return $this->assertViewIs($view_name);
}
/**
* Tests to see whether the view template provided is the one
* used in the rendered view.
*
* @param string
* @return \Illuminate\Foundation\Testing\TestCase
*/
protected function assertTemplateIs($view_name)
{
return $this->assertViewIs($view_name);
}
/**
* Tests to see whether the view template provided contains
* a link with an href matching the given one.
*
* @param string
* @return \Illuminate\Foundation\Testing\TestCase
*/
protected function assertLink($link)
{
$this->assertGreaterThan(0, $this->crawler()->filter('a[href="'. $link .'"]')->count());
return $this;
}
/**
* Tests to see whether the view template provided do not contains
* a link with an href matching the given one.
*
* @param string
* @return \Illuminate\Foundation\Testing\TestCase
*/
protected function assertNotLink($link)
{
$this->assertEquals(0, $this->crawler()->filter('a[href="'. $link .'"]')->count());
return $this;
}
/**
* Tests to see whether the view template provided contains
* {$count} times a link with an href matching the given one.
*
* @param string
* @param int
* @return \Illuminate\Foundation\Testing\TestCase
*/
protected function assertLinkCount($link, $count)
{
$this->assertEquals($count, $this->crawler()->filter('a[href="'. $link .'"]')->count());
return $this;
}
/**
* Tests to see whether the view template provided do not contains
* {$count} times a link with an href matching the given one.
*
* @param string
* @param int
* @return \Illuminate\Foundation\Testing\TestCase
*/
protected function assertNotLinkCount($link, $count)
{
$this->assertNotEquals($count, $this->crawler()->filter('a[href="'. $link .'"]')->count());
return $this;
}
/**
* Tests to see whether the view template provided contains
* more than {$count} times a link with an href matching the given one.
*
* @param string
* @param int
* @return \Illuminate\Foundation\Testing\TestCase
*/
protected function assertLinkCountGreaterThan($link, $count)
{
$this->assertGreaterThan($count, $this->crawler()->filter('a[href="'. $link .'"]')->count());
return $this;
}
/**
* Tests to see whether the view template provided contains
* at least {$count} times a link with an href matching the given one.
*
* @param string
* @param int
* @return \Illuminate\Foundation\Testing\TestCase
*/
protected function assertLinkCountGreaterThanOrEqual($link, $count)
{
$this->assertGreaterThanOrEqual($count, $this->crawler()->filter('a[href="'. $link .'"]')->count());
return $this;
}
/**
* Tests to see whether the view template provided contains
* less than {$count} times a link with an href matching the given one.
*
* @param string
* @param int
* @return \Illuminate\Foundation\Testing\TestCase
*/
protected function assertLinkCountLessThan($link, $count)
{
$this->assertLessThan($count, $this->crawler()->filter('a[href="'. $link .'"]')->count());
return $this;
}
/**
* Tests to see whether the view template provided contains
* at most {$count} times a link with an href matching the given one.
*
* @param string
* @param int
* @return \Illuminate\Foundation\Testing\TestCase
*/
protected function assertLinkCountLessThanOrEqual($link, $count)
{
$this->assertLessThanOrEqual($count, $this->crawler()->filter('a[href="'. $link .'"]')->count());
return $this;
}
/**
* Tests to see whether the view template provided contains
* a link generated with the provided route name.
*
* @param string
* @return \Illuminate\Foundation\Testing\TestCase
*/
protected function assertLinkRoute($route_name)
{
return $this->assertLink(route($route_name));
}
/**
* Tests to see whether the view template provided do not contains
* a link generated with the provided route name.
*
* @param string
* @return \Illuminate\Foundation\Testing\TestCase
*/
protected function assertNotLinkRoute($route_name)
{
return $this->assertNotLink(route($route_name));
}
/**
* Tests to see whether the view template provided contains
* {$count} times a link generated with the provided route name.
*
* @param string
* @param int
* @return \Illuminate\Foundation\Testing\TestCase
*/
protected function assertLinkRouteCount($route_name, $count)
{
return $this->assertLinkCount(route($route_name), $count);
}
/**
* Tests to see whether the view template provided do not contains
* {$count} times a link generated with the provided route name.
*
* @param string
* @param int
* @return \Illuminate\Foundation\Testing\TestCase
*/
protected function assertNotLinkRouteCount($route_name, $count)
{
return $this->assertNotLinkCount(route($route_name), $count);
}
/**
* Tests to see whether the view template provided contains
* more than {$count} times a link generated with the provided route name.
*
* @param string
* @param int
* @return \Illuminate\Foundation\Testing\TestCase
*/
protected function assertLinkRouteCountGreaterThan($route_name, $count)
{
return $this->assertLinkCountGreaterThan(route($route_name), $count);
}
/**
* Tests to see whether the view template provided contains
* at least {$count} times a link generated with the provided route name.
*
* @param string
* @param int
* @return \Illuminate\Foundation\Testing\TestCase
*/
protected function assertLinkRouteCountGreaterThanOrEqual($route_name, $count)
{
return $this->assertLinkCountGreaterThanOrEqual(route($route_name), $count);
}
/**
* Tests to see whether the view template provided contains
* less than {$count} times a link generated with the provided route name.
*
* @param string
* @param int
* @return \Illuminate\Foundation\Testing\TestCase
*/
protected function assertLinkRouteCountLessThan($route_name, $count)
{
return $this->assertLinkCountLessThan(route($route_name), $count);
}
/**
* Tests to see whether the view template provided contains
* at most {$count} times a link generated with the provided route name.
*
* @param string
* @param int
* @return \Illuminate\Foundation\Testing\TestCase
*/
protected function assertLinkRouteCountLessThanOrEqual($route_name, $count)
{
return $this->assertLinkCountGreaterThanOrEqual(route($route_name), $count);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment