UtilityTest   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 4
eloc 36
dl 0
loc 55
c 0
b 0
f 0
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 6 1
A testSetHttpOption() 0 24 1
A testIsValidLocale() 0 18 2
1
<?php
2
3
namespace Stichoza\GoogleTranslate\Tests;
4
5
use ReflectionClass;
6
use Stichoza\GoogleTranslate\TranslateClient;
7
8
class UtilityTest extends \PHPUnit_Framework_TestCase
0 ignored issues
show
Bug introduced by
The type PHPUnit_Framework_TestCase was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
9
{
10
    public function setUp()
11
    {
12
        $this->tr = new TranslateClient();
0 ignored issues
show
Bug Best Practice introduced by
The property tr does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
13
        $reflection = new ReflectionClass(get_class($this->tr));
14
        $this->method = $reflection->getMethod('isValidLocale');
0 ignored issues
show
Bug Best Practice introduced by
The property method does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
15
        $this->method->setAccessible(true);
16
    }
17
18
    public function testIsValidLocale()
19
    {
20
        $m = $this->method;
21
        $t = $this->tr;
22
23
        $booleanAssertions = [
24
            'ab'      => true,
25
            'ab-CD'   => true,
26
            'ab-CDE'  => false,
27
            'abc-DE'  => false,
28
            'abc-DEF' => false,
29
            'abc'     => false,
30
            'ab-'     => false,
31
            'a'       => false,
32
        ];
33
34
        foreach ($booleanAssertions as $key => $value) {
35
            $this->assertEquals($m->invokeArgs($t, [$key]), $value);
36
        }
37
    }
38
39
    public function testSetHttpOption()
40
    {
41
        $res = fopen('php://memory', 'r+');
42
43
        $this->tr->setHttpOption([
44
            'debug'   => $res,
45
            'headers' => [
46
                'User-Agent' => 'Foo',
47
            ],
48
        ])->translate('hello');
49
        rewind($res);
0 ignored issues
show
Bug introduced by
It seems like $res can also be of type false; however, parameter $handle of rewind() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

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

49
        rewind(/** @scrutinizer ignore-type */ $res);
Loading history...
50
        $output = str_replace("\r", '', stream_get_contents($res));
0 ignored issues
show
Bug introduced by
It seems like $res can also be of type false; however, parameter $handle of stream_get_contents() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

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

50
        $output = str_replace("\r", '', stream_get_contents(/** @scrutinizer ignore-type */ $res));
Loading history...
51
        $this->assertContains('User-Agent: Foo', $output);
52
53
        $this->tr->setHttpOption([
54
            'debug'   => $res,
55
            'headers' => [
56
                'User-Agent' => 'Bar',
57
            ],
58
        ])->translate('world');
59
        rewind($res);
60
        $output = str_replace("\r", '', stream_get_contents($res));
61
        $this->assertContains('User-Agent: Bar', $output);
62
        fclose($res);
0 ignored issues
show
Bug introduced by
It seems like $res can also be of type false; however, parameter $handle of fclose() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

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

62
        fclose(/** @scrutinizer ignore-type */ $res);
Loading history...
63
    }
64
}
65