File: ProfileTest.php

package info (click to toggle)
php-twig 3.20.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 5,940 kB
  • sloc: php: 23,320; makefile: 110; sh: 43
file content (124 lines) | stat: -rw-r--r-- 3,712 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
<?php

namespace Twig\Tests\Profiler;

/*
 * This file is part of Twig.
 *
 * (c) Fabien Potencier
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

use PHPUnit\Framework\TestCase;
use Twig\Profiler\Profile;

class ProfileTest extends TestCase
{
    public function testConstructor()
    {
        $profile = new Profile('template', 'type', 'name');

        $this->assertEquals('template', $profile->getTemplate());
        $this->assertEquals('type', $profile->getType());
        $this->assertEquals('name', $profile->getName());
    }

    public function testIsRoot()
    {
        $profile = new Profile('template', Profile::ROOT);
        $this->assertTrue($profile->isRoot());

        $profile = new Profile('template', Profile::TEMPLATE);
        $this->assertFalse($profile->isRoot());
    }

    public function testIsTemplate()
    {
        $profile = new Profile('template', Profile::TEMPLATE);
        $this->assertTrue($profile->isTemplate());

        $profile = new Profile('template', Profile::ROOT);
        $this->assertFalse($profile->isTemplate());
    }

    public function testIsBlock()
    {
        $profile = new Profile('template', Profile::BLOCK);
        $this->assertTrue($profile->isBlock());

        $profile = new Profile('template', Profile::ROOT);
        $this->assertFalse($profile->isBlock());
    }

    public function testIsMacro()
    {
        $profile = new Profile('template', Profile::MACRO);
        $this->assertTrue($profile->isMacro());

        $profile = new Profile('template', Profile::ROOT);
        $this->assertFalse($profile->isMacro());
    }

    public function testGetAddProfile()
    {
        $profile = new Profile();
        $profile->addProfile($a = new Profile());
        $profile->addProfile($b = new Profile());

        $this->assertSame([$a, $b], $profile->getProfiles());
        $this->assertSame([$a, $b], iterator_to_array($profile));
    }

    public function testGetDuration()
    {
        $profile = new Profile();
        usleep(1);
        $profile->leave();

        $this->assertTrue($profile->getDuration() > 0, \sprintf('Expected duration > 0, got: %f', $profile->getDuration()));
    }

    public function testTimeAccessors()
    {
        $current = microtime(true);
        $profile = new Profile();

        $this->assertEqualsWithDelta($current, $profile->getStartTime(), 1);
        $this->assertSame(0.0, $profile->getEndTime());
    }

    public function testSerialize()
    {
        $profile = new Profile('template', 'type', 'name');
        $profile1 = new Profile('template1', 'type1', 'name1');
        $profile->addProfile($profile1);
        $profile->leave();
        $profile1->leave();

        $profile2 = unserialize(serialize($profile));
        $profiles = $profile->getProfiles();
        $this->assertCount(1, $profiles);
        $profile3 = $profiles[0];

        $this->assertEquals($profile->getTemplate(), $profile2->getTemplate());
        $this->assertEquals($profile->getType(), $profile2->getType());
        $this->assertEquals($profile->getName(), $profile2->getName());
        $this->assertEquals($profile->getDuration(), $profile2->getDuration());

        $this->assertEquals($profile1->getTemplate(), $profile3->getTemplate());
        $this->assertEquals($profile1->getType(), $profile3->getType());
        $this->assertEquals($profile1->getName(), $profile3->getName());
    }

    public function testReset()
    {
        $profile = new Profile();
        usleep(1);
        $profile->leave();
        $profile->reset();

        $this->assertEquals(0, $profile->getDuration());
    }
}