Last active
January 20, 2019 15:56
-
-
Save narthur/434c5d3de5ad9cdcddd4e732ad47bc3d to your computer and use it in GitHub Desktop.
[Twig wrapper object] #php
This file contains 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 Resume; | |
class Twig { | |
private $twig; | |
public function __construct() { | |
$loader = new \Twig_Loader_Filesystem([ | |
BASEDIR . "/twig" | |
]); | |
$this->twig = new \Twig_Environment($loader, array("debug" => true)); | |
$this->twig->addExtension(new \Twig_Extension_Debug()); | |
} | |
public function renderTemplate($template, $data) { | |
$template = $this->twig->load($template); | |
return $template->render(["data" => $data]); | |
} | |
} | |
class StubTwig extends Twig | |
{ | |
use Stub; | |
public function renderTemplate($template, $data) | |
{ | |
return $this->handleCall(__FUNCTION__, func_get_args()); | |
} | |
public function assertTemplateRendered($template) | |
{ | |
$this->assertAnyCallMatches("renderTemplate", function($carry, $call) use($template) { | |
$callTemplate = $call[0]; | |
return $carry || $template === $callTemplate; | |
}); | |
} | |
public function assertTemplateRenderedWithData($template, $data) | |
{ | |
$this->assertAnyCallMatches("renderTemplate", function($carry, $call) use($template, $data) { | |
$callTemplate = $call[0]; | |
$callData = $call[1]; | |
$doesTemplateMatch = $template === $callTemplate; | |
$doesIncludeData = empty($this->array_diff_assoc_recursive($data, $callData)); | |
return $carry || ( $doesTemplateMatch && $doesIncludeData ); | |
}); | |
} | |
private function array_diff_assoc_recursive($array1, $array2) { | |
// http://php.net/manual/en/function.array-diff-assoc.php#111675 | |
$difference=array(); | |
foreach($array1 as $key => $value) { | |
if( is_array($value) ) { | |
if( !isset($array2[$key]) || !is_array($array2[$key]) ) { | |
$difference[$key] = $value; | |
} else { | |
$new_diff = $this->array_diff_assoc_recursive($value, $array2[$key]); | |
if( !empty($new_diff) ) | |
$difference[$key] = $new_diff; | |
} | |
} else if( !array_key_exists($key,$array2) || $array2[$key] !== $value ) { | |
$difference[$key] = $value; | |
} | |
} | |
return $difference; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment