|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Codeception\Module\Symfony; |
| 6 | + |
| 7 | +use Symfony\Component\Translation\DataCollector\TranslationDataCollector; |
| 8 | + |
| 9 | +trait TranslationAssertionsTrait |
| 10 | +{ |
| 11 | + /** |
| 12 | + * Asserts that no defined translations were found. |
| 13 | + * |
| 14 | + * ```php |
| 15 | + * <?php |
| 16 | + * $I->dontSeeTranslationDefinedMessages(); |
| 17 | + * ``` |
| 18 | + */ |
| 19 | + public function dontSeeTranslationDefinedMessages(): void |
| 20 | + { |
| 21 | + $translationCollector = $this->grabTranslationCollector(__FUNCTION__); |
| 22 | + $defines = $translationCollector->getCountDefines(); |
| 23 | + |
| 24 | + $this->assertSame( |
| 25 | + $defines, |
| 26 | + 0, |
| 27 | + "Expected no defined translations, but found {$defines}." |
| 28 | + ); |
| 29 | + } |
| 30 | + |
| 31 | + /** |
| 32 | + * Asserts that no fallback translations were found. |
| 33 | + * |
| 34 | + * ```php |
| 35 | + * <?php |
| 36 | + * $I->dontSeeTranslationFallbackMessages(); |
| 37 | + * ``` |
| 38 | + */ |
| 39 | + public function dontSeeTranslationFallbackMessages(): void |
| 40 | + { |
| 41 | + $translationCollector = $this->grabTranslationCollector(__FUNCTION__); |
| 42 | + $fallbacks = $translationCollector->getCountFallbacks(); |
| 43 | + |
| 44 | + $this->assertSame( |
| 45 | + $fallbacks, |
| 46 | + 0, |
| 47 | + "Expected no fallback translations, but found {$fallbacks}." |
| 48 | + ); |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * Asserts that no missing translations were found. |
| 53 | + * |
| 54 | + * ```php |
| 55 | + * <?php |
| 56 | + * $I->dontSeeTranslationMissingMessages(); |
| 57 | + * ``` |
| 58 | + */ |
| 59 | + public function dontSeeTranslationMissingMessages(): void |
| 60 | + { |
| 61 | + $translationCollector = $this->grabTranslationCollector(__FUNCTION__); |
| 62 | + $missings = $translationCollector->getCountMissings(); |
| 63 | + |
| 64 | + $this->assertSame( |
| 65 | + $missings, |
| 66 | + 0, |
| 67 | + "Expected no missing translations, but found {$missings}." |
| 68 | + ); |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * Asserts that the count of defined translations is greater than the given limit. |
| 73 | + * |
| 74 | + * ```php |
| 75 | + * <?php |
| 76 | + * $I->seeTranslationDefinedMessagesCountGreaterThan(5); |
| 77 | + * ``` |
| 78 | + * |
| 79 | + * @param int $limit Minimum count of defined translations |
| 80 | + */ |
| 81 | + public function seeTranslationDefinedMessagesCountGreaterThan(int $limit): void |
| 82 | + { |
| 83 | + $translationCollector = $this->grabTranslationCollector(__FUNCTION__); |
| 84 | + $defines = $translationCollector->getCountDefines(); |
| 85 | + |
| 86 | + $this->assertGreaterThan( |
| 87 | + $limit, |
| 88 | + $defines, |
| 89 | + "Expected more than {$limit} defined translations, but found {$defines}." |
| 90 | + ); |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * Asserts that the fallback locales match the expected ones. |
| 95 | + * |
| 96 | + * ```php |
| 97 | + * <?php |
| 98 | + * $I->seeTranslationFallbackLocalesAre(['es', 'fr']); |
| 99 | + * ``` |
| 100 | + * |
| 101 | + * @param array $expectedLocales The expected fallback locales |
| 102 | + */ |
| 103 | + public function seeTranslationFallbackLocalesAre(array $expectedLocales): void |
| 104 | + { |
| 105 | + $translationCollector = $this->grabTranslationCollector(__FUNCTION__); |
| 106 | + $fallbackLocales = $translationCollector->getFallbackLocales(); |
| 107 | + |
| 108 | + if ($fallbackLocales instanceof \Symfony\Component\VarDumper\Cloner\Data) { |
| 109 | + $fallbackLocales = $fallbackLocales->getValue(true); |
| 110 | + } |
| 111 | + |
| 112 | + $this->assertEquals( |
| 113 | + $expectedLocales, |
| 114 | + $fallbackLocales, |
| 115 | + "Fallback locales do not match expected." |
| 116 | + ); |
| 117 | + } |
| 118 | + |
| 119 | + /** |
| 120 | + * Asserts that the count of fallback translations is less than the given limit. |
| 121 | + * |
| 122 | + * ```php |
| 123 | + * <?php |
| 124 | + * $I->seeTranslationFallbackMessagesCountLessThan(10); |
| 125 | + * ``` |
| 126 | + * |
| 127 | + * @param int $limit Maximum count of fallback translations |
| 128 | + */ |
| 129 | + public function seeTranslationFallbackMessagesCountLessThan(int $limit): void |
| 130 | + { |
| 131 | + $translationCollector = $this->grabTranslationCollector(__FUNCTION__); |
| 132 | + $fallbacks = $translationCollector->getCountFallbacks(); |
| 133 | + |
| 134 | + $this->assertLessThan( |
| 135 | + $limit, |
| 136 | + $fallbacks, |
| 137 | + "Expected fewer than {$limit} fallback translations, but found {$fallbacks}." |
| 138 | + ); |
| 139 | + } |
| 140 | + |
| 141 | + /** |
| 142 | + * Asserts that the current locale is the expected one. |
| 143 | + * |
| 144 | + * ```php |
| 145 | + * <?php |
| 146 | + * $I->seeTranslationLocaleIs('en'); |
| 147 | + * ``` |
| 148 | + * |
| 149 | + * @param string $expectedLocale The expected locale |
| 150 | + */ |
| 151 | + public function seeTranslationLocaleIs(string $expectedLocale): void |
| 152 | + { |
| 153 | + $translationCollector = $this->grabTranslationCollector(__FUNCTION__); |
| 154 | + $locale = $translationCollector->getLocale(); |
| 155 | + |
| 156 | + $this->assertSame( |
| 157 | + $expectedLocale, |
| 158 | + $locale, |
| 159 | + "Expected locale '{$expectedLocale}', but found '{$locale}'." |
| 160 | + ); |
| 161 | + } |
| 162 | + |
| 163 | + /** |
| 164 | + * Asserts that the count of missing translations is less than the given limit. |
| 165 | + * |
| 166 | + * ```php |
| 167 | + * <?php |
| 168 | + * $I->seeTranslationMissingMessagesCountLessThan(3); |
| 169 | + * ``` |
| 170 | + * |
| 171 | + * @param int $limit Maximum count of missing translations |
| 172 | + */ |
| 173 | + public function seeTranslationMissingMessagesCountLessThan(int $limit): void |
| 174 | + { |
| 175 | + $translationCollector = $this->grabTranslationCollector(__FUNCTION__); |
| 176 | + $missings = $translationCollector->getCountMissings(); |
| 177 | + |
| 178 | + $this->assertLessThan( |
| 179 | + $limit, |
| 180 | + $missings, |
| 181 | + "Expected fewer than {$limit} missing translations, but found {$missings}." |
| 182 | + ); |
| 183 | + } |
| 184 | + |
| 185 | + protected function grabTranslationCollector(string $function): TranslationDataCollector |
| 186 | + { |
| 187 | + return $this->grabCollector('translation', $function); |
| 188 | + } |
| 189 | +} |
0 commit comments