Test Setup Failed
Branch master (c7aea0)
by Alex
04:14 queued 01:25
created

FunctionalTest::getProfiler()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 0
dl 0
loc 9
rs 9.9666
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
class FunctionalTest extends TestCase
12
{
13
    /**
14
     * @var KernelInterface
15
     */
16
    //private $kernel;
17
18
    /**
19
     * @var Profiler
20
     */
21
    private $profiler;
22
23
    protected function setUp()
24
    {
25
        /* TODO: There are more tests we could run with a kernel present *
26
        $this->kernel = new AppKernel('test', true);
27
        $this->kernel->boot();
28
        /* */
29
        //$this->kernel->getContainer()->get('yaml.test');
30
    }
31
32
    protected function tearDown()
33
    {
34
        /* *
35
        $this->kernel->shutdown();
36
        /* */
37
    }
38
39
    /**
40
     * @param int $length
41
     *
42
     * @return string
43
     */
44
    protected function makeToken($length = 8)
45
    {
46
        $characters = 'abcdefghijklmnopqrstuvwxyz0123456789';
47
        $string = '';
48
        $max = strlen($characters) - 1;
49
        for ($i = 0; $i < $length; ++$i) {
50
            $string .= $characters[mt_rand(0, $max)];
51
        }
52
53
        return $string;
54
    }
55
56
    /**
57
     * @return MongoDbProfilerStorage|Profiler
58
     */
59
    public function getProfiler()
60
    {
61
        if (null !== $this->profiler) {
62
            return $this->profiler;
63
        }
64
65
        $profiler = new MongoDbProfilerStorage('mongodb://127.0.0.1/test/profiler');
66
67
        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...
68
    }
69
70
    public function testConnect()
71
    {
72
        $this->assertNotEmpty($this->getProfiler());
73
    }
74
75
    public function testPurge()
76
    {
77
        $this->assertNotEmpty($this->getProfiler()->purge());
78
    }
79
80
    public function testReadWrite()
81
    {
82
        // Store Token
83
        $token = $this->makeToken();
84
85
        // Run Profile
86
        $profile = new Profile($token);
87
        $profile->setTime(time());
88
        $profile->setIp('127.0.0.1');
89
        $profile->setUrl('http://127.0.0.1/');
90
        $profile->setMethod('GET');
91
        $profile->setStatusCode(500);
92
93
        // Write
94
        $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

94
        $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...
95
96
        // Read
97
        $profiles = $this->getProfiler()->find(
98
            $profile->getIp(),
99
            $profile->getUrl(),
100
            20,
101
            $profile->getMethod(),
102
            null,
103
            null,
104
            $profile->getStatusCode()
105
        );
106
107
        // Ensure Profiles Exist
108
        $this->assertNotEmpty($profiles);
109
    }
110
}
111