Passed
Push — master ( 9f5262...c72dd6 )
by Gaetano
09:22
created

LoggerTest::testCharsetAltLogger()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 9
c 2
b 0
f 0
nc 1
nop 0
dl 0
loc 13
rs 9.9666
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
        ob_start();
27
        $ch->encodeEntities('hello world', 'UTF-8', 'NOT-A-CHARSET');
28
        $o = ob_get_clean();
29
        $this->assertEquals('', $o);
30
        $this->assertStringContainsString("via mbstring: failed", $this->errorBuffer);
31
32
        Charset::setLogger($l);
33
    }
34
35
    public function testHttpAltLogger()
36
    {
37
        $h = new Http();
38
        $l = $h->getLogger();
39
        Http::setLogger($this);
40
41
        $s = "HTTP/1.0 200 OK\r\n" .
42
            "Content-Type: unknown\r\n" .
43
            "\r\n" .
44
            "body";
45
        ob_start();
46
        $h->parseResponseHeaders($s, false, 1);
47
        $o = ob_get_clean();
48
        $this->assertEquals('', $o);
49
        $this->assertStringContainsString("HEADER: content-type: unknown", $this->debugBuffer);
50
        Http::setLogger($l);
51
    }
52
53
    public function testXPAltLogger()
54
    {
55
        $xp = new XMLParser();
56
        $l = $xp->getLogger();
57
        XMLParser::setLogger($this);
58
59
        ob_start();
60
        $xp->parse('<?xml version="1.0" ?><methodResponse><params><param><value><boolean>x</boolean></value></param></params></methodResponse>');
61
        $o = ob_get_clean();
62
        $this->assertEquals('', $o);
63
        $this->assertStringContainsString("invalid data received in BOOLEAN value", $this->errorBuffer);
64
65
        XMLParser::setLogger($l);
66
    }
67
68
    // logger API
69
70
    public function debug($message, $context = array())
0 ignored issues
show
Unused Code introduced by
The parameter $context 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

70
    public function debug($message, /** @scrutinizer ignore-unused */ $context = array())

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...
71
    {
72
        $this->debugBuffer .= $message;
73
    }
74
75
    public function error($message, $context = array())
0 ignored issues
show
Unused Code introduced by
The parameter $context 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

75
    public function error($message, /** @scrutinizer ignore-unused */ $context = array())

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...
76
    {
77
        $this->errorBuffer .= $message;
78
    }
79
}
80