Completed
Push — master ( 2ad0de...2fd600 )
by Levan
03:06
created

UtilityTest   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 2
dl 0
loc 61
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 7 1
A testIsValidLocale() 0 20 2
A testSetOptions() 0 25 1
1
<?php
2
3
namespace Stichoza\GoogleTranslate\Tests;
4
5
use ReflectionClass;
6
use PHPUnit\Framework\TestCase;
7
use Stichoza\GoogleTranslate\GoogleTranslate;
8
9
class UtilityTest extends TestCase
10
{
11
    public $tr;
12
13
    public $method;
14
15
    public function setUp()
16
    {
17
        $this->tr = new GoogleTranslate();
18
        $reflection = new ReflectionClass(get_class($this->tr));
19
        $this->method = $reflection->getMethod('isValidLocale');
20
        $this->method->setAccessible(true);
21
    }
22
23
    public function testIsValidLocale()
24
    {
25
        $m = $this->method;
26
        $t = $this->tr;
27
28
        $booleanAssertions = [
29
            'ab'      => true,
30
            'ab-CD'   => true,
31
            'ab-CDE'  => false,
32
            'abc-DE'  => false,
33
            'abc-DEF' => false,
34
            'abc'     => false,
35
            'ab-'     => false,
36
            'a'       => false,
37
        ];
38
39
        foreach ($booleanAssertions as $key => $value) {
40
            $this->assertEquals($m->invokeArgs($t, [$key]), $value);
41
        }
42
    }
43
44
    public function testSetOptions()
45
    {
46
        $res = fopen('php://memory', 'r+');
47
48
        $this->tr->setOptions([
49
            'debug'   => $res,
50
            'headers' => [
51
                'User-Agent' => 'Foo',
52
            ],
53
        ])->translate('hello');
54
        rewind($res);
55
        $output = str_replace("\r", '', stream_get_contents($res));
56
        $this->assertContains('User-Agent: Foo', $output);
57
58
        GoogleTranslate::trans('world', 'en', null, [
59
            'debug'   => $res,
60
            'headers' => [
61
                'User-Agent' => 'Bar',
62
            ],
63
        ]);
64
        rewind($res);
65
        $output = str_replace("\r", '', stream_get_contents($res));
66
        $this->assertContains('User-Agent: Bar', $output);
67
        fclose($res);
68
    }
69
}
70