Use stubs for profiles. Fixes #1660

This commit is contained in:
Matthieu Auger
2015-03-30 00:37:37 +02:00
parent 67920147a8
commit f6bce94d52
@@ -13,32 +13,88 @@ abstract class Twig_Tests_Profiler_Dumper_AbstractTest extends PHPUnit_Framework
{
protected function getProfile()
{
$profile = new Twig_Profiler_Profile();
$index = new Twig_Profiler_Profile('index.twig', Twig_Profiler_Profile::TEMPLATE);
$profile->addProfile($index);
$body = new Twig_Profiler_Profile('embedded.twig', Twig_Profiler_Profile::BLOCK, 'body');
$body->leave();
$index->addProfile($body);
$embedded = new Twig_Profiler_Profile('embedded.twig', Twig_Profiler_Profile::TEMPLATE);
$included = new Twig_Profiler_Profile('included.twig', Twig_Profiler_Profile::TEMPLATE);
$embedded->addProfile($included);
$index->addProfile($embedded);
$included->leave();
$embedded->leave();
$profile = $this->getMockBuilder('Twig_Profiler_Profile')->disableOriginalConstructor()->getMock();
$macro = new Twig_Profiler_Profile('index.twig', Twig_Profiler_Profile::MACRO, 'foo');
$macro->leave();
$index->addProfile($macro);
$profile->expects($this->any())->method('isRoot')->will($this->returnValue(true));
$profile->expects($this->any())->method('getName')->will($this->returnValue('main'));
$profile->expects($this->any())->method('getDuration')->will($this->returnValue(1));
$profile->expects($this->any())->method('getMemoryUsage')->will($this->returnValue(0));
$profile->expects($this->any())->method('getPeakMemoryUsage')->will($this->returnValue(0));
$embedded = clone $embedded;
$index->addProfile($embedded);
usleep(500);
$embedded->leave();
$subProfiles = array(
$this->getIndexProfile(
array(
$this->getEmbeddedBlockProfile(),
$this->getEmbeddedTemplateProfile(
array(
$this->getIncludedTemplateProfile(),
)
),
$this->getMacroProfile(),
$this->getEmbeddedTemplateProfile(
array(
$this->getIncludedTemplateProfile(),
)
),
)
),
);
usleep(4500);
$index->leave();
$profile->expects($this->any())->method('getProfiles')->will($this->returnValue($subProfiles));
$profile->expects($this->any())->method('getIterator')->will($this->returnValue(new ArrayIterator($subProfiles)));
$profile->leave();
return $profile;
}
private function getIndexProfile(array $subProfiles = array())
{
return $this->generateProfile('main', 1, true, 'template', 'index.twig', $subProfiles);
}
private function getEmbeddedBlockProfile(array $subProfiles = array())
{
return $this->generateProfile('body', 0.0001, false, 'block', 'embedded.twig', $subProfiles);
}
private function getEmbeddedTemplateProfile(array $subProfiles = array())
{
return $this->generateProfile('main', 0.0001, true, 'template', 'embedded.twig', $subProfiles);
}
private function getIncludedTemplateProfile(array $subProfiles = array())
{
return $this->generateProfile('main', 0.0001, true, 'template', 'included.twig', $subProfiles);
}
private function getMacroProfile(array $subProfiles = array())
{
return $this->generateProfile('foo', 0.0001, false, 'macro', 'index.twig', $subProfiles);
}
/**
* @param string $name
* @param float $duration
* @param bool $isTemplate
* @param string $type
* @param string $templateName
* @param array $subProfiles
*
* @return Twig_Profiler_Profile
*/
private function generateProfile($name, $duration, $isTemplate, $type, $templateName, array $subProfiles = array())
{
$profile = $this->getMockBuilder('Twig_Profiler_Profile')->disableOriginalConstructor()->getMock();
$profile->expects($this->any())->method('isRoot')->will($this->returnValue(false));
$profile->expects($this->any())->method('getName')->will($this->returnValue($name));
$profile->expects($this->any())->method('getDuration')->will($this->returnValue($duration));
$profile->expects($this->any())->method('getMemoryUsage')->will($this->returnValue(0));
$profile->expects($this->any())->method('getPeakMemoryUsage')->will($this->returnValue(0));
$profile->expects($this->any())->method('isTemplate')->will($this->returnValue($isTemplate));
$profile->expects($this->any())->method('getType')->will($this->returnValue($type));
$profile->expects($this->any())->method('getTemplate')->will($this->returnValue($templateName));
$profile->expects($this->any())->method('getProfiles')->will($this->returnValue($subProfiles));
$profile->expects($this->any())->method('getIterator')->will($this->returnValue(new ArrayIterator($subProfiles)));
return $profile;
}