Skip to content

Commit

Permalink
Added constraints for template/layout files
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremyharris committed May 10, 2018
1 parent 4975dfc commit 9869e5f
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 8 deletions.
31 changes: 31 additions & 0 deletions src/TestSuite/Constraint/View/LayoutFileEquals.php
@@ -0,0 +1,31 @@
<?php
/**
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @since 3.7.0
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
namespace Cake\TestSuite\Constraint\View;

/**
* LayoutFileEquals
*/
class LayoutFileEquals extends TemplateFileEquals
{

/**
* Assertion message
*
* @return string
*/
public function toString()
{
return sprintf('equals layout file `%s`', $this->filename);
}
}
61 changes: 61 additions & 0 deletions src/TestSuite/Constraint/View/TemplateFileEquals.php
@@ -0,0 +1,61 @@
<?php
/**
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @since 3.7.0
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
namespace Cake\TestSuite\Constraint\View;

use PHPUnit\Framework\Constraint\Constraint;

/**
* TemplateFileEquals
*/
class TemplateFileEquals extends Constraint
{

/**
* @var string
*/
protected $filename;

/**
* Constructor
*
* @param string $filename Template file name
*/
public function __construct($filename)
{
parent::__construct();

$this->filename = $filename;
}

/**
* Checks assertion
*
* @param mixed $other Expected filename
* @return bool
*/
public function matches($other)
{
return strpos($this->filename, $other) !== false;
}

/**
* Assertion message
*
* @return string
*/
public function toString()
{
return sprintf('equals template file `%s`', $this->filename);
}
}
12 changes: 4 additions & 8 deletions src/TestSuite/IntegrationTestTrait.php
Expand Up @@ -26,6 +26,8 @@
use Cake\TestSuite\Constraint\Response\StatusFailure;
use Cake\TestSuite\Constraint\Response\StatusOk;
use Cake\TestSuite\Constraint\Response\StatusSuccess;
use Cake\TestSuite\Constraint\View\TemplateFileEquals;
use Cake\TestSuite\Constraint\View\LayoutFileEquals;
use Cake\TestSuite\Stub\TestExceptionRenderer;
use Cake\Utility\CookieCryptTrait;
use Cake\Utility\Hash;
Expand Down Expand Up @@ -975,10 +977,7 @@ public function assertResponseEmpty($message = '')
*/
public function assertTemplate($content, $message = '')
{
if (!$this->_viewName) {
$this->fail('No view name stored. ' . $message);
}
$this->assertContains($content, $this->_viewName, $message);
$this->assertThat($content, new TemplateFileEquals($this->_viewName), $message);
}

/**
Expand All @@ -990,10 +989,7 @@ public function assertTemplate($content, $message = '')
*/
public function assertLayout($content, $message = '')
{
if (!$this->_layoutName) {
$this->fail('No layout name stored. ' . $message);
}
$this->assertContains($content, $this->_layoutName, $message);
$this->assertThat($content, new LayoutFileEquals($this->_layoutName), $message);
}

/**
Expand Down

0 comments on commit 9869e5f

Please sign in to comment.