|
7 | 7 | use Bambamboole\LaravelLokalise\LokaliseService; |
8 | 8 | use Illuminate\Filesystem\Filesystem; |
9 | 9 | use Illuminate\Support\Str; |
| 10 | +use PHPUnit\Framework\MockObject\MockObject; |
10 | 11 | use PHPUnit\Framework\TestCase; |
11 | 12 |
|
12 | 13 | class LokaliseServiceTest extends TestCase |
13 | 14 | { |
14 | | - public function test_it_preserves_curly_braces_formats() |
15 | | - { |
16 | | - $client = $this->createMock(LokaliseClient::class); |
17 | | - $repository = $this->createMock(LocalTranslationRepository::class); |
18 | | - $service = new LokaliseService($client, $repository, __DIR__); |
19 | | - |
20 | | - $result = $service->prepare([ |
21 | | - // Basic Laravel placeholders |
22 | | - 'accepted' => 'The :attribute must be accepted.', |
23 | | - |
24 | | - // Variable within curly braces - should not be converted |
25 | | - 'page_page_nb' => 'Page :page of {nb}', |
26 | | - |
27 | | - // Complex variables in curly braces - should not be converted |
28 | | - 'eg_discount' => 'e.g. discount {ZAHLUNGSZIELSKONTO}% within {ZAHLUNGSZIELTAGESKONTO} days.', |
29 | | - |
30 | | - // Variable in curly braces with text explanation - should not convert the curly brace content |
31 | | - 'content_password' => 'The content can be accessed via the variable {PASSWORT}.', |
32 | | - |
33 | | - // HTML with styling that contains colons - should not convert CSS attributes |
34 | | - 'table_style' => '<table style="width:100%;"><tr><td width="100%">:prefix should convert</td></tr></table>', |
35 | | - |
36 | | - // Mixed example with both HTML styling and regular Laravel variables |
37 | | - 'mixed_html' => '<div style="color:red; width:50%;">The :attribute field is :status.</div>', |
38 | | - ]); |
| 15 | + private string $basePath; |
39 | 16 |
|
40 | | - $this->assertEquals('The {{attribute}} must be accepted.', $result['accepted']); |
41 | | - // Verify basic Laravel placeholders are converted |
42 | | - $this->assertEquals('The {{attribute}} must be accepted.', $result['accepted']); |
| 17 | + private MockObject|LokaliseClient $client; |
43 | 18 |
|
44 | | - // Verify variable within curly braces are not converted |
45 | | - $this->assertEquals('Page {{page}} of {nb}', $result['page_page_nb']); |
| 19 | + private LocalTranslationRepository $repo; |
46 | 20 |
|
47 | | - // Verify complex variables in curly braces are not converted |
48 | | - $this->assertEquals('e.g. discount {ZAHLUNGSZIELSKONTO}% within {ZAHLUNGSZIELTAGESKONTO} days.', $result['eg_discount']); |
49 | | - |
50 | | - // Verify variable in curly braces with text explanation is not converted |
51 | | - $this->assertEquals('The content can be accessed via the variable {PASSWORT}.', $result['content_password']); |
52 | | - |
53 | | - // Verify HTML with styling that contains colons is not converted in attributes |
54 | | - $this->assertEquals('<table style="width:100%;"><tr><td width="100%">{{prefix}} should convert</td></tr></table>', $result['table_style']); |
55 | | - |
56 | | - // Verify mixed example works correctly |
57 | | - $this->assertEquals('<div style="color:red; width:50%;">The {{attribute}} field is {{status}}.</div>', $result['mixed_html']); |
| 21 | + protected function setUp(): void |
| 22 | + { |
| 23 | + $this->basePath = dirname(__DIR__).'/fixtures'; |
| 24 | + $this->client = $this->createMock(LokaliseClient::class); |
| 25 | + $this->repo = new LocalTranslationRepository(new Filesystem, $this->basePath); |
58 | 26 | } |
59 | 27 |
|
60 | 28 | public function test_it_skips_json_files_if_configured() |
61 | 29 | { |
62 | | - $basePath = dirname(__DIR__).'/fixtures'; |
63 | | - $repo = new LocalTranslationRepository(new Filesystem, $basePath); |
64 | | - $client = $this->createMock(LokaliseClient::class); |
65 | | - $client->expects(self::once()) |
| 30 | + $this->client->expects(self::once()) |
66 | 31 | ->method('getLocales') |
67 | 32 | ->willReturn(['en', 'de']); |
68 | | - $client->expects(self::exactly(2)) |
| 33 | + $this->client->expects(self::exactly(2)) |
69 | 34 | ->method('uploadFile') |
70 | 35 | ->with(self::anything(), self::callback(fn ($file) => Str::endsWith($file, '.php'))); |
71 | 36 |
|
72 | | - $service = new LokaliseService($client, $repo, dirname(__DIR__).'/fixtures'); |
73 | | - |
74 | | - $service->uploadTranslations(); |
| 37 | + $this->createSubject()->uploadTranslations(); |
75 | 38 | } |
76 | 39 |
|
77 | 40 | public function test_it_includes_json_files_if_configured() |
78 | 41 | { |
79 | | - $basePath = dirname(__DIR__).'/fixtures'; |
80 | | - $repo = new LocalTranslationRepository(new Filesystem, $basePath); |
81 | | - $client = $this->createMock(LokaliseClient::class); |
82 | | - $client->expects(self::once()) |
| 42 | + $this->client->expects(self::once()) |
83 | 43 | ->method('getLocales') |
84 | 44 | ->willReturn(['en', 'de']); |
85 | | - $client->expects(self::exactly(4)) |
| 45 | + $this->client->expects(self::exactly(4)) |
86 | 46 | ->method('uploadFile') |
87 | 47 | ->with( |
88 | 48 | self::anything(), |
89 | 49 | self::callback(fn ($file) => Str::endsWith($file, '.php') || Str::endsWith($file, '.json')), |
90 | 50 | ); |
91 | 51 |
|
92 | | - $service = new LokaliseService($client, $repo, dirname(__DIR__).'/fixtures', false); |
| 52 | + $this->createSubject(false)->uploadTranslations(); |
| 53 | + } |
93 | 54 |
|
94 | | - $service->uploadTranslations(); |
| 55 | + private function createSubject(bool $skipJsonFiles = true): LokaliseService |
| 56 | + { |
| 57 | + return new LokaliseService($this->client, $this->repo, $this->basePath, $skipJsonFiles); |
95 | 58 | } |
96 | 59 | } |
0 commit comments