Passed
Push — master ( b166f8...dc6f85 )
by Gaetano
09:03 queued 51s
created

LoggerTest::debugMessage()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 3
rs 10
1
<?php
2
3
include_once __DIR__ . '/PolyfillTestCase.php';
4
5
use PhpXmlRpc\Helper\Charset;
6
use PhpXmlRpc\Helper\Http;
7
use PhpXmlRpc\Helper\XMLParser;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, XMLParser. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
8
9
class LoggerTest extends PhpXmlRpc_PolyfillTestCase
10
{
11
    protected $debugBuffer = '';
12
    protected $errorBuffer = '';
13
14
    protected function set_up()
15
    {
16
        $this->debugBuffer = '';
17
        $this->errorBuffer = '';
18
    }
19
20
    public function testCharsetAltLogger()
21
    {
22
        $ch = Charset::instance();
23
        $l = $ch->getLogger();
24
        Charset::setLogger($this);
25
26
        // silence the mbstring warning
27
        $ch->encodeEntities('hello world', 'UTF-8', 'NOT-A-CHARSET');
28
        $this->assertStringContainsString("via mbstring: failed", $this->errorBuffer);
29
30
        Charset::setLogger($l);
31
    }
32
33
    public function testHttpAltLogger()
34
    {
35
        $l = Http::getLogger();
36
        Http::setLogger($this);
37
38
        $h = new Http();
39
        $s = "HTTP/1.0 200 OK\r\n" .
40
            "Content-Type: unknown\r\n" .
41
            "\r\n" .
42
            "body";
43
        $h->parseResponseHeaders($s, false, 1);
44
        $this->assertStringContainsString("HEADER: content-type: unknown", $this->debugBuffer);
45
        Http::setLogger($l);
46
    }
47
48
    public function testXPAltLogger()
49
    {
50
        $xp = new XMLParser();
51
        $l = $xp->getLogger();
52
        XMLParser::setLogger($this);
53
54
        $xp->parse('<?xml version="1.0" ?><methodResponse><params><param><value><boolean> 1 </boolean></value></param></params></methodResponse>');
55
        $this->assertStringContainsString("invalid data received in BOOLEAN value", $this->errorBuffer);
56
57
        XMLParser::setLogger($l);
58
    }
59
60
    // logger API
61
62
    public function debugMessage($message, $encoding = null)
0 ignored issues
show
Unused Code introduced by
The parameter $encoding is not used and could be removed. ( Ignorable by Annotation )

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

62
    public function debugMessage($message, /** @scrutinizer ignore-unused */ $encoding = null)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
63
    {
64
        $this->debugBuffer .= $message;
65
    }
66
67
    public function errorLog($message)
68
    {
69
        $this->errorBuffer .= $message;
70
    }
71
}
72