FunctionalTest::getProfiler()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 0
dl 0
loc 9
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Sitetheory\Bundle\ProfilerStorageBundle\Tests\Functional;
4
5
use PHPUnit\Framework\TestCase;
6
use Sitetheory\Bundle\ProfilerStorageBundle\Profiler\MongoDbProfilerStorage;
7
use Sitetheory\Bundle\ProfilerStorageBundle\Profiler\Profiler;
8
use Symfony\Component\HttpKernel\KernelInterface;
9
use Symfony\Component\HttpKernel\Profiler\Profile;
10
11
/**
12
 * @internal
13
 * @coversNothing
14
 */
15
class FunctionalTest extends TestCase
16
{
17
    /**
18
     * @var KernelInterface
19
     */
20
    //private $kernel;
21
22
    /**
23
     * @var Profiler
24
     */
25
    private $profiler;
26
27
    protected function setUp()
28
    {
29
        /* TODO: There are more tests we could run with a kernel present *
30
        $this->kernel = new AppKernel('test', true);
31
        $this->kernel->boot();
32
        /* */
33
        //$this->kernel->getContainer()->get('yaml.test');
34
    }
35
36
    protected function tearDown()
37
    {
38
        /* *
39
        $this->kernel->shutdown();
40
        /* */
41
    }
42
43
    /**
44
     * @param int $length
45
     *
46
     * @return string
47
     */
48
    protected function makeToken($length = 8)
49
    {
50
        $characters = 'abcdefghijklmnopqrstuvwxyz0123456789';
51
        $string = '';
52
        $max = strlen($characters) - 1;
53
        for ($i = 0; $i < $length; ++$i) {
54
            $string .= $characters[mt_rand(0, $max)];
55
        }
56
57
        return $string;
58
    }
59
60
    /**
61
     * @return MongoDbProfilerStorage|Profiler
62
     */
63
    public function getProfiler()
64
    {
65
        if (null !== $this->profiler) {
66
            return $this->profiler;
67
        }
68
69
        $profiler = new MongoDbProfilerStorage('mongodb://127.0.0.1/test/profiler');
70
71
        return $this->profiler = $profiler;
0 ignored issues
show
Documentation Bug introduced by
It seems like $profiler of type Sitetheory\Bundle\Profil...\MongoDbProfilerStorage is incompatible with the declared type Sitetheory\Bundle\Profil...undle\Profiler\Profiler of property $profiler.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
72
    }
73
74
    public function testConnect()
75
    {
76
        $this->assertNotEmpty($this->getProfiler());
77
    }
78
79
    public function testPurge()
80
    {
81
        $this->assertNotEmpty($this->getProfiler()->purge());
82
    }
83
84
    public function testReadWrite()
85
    {
86
        // Store Token
87
        $token = $this->makeToken();
88
89
        // Run Profile
90
        $profile = new Profile($token);
91
        $profile->setTime(time());
92
        $profile->setIp('127.0.0.1');
93
        $profile->setUrl('http://127.0.0.1/');
94
        $profile->setMethod('GET');
95
        $profile->setStatusCode(500);
96
97
        // Write
98
        $this->assertTrue($this->getProfiler()->write($profile), 'Write Profile');
0 ignored issues
show
Bug introduced by
The method write() does not exist on Sitetheory\Bundle\Profil...undle\Profiler\Profiler. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

98
        $this->assertTrue($this->getProfiler()->/** @scrutinizer ignore-call */ write($profile), 'Write Profile');

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
99
100
        // Read
101
        $profiles = $this->getProfiler()->find(
102
            $profile->getIp(),
103
            $profile->getUrl(),
104
            20,
105
            $profile->getMethod(),
106
            null,
107
            null,
108
            $profile->getStatusCode()
109
        );
110
111
        // Ensure Profiles Exist
112
        $this->assertNotEmpty($profiles);
113
    }
114
}
115