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
14class Twig_Tests_Profiler_ProfileTest extends \PHPUnit\Framework\TestCase
15{
16    public function testConstructor()
17    {
18        $profile = new Profile('template', 'type', 'name');
19
20        $this->assertEquals('template', $profile->getTemplate());
21        $this->assertEquals('type', $profile->getType());
22        $this->assertEquals('name', $profile->getName());
23    }
24
25    public function testIsRoot()
26    {
27        $profile = new Profile('template', Profile::ROOT);
28        $this->assertTrue($profile->isRoot());
29
30        $profile = new Profile('template', Profile::TEMPLATE);
31        $this->assertFalse($profile->isRoot());
32    }
33
34    public function testIsTemplate()
35    {
36        $profile = new Profile('template', Profile::TEMPLATE);
37        $this->assertTrue($profile->isTemplate());
38
39        $profile = new Profile('template', Profile::ROOT);
40        $this->assertFalse($profile->isTemplate());
41    }
42
43    public function testIsBlock()
44    {
45        $profile = new Profile('template', Profile::BLOCK);
46        $this->assertTrue($profile->isBlock());
47
48        $profile = new Profile('template', Profile::ROOT);
49        $this->assertFalse($profile->isBlock());
50    }
51
52    public function testIsMacro()
53    {
54        $profile = new Profile('template', Profile::MACRO);
55        $this->assertTrue($profile->isMacro());
56
57        $profile = new Profile('template', Profile::ROOT);
58        $this->assertFalse($profile->isMacro());
59    }
60
61    public function testGetAddProfile()
62    {
63        $profile = new Profile();
64        $profile->addProfile($a = new Profile());
65        $profile->addProfile($b = new Profile());
66
67        $this->assertSame([$a, $b], $profile->getProfiles());
68        $this->assertSame([$a, $b], iterator_to_array($profile));
69    }
70
71    public function testGetDuration()
72    {
73        $profile = new Profile();
74        usleep(1);
75        $profile->leave();
76
77        $this->assertTrue($profile->getDuration() > 0, sprintf('Expected duration > 0, got: %f', $profile->getDuration()));
78    }
79
80    public function testSerialize()
81    {
82        $profile = new Profile('template', 'type', 'name');
83        $profile1 = new Profile('template1', 'type1', 'name1');
84        $profile->addProfile($profile1);
85        $profile->leave();
86        $profile1->leave();
87
88        $profile2 = unserialize(serialize($profile));
89        $profiles = $profile->getProfiles();
90        $this->assertCount(1, $profiles);
91        $profile3 = $profiles[0];
92
93        $this->assertEquals($profile->getTemplate(), $profile2->getTemplate());
94        $this->assertEquals($profile->getType(), $profile2->getType());
95        $this->assertEquals($profile->getName(), $profile2->getName());
96        $this->assertEquals($profile->getDuration(), $profile2->getDuration());
97
98        $this->assertEquals($profile1->getTemplate(), $profile3->getTemplate());
99        $this->assertEquals($profile1->getType(), $profile3->getType());
100        $this->assertEquals($profile1->getName(), $profile3->getName());
101    }
102
103    public function testReset()
104    {
105        $profile = new Profile();
106        usleep(1);
107        $profile->leave();
108        $profile->reset();
109
110        $this->assertEquals(0, $profile->getDuration());
111    }
112}
113