|
2 | 2 |
|
3 | 3 | namespace Tests\Tempest\Integration\Intl; |
4 | 4 |
|
5 | | -use Tempest\Core\Commands\DiscoveryClearCommand; |
6 | | -use Tempest\Core\DiscoveryCache; |
7 | | -use Tempest\Core\FrameworkKernel; |
8 | | -use Tempest\Core\Kernel\LoadDiscoveryClasses; |
9 | | -use Tempest\Discovery\DiscoveryLocation; |
| 5 | +use Tempest\EventBus\EventBus; |
10 | 6 | use Tempest\Intl\Catalog\Catalog; |
11 | 7 | use Tempest\Intl\IntlConfig; |
12 | 8 | use Tempest\Intl\Locale; |
| 9 | +use Tempest\Intl\TranslationFailure; |
| 10 | +use Tempest\Intl\TranslationMiss; |
13 | 11 | use Tempest\Intl\Translator; |
14 | 12 | use Tests\Tempest\Integration\FrameworkIntegrationTestCase; |
15 | 13 |
|
16 | 14 | use function Tempest\Intl\translate; |
17 | 15 | use function Tempest\Intl\translate_locale; |
18 | | -use function Tempest\Support\Path\normalize; |
19 | 16 |
|
20 | 17 | final class TranslatorTest extends FrameworkIntegrationTestCase |
21 | 18 | { |
@@ -50,4 +47,44 @@ public function test_default_locale(): void |
50 | 47 |
|
51 | 48 | $this->assertSame(Locale::default(), $config->currentLocale); |
52 | 49 | } |
| 50 | + |
| 51 | + public function test_event_miss(): void |
| 52 | + { |
| 53 | + /** @var TranslationMiss|null $received */ |
| 54 | + $received = null; |
| 55 | + |
| 56 | + $eventbus = $this->container->get(EventBus::class); |
| 57 | + $eventbus->listen(TranslationMiss::class, function (TranslationMiss $event) use (&$received): void { |
| 58 | + $received = $event; |
| 59 | + }); |
| 60 | + |
| 61 | + $translator = $this->container->get(Translator::class); |
| 62 | + $translator->translate('unknown'); |
| 63 | + |
| 64 | + $this->assertInstanceOf(TranslationMiss::class, $received); |
| 65 | + $this->assertSame(Locale::ENGLISH_UNITED_STATES, $received->locale); |
| 66 | + $this->assertSame('unknown', $received->key); |
| 67 | + } |
| 68 | + |
| 69 | + public function test_event_fail(): void |
| 70 | + { |
| 71 | + /** @var TranslationFailure|null $received */ |
| 72 | + $received = null; |
| 73 | + |
| 74 | + $eventbus = $this->container->get(EventBus::class); |
| 75 | + $eventbus->listen(TranslationFailure::class, function (TranslationFailure $event) use (&$received): void { |
| 76 | + $received = $event; |
| 77 | + }); |
| 78 | + |
| 79 | + $catalog = $this->container->get(Catalog::class); |
| 80 | + $catalog->add(Locale::ENGLISH_UNITED_STATES, 'failure', '{$foo'); |
| 81 | + |
| 82 | + $translator = $this->container->get(Translator::class); |
| 83 | + $translator->translate('failure'); |
| 84 | + |
| 85 | + $this->assertInstanceOf(TranslationFailure::class, $received); |
| 86 | + $this->assertSame(Locale::ENGLISH_UNITED_STATES, $received->locale); |
| 87 | + $this->assertSame('failure', $received->key); |
| 88 | + $this->assertSame('Failed to parse message.', $received->exception->getMessage()); |
| 89 | + } |
53 | 90 | } |
0 commit comments