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); |
|
|
|
|
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); |
|
|
|
|
76
|
|
|
$stream->method('__toString')->willReturn('{}'); |
|
|
|
|
77
|
|
|
$response->method('getStatusCode')->willReturn(201); |
|
|
|
|
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
|
|
|
|
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: