1<?php
2
3/*
4 * This file is part of Twig.
5 *
6 * (c) Fabien Potencier
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
12use Twig\Profiler\Profile;
13
14abstract class Twig_Tests_Profiler_Dumper_AbstractTest extends \PHPUnit\Framework\TestCase
15{
16    protected function getProfile()
17    {
18        $profile = new Profile('main');
19        $subProfiles = [
20            $this->getIndexProfile(
21                [
22                    $this->getEmbeddedBlockProfile(),
23                    $this->getEmbeddedTemplateProfile(
24                        [
25                            $this->getIncludedTemplateProfile(),
26                        ]
27                    ),
28                    $this->getMacroProfile(),
29                    $this->getEmbeddedTemplateProfile(
30                        [
31                            $this->getIncludedTemplateProfile(),
32                        ]
33                    ),
34                ]
35            ),
36        ];
37
38        $p = new \ReflectionProperty($profile, 'profiles');
39        $p->setAccessible(true);
40        $p->setValue($profile, $subProfiles);
41
42        return $profile;
43    }
44
45    private function getIndexProfile(array $subProfiles = [])
46    {
47        return $this->generateProfile('main', 1, 'template', 'index.twig', $subProfiles);
48    }
49
50    private function getEmbeddedBlockProfile(array $subProfiles = [])
51    {
52        return $this->generateProfile('body', 0.0001, 'block', 'embedded.twig', $subProfiles);
53    }
54
55    private function getEmbeddedTemplateProfile(array $subProfiles = [])
56    {
57        return $this->generateProfile('main', 0.0001, 'template', 'embedded.twig', $subProfiles);
58    }
59
60    private function getIncludedTemplateProfile(array $subProfiles = [])
61    {
62        return $this->generateProfile('main', 0.0001, 'template', 'included.twig', $subProfiles);
63    }
64
65    private function getMacroProfile(array $subProfiles = [])
66    {
67        return $this->generateProfile('foo', 0.0001, 'macro', 'index.twig', $subProfiles);
68    }
69
70    /**
71     * @param string $name
72     * @param float  $duration
73     * @param bool   $isTemplate
74     * @param string $type
75     * @param string $templateName
76     * @param array  $subProfiles
77     *
78     * @return Profile
79     */
80    private function generateProfile($name, $duration, $type, $templateName, array $subProfiles = [])
81    {
82        $profile = new Profile($templateName, $type, $name);
83
84        $p = new \ReflectionProperty($profile, 'profiles');
85        $p->setAccessible(true);
86        $p->setValue($profile, $subProfiles);
87
88        $starts = new \ReflectionProperty($profile, 'starts');
89        $starts->setAccessible(true);
90        $starts->setValue($profile, [
91            'wt' => 0,
92            'mu' => 0,
93            'pmu' => 0,
94        ]);
95        $ends = new \ReflectionProperty($profile, 'ends');
96        $ends->setAccessible(true);
97        $ends->setValue($profile, [
98            'wt' => $duration,
99            'mu' => 0,
100            'pmu' => 0,
101        ]);
102
103        return $profile;
104    }
105}
106