Completed
Push — master ( 581cf6...f9ddcf )
by Tobias
01:30
created

testOverridesTheDefaultLocaleWhenUsingTranslationKeys()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 34

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 34
rs 9.376
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the PHP Translation package.
7
 *
8
 * (c) PHP Translation team <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Translation\PlatformAdapter\Loco\Tests\Unit;
15
16
use FAPI\Localise\Hydrator\Hydrator;
17
use FAPI\Localise\LocoClient;
18
use Http\Client\HttpClient;
19
use PHPUnit\Framework\MockObject\MockObject;
20
use PHPUnit\Framework\TestCase;
21
use Psr\Http\Message\RequestInterface;
22
use Psr\Http\Message\ResponseInterface;
23
use Psr\Http\Message\StreamInterface;
24
use Symfony\Component\Translation\MessageCatalogue;
25
use Translation\PlatformAdapter\Loco\Loco;
26
use Translation\PlatformAdapter\Loco\Model\LocoProject;
27
28
class LocoTest extends TestCase
29
{
30
    /**
31
     * @var LocoClient
32
     */
33
    private $client;
34
35
    /**
36
     * @var HttpClient|MockObject
37
     */
38
    private $httpClient;
39
40
    /**
41
     * @var Hydrator|MockObject
42
     */
43
    private $hydrator;
44
45
    protected function setUp(): void
46
    {
47
        $this->httpClient = $this->createMock(HttpClient::class);
48
        $this->hydrator = $this->createMock(Hydrator::class);
49
        $this->client = new LocoClient($this->httpClient, $this->hydrator);
0 ignored issues
show
Documentation introduced by
$this->httpClient is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Http\Client\HttpClient>.

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...
Documentation introduced by
$this->hydrator is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a null|object<FAPI\Localise\Hydrator\Hydrator>.

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...
50
    }
51
52
    public function testOverridesTheDefaultLocaleWhenUsingTranslationKeys(): void
53
    {
54
        $locoProject = new LocoProject('main', ['api_key' => 'FooBar', 'index_parameter' => 'id']);
55
        $loco = new Loco($this->client, [$locoProject]);
56
57
        $catalogue = new MessageCatalogue('nl', []);
58
59
        $response = $this->createMock(ResponseInterface::class);
60
        $this->httpClient
61
            ->method('sendRequest')
62
            ->with(
63
                $this->callback(
64
                    // Capture the request body so we can make assertions on it later on
65
                    function (RequestInterface $argument) use (&$body): bool {
66
                        $body = $argument->getBody()->__toString();
67
68
                        return true;
69
                    }
70
                )
71
            )
72
            ->willReturn($response);
73
74
        $stream = $this->createMock(StreamInterface::class);
75
        $response->method('getBody')->willReturn($stream);
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<PHPUnit\Framework\MockObject\MockObject>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
76
        $stream->method('__toString')->willReturn('{}');
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<PHPUnit\Framework\MockObject\MockObject>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
77
        $response->method('getStatusCode')->willReturn(201);
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<PHPUnit\Framework\MockObject\MockObject>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
78
79
        $loco->import($catalogue);
80
81
        $this->assertStringContainsString(
82
            '<xliff xmlns="urn:oasis:names:tc:xliff:document:2.0" version="2.0" srcLang="x-id" trgLang="nl">',
83
            $body
84
        );
85
    }
86
}
87