|
| 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