SyntaxHighlighterTest::getHighlighter()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 19
Code Lines 12

Duplication

Lines 19
Ratio 100 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
c 2
b 1
f 0
dl 19
loc 19
rs 9.4285
cc 1
eloc 12
nc 1
nop 0
1
<?php
2
3
namespace PhpSchool\PSXTest;
4
5
use Colors\Color;
6
use PhpParser\ParserFactory;
7
use PhpSchool\PSX\ColorsAdapter;
8
use PhpSchool\PSX\Lexer;
9
use PhpSchool\PSX\SyntaxHighlighter;
10
use PhpSchool\PSX\SyntaxHighlighterConfig;
11
use PhpSchool\PSX\SyntaxHighlightPrinter;
12
use PHPUnit_Framework_TestCase;
13
use InvalidArgumentException;
14
15
/**
16
 * Class SyntaxHighlighterTest
17
 * @package PhpSchool\PSXTest
18
 * @author Aydin Hassan <[email protected]>
19
 */
20
class SyntaxHighlighterTest extends PHPUnit_Framework_TestCase
21
{
22
    public function testExceptionIsThrownIfCodeIsNotString()
23
    {
24
        $this->setExpectedException(
25
            InvalidArgumentException::class,
26
            'Argument 1 should be a string of valid PHP code'
27
        );
28
        $highlighter = $this->getHighlighter();
29
        $highlighter->highlight([]);
0 ignored issues
show
Documentation introduced by
array() is of type array, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
30
    }
31
32
    public function testExceptionIsThrownIfInvalidCode()
33
    {
34
        $this->setExpectedException(
35
            InvalidArgumentException::class,
36
            'PHP could not be parsed'
37
        );
38
        $highlighter = $this->getHighlighter();
39
        $highlighter->highlight('<?php echo');
40
    }
41
42
    public function testSyntaxHighlighter()
43
    {
44
        $code = '<?php echo "hello world!";';
45
        $highlighter = $this->getHighlighter();
46
        $expected = "<?php\n\necho \"hello world!\";";
47
        $this->assertEquals($expected, $highlighter->highlight($code));
48
    }
49
50
    /**
51
     * @return SyntaxHighlighter
52
     */
53 View Code Duplication
    private function getHighlighter()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
54
    {
55
        $lexer = new Lexer([
56
            'usedAttributes' => [
57
                'comments', 'startLine', 'endLine', 'startFilePos', 'endFilePos', 'startTokenPos', 'endTokenPos'
58
            ]
59
        ]);
60
        
61
        $parserFactory = new ParserFactory;
62
        $color = new Color;
63
        $color->setForceStyle(true);
64
        return new SyntaxHighlighter(
65
            $parserFactory->create(ParserFactory::PREFER_PHP7, $lexer),
66
            new SyntaxHighlightPrinter(
67
                new SyntaxHighlighterConfig,
68
                new ColorsAdapter($color)
69
            )
70
        );
71
    }
72
}
73