Skip to content

Commit 94e411b

Browse files
authored
Added Symfony Translation assertions (#205)
1 parent 5e042f1 commit 94e411b

File tree

3 files changed

+181
-0
lines changed

3 files changed

+181
-0
lines changed

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
"symfony/security-core": "^5.4 | ^6.4 | ^7.2",
5353
"symfony/security-csrf": "^5.4 | ^6.4 | ^7.2",
5454
"symfony/security-http": "^5.4 | ^6.4 | ^7.2",
55+
"symfony/translation": "^5.4 | ^6.4 | ^7.2",
5556
"symfony/twig-bundle": "^5.4 | ^6.4 | ^7.2",
5657
"symfony/validator": "^5.4 | ^6.4 | ^7.2",
5758
"symfony/var-exporter": "^5.4 | ^6.4 | ^7.2",

src/Codeception/Module/Symfony.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
use Codeception\Module\Symfony\ServicesAssertionsTrait;
2626
use Codeception\Module\Symfony\SessionAssertionsTrait;
2727
use Codeception\Module\Symfony\TimeAssertionsTrait;
28+
use Codeception\Module\Symfony\TranslationAssertionsTrait;
2829
use Codeception\Module\Symfony\TwigAssertionsTrait;
2930
use Codeception\Module\Symfony\ValidatorAssertionsTrait;
3031
use Codeception\TestInterface;
@@ -148,6 +149,7 @@ class Symfony extends Framework implements DoctrineProvider, PartedModule
148149
use SecurityAssertionsTrait;
149150
use ServicesAssertionsTrait;
150151
use SessionAssertionsTrait;
152+
use TranslationAssertionsTrait;
151153
use TimeAssertionsTrait;
152154
use TwigAssertionsTrait;
153155
use ValidatorAssertionsTrait;
Lines changed: 178 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Codeception\Module\Symfony;
6+
7+
use Symfony\Component\Translation\DataCollector\TranslationDataCollector;
8+
use Symfony\Component\VarDumper\Cloner\Data;
9+
10+
trait TranslationAssertionsTrait
11+
{
12+
/**
13+
* Asserts that no fallback translations were found.
14+
*
15+
* ```php
16+
* <?php
17+
* $I->dontSeeFallbackTranslations();
18+
* ```
19+
*/
20+
public function dontSeeFallbackTranslations(): void
21+
{
22+
$translationCollector = $this->grabTranslationCollector(__FUNCTION__);
23+
$fallbacks = $translationCollector->getCountFallbacks();
24+
25+
$this->assertSame(
26+
$fallbacks,
27+
0,
28+
"Expected no fallback translations, but found {$fallbacks}."
29+
);
30+
}
31+
32+
/**
33+
* Asserts that no missing translations were found.
34+
*
35+
* ```php
36+
* <?php
37+
* $I->dontSeeMissingTranslations();
38+
* ```
39+
*/
40+
public function dontSeeMissingTranslations(): void
41+
{
42+
$translationCollector = $this->grabTranslationCollector(__FUNCTION__);
43+
$missings = $translationCollector->getCountMissings();
44+
45+
$this->assertSame(
46+
$missings,
47+
0,
48+
"Expected no missing translations, but found {$missings}."
49+
);
50+
}
51+
52+
/**
53+
* Grabs the count of defined translations.
54+
*
55+
* ```php
56+
* <?php
57+
* $count = $I->grabDefinedTranslations();
58+
* ```
59+
*
60+
* @return int The count of defined translations.
61+
*/
62+
public function grabDefinedTranslationsCount(): int
63+
{
64+
$translationCollector = $this->grabTranslationCollector(__FUNCTION__);
65+
return $translationCollector->getCountDefines();
66+
}
67+
68+
/**
69+
* Asserts that there are no missing translations and no fallback translations.
70+
*
71+
* ```php
72+
* <?php
73+
* $I->seeAllTranslationsDefined();
74+
* ```
75+
*/
76+
public function seeAllTranslationsDefined(): void
77+
{
78+
$this->dontSeeMissingTranslations();
79+
$this->dontSeeFallbackTranslations();
80+
}
81+
82+
/**
83+
* Asserts that the default locale is the expected one.
84+
*
85+
* ```php
86+
* <?php
87+
* $I->seeDefaultLocaleIs('en');
88+
* ```
89+
*
90+
* @param string $expectedLocale The expected default locale
91+
*/
92+
public function seeDefaultLocaleIs(string $expectedLocale): void
93+
{
94+
$translationCollector = $this->grabTranslationCollector(__FUNCTION__);
95+
$locale = $translationCollector->getLocale();
96+
97+
$this->assertSame(
98+
$expectedLocale,
99+
$locale,
100+
"Expected default locale '{$expectedLocale}', but found '{$locale}'."
101+
);
102+
}
103+
104+
/**
105+
* Asserts that the fallback locales match the expected ones.
106+
*
107+
* ```php
108+
* <?php
109+
* $I->seeFallbackLocalesAre(['es', 'fr']);
110+
* ```
111+
*
112+
* @param array $expectedLocales The expected fallback locales
113+
*/
114+
public function seeFallbackLocalesAre(array $expectedLocales): void
115+
{
116+
$translationCollector = $this->grabTranslationCollector(__FUNCTION__);
117+
$fallbackLocales = $translationCollector->getFallbackLocales();
118+
119+
if ($fallbackLocales instanceof Data) {
120+
$fallbackLocales = $fallbackLocales->getValue(true);
121+
}
122+
123+
$this->assertSame(
124+
$expectedLocales,
125+
$fallbackLocales,
126+
"Fallback locales do not match expected."
127+
);
128+
}
129+
130+
/**
131+
* Asserts that the count of fallback translations is less than the given limit.
132+
*
133+
* ```php
134+
* <?php
135+
* $I->seeFallbackTranslationsCountLessThan(10);
136+
* ```
137+
*
138+
* @param int $limit Maximum count of fallback translations
139+
*/
140+
public function seeFallbackTranslationsCountLessThan(int $limit): void
141+
{
142+
$translationCollector = $this->grabTranslationCollector(__FUNCTION__);
143+
$fallbacks = $translationCollector->getCountFallbacks();
144+
145+
$this->assertLessThan(
146+
$limit,
147+
$fallbacks,
148+
"Expected fewer than {$limit} fallback translations, but found {$fallbacks}."
149+
);
150+
}
151+
152+
/**
153+
* Asserts that the count of missing translations is less than the given limit.
154+
*
155+
* ```php
156+
* <?php
157+
* $I->seeMissingTranslationsCountLessThan(5);
158+
* ```
159+
*
160+
* @param int $limit Maximum count of missing translations
161+
*/
162+
public function seeMissingTranslationsCountLessThan(int $limit): void
163+
{
164+
$translationCollector = $this->grabTranslationCollector(__FUNCTION__);
165+
$missings = $translationCollector->getCountMissings();
166+
167+
$this->assertLessThan(
168+
$limit,
169+
$missings,
170+
"Expected fewer than {$limit} missing translations, but found {$missings}."
171+
);
172+
}
173+
174+
protected function grabTranslationCollector(string $function): TranslationDataCollector
175+
{
176+
return $this->grabCollector('translation', $function);
177+
}
178+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy