I18nDataTest.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744
  1. <?php
  2. require_once __DIR__ . '/../../../cli/i18n/I18nData.php';
  3. require_once __DIR__ . '/../../../cli/i18n/I18nValue.php';
  4. class I18nDataTest extends PHPUnit\Framework\TestCase {
  5. /** @var array<string,array<string,array<string,I18nValue>>> */
  6. private $referenceData;
  7. /** @var I18nValue&PHPUnit\Framework\MockObject\MockObject */
  8. private $value;
  9. public function setUp(): void {
  10. $this->value = $this->getMockBuilder(I18nValue::class)
  11. ->disableOriginalConstructor()
  12. ->getMock();
  13. $this->referenceData = [
  14. 'en' => [
  15. 'file1.php' => [
  16. 'file1.l1.l2.k1' => $this->value,
  17. 'file1.l1.l2.k2' => $this->value,
  18. ],
  19. 'file2.php' => [
  20. 'file2.l1.l2._' => $this->value,
  21. 'file2.l1.l2.k1' => $this->value,
  22. 'file2.l1.l2.k2' => $this->value,
  23. ],
  24. 'file3.php' => [
  25. 'file3.l1.l2._' => $this->value,
  26. 'file3.l1.l2.k1' => $this->value,
  27. ],
  28. ],
  29. ];
  30. }
  31. public function testConstructWhenReferenceOnly(): void {
  32. $data = new I18nData($this->referenceData);
  33. $this->assertEquals($this->referenceData, $data->getData());
  34. }
  35. public function testConstructorWhenLanguageIsMissingFile(): void {
  36. $rawData = array_merge($this->referenceData, [
  37. 'fr' => [
  38. 'file1.php' => [
  39. 'file1.l1.l2.k1' => $this->value,
  40. ],
  41. ],
  42. ]);
  43. $data = new I18nData($rawData);
  44. $this->assertEquals([
  45. 'en' => [
  46. 'file1.php' => [
  47. 'file1.l1.l2.k1' => $this->value,
  48. 'file1.l1.l2.k2' => $this->value,
  49. ],
  50. 'file2.php' => [
  51. 'file2.l1.l2._' => $this->value,
  52. 'file2.l1.l2.k1' => $this->value,
  53. 'file2.l1.l2.k2' => $this->value,
  54. ],
  55. 'file3.php' => [
  56. 'file3.l1.l2._' => $this->value,
  57. 'file3.l1.l2.k1' => $this->value,
  58. ],
  59. ],
  60. 'fr' => [
  61. 'file1.php' => [
  62. 'file1.l1.l2.k1' => $this->value,
  63. 'file1.l1.l2.k2' => $this->value,
  64. ],
  65. 'file2.php' => [
  66. 'file2.l1.l2._' => $this->value,
  67. 'file2.l1.l2.k1' => $this->value,
  68. 'file2.l1.l2.k2' => $this->value,
  69. ],
  70. 'file3.php' => [
  71. 'file3.l1.l2._' => $this->value,
  72. 'file3.l1.l2.k1' => $this->value,
  73. ],
  74. ],
  75. ], $data->getData());
  76. }
  77. public function testConstructorWhenLanguageIsMissingKeys(): void {
  78. $rawData = array_merge($this->referenceData, [
  79. 'fr' => [
  80. 'file1.php' => [
  81. 'file1.l1.l2.k1' => $this->value,
  82. ],
  83. 'file2.php' => [
  84. 'file2.l1.l2.k1' => $this->value,
  85. ],
  86. ],
  87. ]);
  88. $data = new I18nData($rawData);
  89. $this->assertEquals([
  90. 'en' => [
  91. 'file1.php' => [
  92. 'file1.l1.l2.k1' => $this->value,
  93. 'file1.l1.l2.k2' => $this->value,
  94. ],
  95. 'file2.php' => [
  96. 'file2.l1.l2._' => $this->value,
  97. 'file2.l1.l2.k1' => $this->value,
  98. 'file2.l1.l2.k2' => $this->value,
  99. ],
  100. 'file3.php' => [
  101. 'file3.l1.l2._' => $this->value,
  102. 'file3.l1.l2.k1' => $this->value,
  103. ],
  104. ],
  105. 'fr' => [
  106. 'file1.php' => [
  107. 'file1.l1.l2.k1' => $this->value,
  108. 'file1.l1.l2.k2' => $this->value,
  109. ],
  110. 'file2.php' => [
  111. 'file2.l1.l2._' => $this->value,
  112. 'file2.l1.l2.k1' => $this->value,
  113. 'file2.l1.l2.k2' => $this->value,
  114. ],
  115. 'file3.php' => [
  116. 'file3.l1.l2._' => $this->value,
  117. 'file3.l1.l2.k1' => $this->value,
  118. ],
  119. ],
  120. ], $data->getData());
  121. }
  122. public function testConstructorWhenLanguageHasExtraKeys(): void {
  123. $rawData = array_merge($this->referenceData, [
  124. 'fr' => [
  125. 'file1.php' => [
  126. 'file1.l1.l2.k1' => $this->value,
  127. 'file1.l1.l2.k2' => $this->value,
  128. 'file1.l1.l2.k3' => $this->value,
  129. ],
  130. 'file2.php' => [
  131. 'file2.l1.l2.k1' => $this->value,
  132. 'file2.l1.l2.k2' => $this->value,
  133. 'file2.l1.l2.k3' => $this->value,
  134. ],
  135. 'file3.php' => [
  136. 'file3.l1.l2._' => $this->value,
  137. 'file3.l1.l2.k1' => $this->value,
  138. ],
  139. ],
  140. ]);
  141. $data = new I18nData($rawData);
  142. $this->assertEquals([
  143. 'en' => [
  144. 'file1.php' => [
  145. 'file1.l1.l2.k1' => $this->value,
  146. 'file1.l1.l2.k2' => $this->value,
  147. ],
  148. 'file2.php' => [
  149. 'file2.l1.l2._' => $this->value,
  150. 'file2.l1.l2.k1' => $this->value,
  151. 'file2.l1.l2.k2' => $this->value,
  152. ],
  153. 'file3.php' => [
  154. 'file3.l1.l2._' => $this->value,
  155. 'file3.l1.l2.k1' => $this->value,
  156. ],
  157. ],
  158. 'fr' => [
  159. 'file1.php' => [
  160. 'file1.l1.l2.k1' => $this->value,
  161. 'file1.l1.l2.k2' => $this->value,
  162. ],
  163. 'file2.php' => [
  164. 'file2.l1.l2._' => $this->value,
  165. 'file2.l1.l2.k1' => $this->value,
  166. 'file2.l1.l2.k2' => $this->value,
  167. ],
  168. 'file3.php' => [
  169. 'file3.l1.l2._' => $this->value,
  170. 'file3.l1.l2.k1' => $this->value,
  171. ],
  172. ],
  173. ], $data->getData());
  174. }
  175. public function testConstructorWhenValueIsIdenticalAndIsMarkedAsIgnore(): void {
  176. $value = $this->getMockBuilder(I18nValue::class)
  177. ->disableOriginalConstructor()
  178. ->getMock();
  179. $value->expects($this->exactly(2))
  180. ->method('isIgnore')
  181. ->willReturn(true);
  182. $value->expects($this->never())
  183. ->method('markAsTodo');
  184. $value->expects($this->exactly(3))
  185. ->method('equal')
  186. ->with($value)
  187. ->willReturn(true);
  188. $rawData = array_merge($this->referenceData, [
  189. 'fr' => [
  190. 'file2.php' => [
  191. 'file2.l1.l2.k1' => $value,
  192. ],
  193. ],
  194. ]);
  195. $rawData['en']['file2.php']['file2.l1.l2.k1'] = $value;
  196. new I18nData($rawData);
  197. }
  198. public function testConstructorWhenValueIsIdenticalAndIsNotMarkedAsIgnore(): void {
  199. $value = $this->getMockBuilder(I18nValue::class)
  200. ->disableOriginalConstructor()
  201. ->getMock();
  202. $value->expects($this->exactly(2))
  203. ->method('isIgnore')
  204. ->willReturn(false);
  205. $value->expects($this->exactly(2))
  206. ->method('markAsTodo');
  207. $value->expects($this->exactly(2))
  208. ->method('equal')
  209. ->with($value)
  210. ->willReturn(true);
  211. $rawData = array_merge($this->referenceData, [
  212. 'fr' => [
  213. 'file2.php' => [
  214. 'file2.l1.l2.k1' => $value,
  215. ],
  216. ],
  217. ]);
  218. $rawData['en']['file2.php']['file2.l1.l2.k1'] = $value;
  219. new I18nData($rawData);
  220. }
  221. public function testConstructorWhenValueIsDifferentAndIsMarkedAsToDo(): void {
  222. $value = $this->getMockBuilder(I18nValue::class)
  223. ->disableOriginalConstructor()
  224. ->getMock();
  225. $value->expects($this->once())
  226. ->method('isTodo')
  227. ->willReturn(true);
  228. $value->expects($this->once())
  229. ->method('markAsDirty');
  230. $rawData = array_merge($this->referenceData, [
  231. 'fr' => [
  232. 'file2.php' => [
  233. 'file2.l1.l2.k1' => $value,
  234. ],
  235. ],
  236. ]);
  237. new I18nData($rawData);
  238. }
  239. public function testConstructorWhenValueIsDifferentAndIsNotMarkedAsTodo(): void {
  240. $value = $this->getMockBuilder(I18nValue::class)
  241. ->disableOriginalConstructor()
  242. ->getMock();
  243. $value->expects($this->once())
  244. ->method('isTodo')
  245. ->willReturn(false);
  246. $value->expects($this->never())
  247. ->method('markAsDirty');
  248. $rawData = array_merge($this->referenceData, [
  249. 'fr' => [
  250. 'file2.php' => [
  251. 'file2.l1.l2.k1' => $value,
  252. ],
  253. ],
  254. ]);
  255. new I18nData($rawData);
  256. }
  257. public function testGetAvailableLanguagesWhenTheyAreSorted(): void {
  258. $rawData = array_merge($this->referenceData, [
  259. 'fr' => [],
  260. 'nl' => [],
  261. ]);
  262. $data = new I18nData($rawData);
  263. $this->assertEquals([
  264. 'en',
  265. 'fr',
  266. 'nl',
  267. ], $data->getAvailableLanguages());
  268. }
  269. public function testGetAvailableLanguagesWhenTheyAreNotSorted(): void {
  270. $rawData = array_merge($this->referenceData, [
  271. 'nl' => [],
  272. 'fr' => [],
  273. 'de' => [],
  274. ]);
  275. $data = new I18nData($rawData);
  276. $this->assertEquals([
  277. 'de',
  278. 'en',
  279. 'fr',
  280. 'nl',
  281. ], $data->getAvailableLanguages());
  282. }
  283. public function testAddLanguageWhenLanguageExists(): void {
  284. $this->expectException(\Exception::class);
  285. $this->expectExceptionMessage('The selected language already exist.');
  286. $data = new I18nData($this->referenceData);
  287. $data->addLanguage('en');
  288. }
  289. public function testAddLanguageWhenNoReferenceProvided(): void {
  290. $data = new I18nData($this->referenceData);
  291. $data->addLanguage('fr');
  292. $this->assertEquals([
  293. 'en' => [
  294. 'file1.php' => [
  295. 'file1.l1.l2.k1' => $this->value,
  296. 'file1.l1.l2.k2' => $this->value,
  297. ],
  298. 'file2.php' => [
  299. 'file2.l1.l2._' => $this->value,
  300. 'file2.l1.l2.k1' => $this->value,
  301. 'file2.l1.l2.k2' => $this->value,
  302. ],
  303. 'file3.php' => [
  304. 'file3.l1.l2._' => $this->value,
  305. 'file3.l1.l2.k1' => $this->value,
  306. ],
  307. ],
  308. 'fr' => [
  309. 'file1.php' => [
  310. 'file1.l1.l2.k1' => $this->value,
  311. 'file1.l1.l2.k2' => $this->value,
  312. ],
  313. 'file2.php' => [
  314. 'file2.l1.l2._' => $this->value,
  315. 'file2.l1.l2.k1' => $this->value,
  316. 'file2.l1.l2.k2' => $this->value,
  317. ],
  318. 'file3.php' => [
  319. 'file3.l1.l2._' => $this->value,
  320. 'file3.l1.l2.k1' => $this->value,
  321. ],
  322. ],
  323. ], $data->getData());
  324. }
  325. public function testAddLanguageWhenUnknownReferenceProvided(): void {
  326. $data = new I18nData($this->referenceData);
  327. $data->addLanguage('fr', 'unknown');
  328. $this->assertEquals([
  329. 'en' => [
  330. 'file1.php' => [
  331. 'file1.l1.l2.k1' => $this->value,
  332. 'file1.l1.l2.k2' => $this->value,
  333. ],
  334. 'file2.php' => [
  335. 'file2.l1.l2._' => $this->value,
  336. 'file2.l1.l2.k1' => $this->value,
  337. 'file2.l1.l2.k2' => $this->value,
  338. ],
  339. 'file3.php' => [
  340. 'file3.l1.l2._' => $this->value,
  341. 'file3.l1.l2.k1' => $this->value,
  342. ],
  343. ],
  344. 'fr' => [
  345. 'file1.php' => [
  346. 'file1.l1.l2.k1' => $this->value,
  347. 'file1.l1.l2.k2' => $this->value,
  348. ],
  349. 'file2.php' => [
  350. 'file2.l1.l2._' => $this->value,
  351. 'file2.l1.l2.k1' => $this->value,
  352. 'file2.l1.l2.k2' => $this->value,
  353. ],
  354. 'file3.php' => [
  355. 'file3.l1.l2._' => $this->value,
  356. 'file3.l1.l2.k1' => $this->value,
  357. ],
  358. ],
  359. ], $data->getData());
  360. }
  361. public function testAddLanguageWhenKnownReferenceProvided(): void {
  362. $data = new I18nData($this->referenceData);
  363. $data->addLanguage('fr', 'en');
  364. $this->assertEquals([
  365. 'en' => [
  366. 'file1.php' => [
  367. 'file1.l1.l2.k1' => $this->value,
  368. 'file1.l1.l2.k2' => $this->value,
  369. ],
  370. 'file2.php' => [
  371. 'file2.l1.l2._' => $this->value,
  372. 'file2.l1.l2.k1' => $this->value,
  373. 'file2.l1.l2.k2' => $this->value,
  374. ],
  375. 'file3.php' => [
  376. 'file3.l1.l2._' => $this->value,
  377. 'file3.l1.l2.k1' => $this->value,
  378. ],
  379. ],
  380. 'fr' => [
  381. 'file1.php' => [
  382. 'file1.l1.l2.k1' => $this->value,
  383. 'file1.l1.l2.k2' => $this->value,
  384. ],
  385. 'file2.php' => [
  386. 'file2.l1.l2._' => $this->value,
  387. 'file2.l1.l2.k1' => $this->value,
  388. 'file2.l1.l2.k2' => $this->value,
  389. ],
  390. 'file3.php' => [
  391. 'file3.l1.l2._' => $this->value,
  392. 'file3.l1.l2.k1' => $this->value,
  393. ],
  394. ],
  395. ], $data->getData());
  396. }
  397. public function testIsKnownWhenKeyExists(): void {
  398. $data = new I18nData($this->referenceData);
  399. $this->assertTrue($data->isKnown('file2.l1.l2.k2'));
  400. }
  401. public function testIsKnownWhenKeyDoesNotExist(): void {
  402. $data = new I18nData($this->referenceData);
  403. $this->assertFalse($data->isKnown('file2.l1.l2.k3'));
  404. }
  405. public function testAddKeyWhenKeyExists(): void {
  406. $this->expectException(\Exception::class);
  407. $this->expectExceptionMessage('The selected key already exist.');
  408. $data = new I18nData($this->referenceData);
  409. $data->addKey('file2.l1.l2.k1', 'value');
  410. }
  411. public function testAddKeyWhenParentKeyExists(): void {
  412. $rawData = array_merge($this->referenceData, [
  413. 'fr' => [],
  414. ]);
  415. $data = new I18nData($rawData);
  416. $this->assertTrue($data->isKnown('file2.l1.l2.k1'));
  417. $this->assertFalse($data->isKnown('file2.l1.l2.k1._'));
  418. $this->assertFalse($data->isKnown('file2.l1.l2.k1.sk1'));
  419. $data->addKey('file2.l1.l2.k1.sk1', 'value');
  420. $this->assertFalse($data->isKnown('file2.l1.l2.k1'));
  421. $this->assertTrue($data->isKnown('file2.l1.l2.k1._'));
  422. $this->assertTrue($data->isKnown('file2.l1.l2.k1.sk1'));
  423. }
  424. public function testAddKeyWhenKeyIsParent(): void {
  425. $rawData = array_merge($this->referenceData, [
  426. 'fr' => [],
  427. ]);
  428. $data = new I18nData($rawData);
  429. $this->assertFalse($data->isKnown('file1.l1.l2._'));
  430. $this->assertTrue($data->isKnown('file1.l1.l2.k1'));
  431. $this->assertTrue($data->isKnown('file1.l1.l2.k2'));
  432. $data->addKey('file1.l1.l2', 'value');
  433. $this->assertTrue($data->isKnown('file1.l1.l2._'));
  434. $this->assertTrue($data->isKnown('file1.l1.l2.k1'));
  435. $this->assertTrue($data->isKnown('file1.l1.l2.k2'));
  436. }
  437. public function testAddKey(): void {
  438. $getTargetedValue = static function (I18nData $data, string $language) {
  439. return $data->getData()[$language]['file2.php']['file2.l1.l2.k3'];
  440. };
  441. $rawData = array_merge($this->referenceData, [
  442. 'fr' => [],
  443. ]);
  444. $data = new I18nData($rawData);
  445. $this->assertFalse($data->isKnown('file2.l1.l2.k3'));
  446. $data->addKey('file2.l1.l2.k3', 'value');
  447. $this->assertTrue($data->isKnown('file2.l1.l2.k3'));
  448. $enValue = $getTargetedValue($data, 'en');
  449. $frValue = $getTargetedValue($data, 'fr');
  450. $this->assertInstanceOf(I18nValue::class, $enValue);
  451. $this->assertEquals('value', $enValue->getValue());
  452. $this->assertTrue($enValue->isTodo());
  453. $this->assertEquals($frValue, $enValue);
  454. }
  455. public function testAddValueWhenLanguageDoesNotExist(): void {
  456. $this->expectException(\Exception::class);
  457. $this->expectExceptionMessage('The selected language does not exist.');
  458. $data = new I18nData($this->referenceData);
  459. $data->addValue('file2.l1.l2.k2', 'new value', 'fr');
  460. }
  461. public function testAddValueWhenKeyDoesNotExist(): void {
  462. $this->expectException(\Exception::class);
  463. $this->expectExceptionMessage('The selected key does not exist for the selected language.');
  464. $data = new I18nData($this->referenceData);
  465. $data->addValue('unknown key', 'new value', 'en');
  466. }
  467. public function testAddValueWhenLanguageIsReferenceAndValueInOtherLanguageHasNotChange(): void {
  468. $getTargetedValue = static function (I18nData $data, string $language) {
  469. return $data->getData()[$language]['file2.php']['file2.l1.l2.k2'];
  470. };
  471. $this->value->expects($this->atLeast(2))
  472. ->method('equal')
  473. ->with($this->value)
  474. ->willReturn(true);
  475. $rawData = array_merge($this->referenceData, [
  476. 'fr' => [],
  477. ]);
  478. $data = new I18nData($rawData);
  479. $beforeEnValue = $getTargetedValue($data, 'en');
  480. $beforeFrValue = $getTargetedValue($data, 'fr');
  481. $data->addValue('file2.l1.l2.k2', 'new value', 'en');
  482. $afterEnValue = $getTargetedValue($data, 'en');
  483. $afterFrValue = $getTargetedValue($data, 'fr');
  484. $this->assertEquals($this->value, $beforeEnValue);
  485. $this->assertEquals($this->value, $beforeFrValue);
  486. $this->assertInstanceOf(I18nValue::class, $afterEnValue);
  487. $this->assertEquals('new value', $afterEnValue->getValue());
  488. $this->assertInstanceOf(I18nValue::class, $afterFrValue);
  489. $this->assertEquals('new value', $afterFrValue->getValue());
  490. }
  491. public function testAddValueWhenLanguageIsReferenceAndValueInOtherLanguageHasChange(): void {
  492. $getTargetedValue = static function (I18nData $data, string $language) {
  493. return $data->getData()[$language]['file2.php']['file2.l1.l2.k2'];
  494. };
  495. $this->value->expects($this->any())
  496. ->method('equal')
  497. ->with($this->value)
  498. ->willReturn(true);
  499. $value = $this->getMockBuilder(I18nValue::class)
  500. ->disableOriginalConstructor()
  501. ->getMock();
  502. $rawData = array_merge($this->referenceData, [
  503. 'fr' => [
  504. 'file2.php' => [
  505. 'file2.l1.l2.k2' => $value,
  506. ]
  507. ],
  508. ]);
  509. $data = new I18nData($rawData);
  510. $beforeEnValue = $getTargetedValue($data, 'en');
  511. $beforeFrValue = $getTargetedValue($data, 'fr');
  512. $data->addValue('file2.l1.l2.k2', 'new value', 'en');
  513. $afterEnValue = $getTargetedValue($data, 'en');
  514. $afterFrValue = $getTargetedValue($data, 'fr');
  515. $this->assertEquals($this->value, $beforeEnValue);
  516. $this->assertEquals($value, $beforeFrValue);
  517. $this->assertInstanceOf(I18nValue::class, $afterEnValue);
  518. $this->assertEquals('new value', $afterEnValue->getValue());
  519. $this->assertEquals($value, $afterFrValue);
  520. }
  521. public function testAddValueWhenLanguageIsNotReference(): void {
  522. $getTargetedValue = static function (I18nData $data, string $language) {
  523. return $data->getData()[$language]['file2.php']['file2.l1.l2.k2'];
  524. };
  525. $rawData = array_merge($this->referenceData, [
  526. 'fr' => [],
  527. ]);
  528. $data = new I18nData($rawData);
  529. $beforeEnValue = $getTargetedValue($data, 'en');
  530. $beforeFrValue = $getTargetedValue($data, 'fr');
  531. $data->addValue('file2.l1.l2.k2', 'new value', 'fr');
  532. $afterEnValue = $getTargetedValue($data, 'en');
  533. $afterFrValue = $getTargetedValue($data, 'fr');
  534. $this->assertEquals($this->value, $beforeEnValue);
  535. $this->assertEquals($this->value, $beforeFrValue);
  536. $this->assertEquals($this->value, $afterEnValue);
  537. $this->assertInstanceOf(I18nValue::class, $afterFrValue);
  538. $this->assertEquals('new value', $afterFrValue->getValue());
  539. }
  540. public function testRemoveKeyWhenKeyDoesNotExist(): void {
  541. $this->expectException(\Exception::class);
  542. $this->expectExceptionMessage('The selected key does not exist.');
  543. $data = new I18nData($this->referenceData);
  544. $data->removeKey('Unknown key');
  545. }
  546. public function testRemoveKeyWhenKeyHasNoEmptySibling(): void {
  547. $this->expectException(\Exception::class);
  548. $this->expectExceptionMessage('The selected key does not exist.');
  549. $data = new I18nData($this->referenceData);
  550. $data->removeKey('file1.l1.l2');
  551. }
  552. public function testRemoveKeyWhenKeyIsEmptySibling(): void {
  553. $rawData = array_merge($this->referenceData, [
  554. 'fr' => [],
  555. ]);
  556. $data = new I18nData($rawData);
  557. $data->removeKey('file2.l1.l2');
  558. $this->assertEquals([
  559. 'en' => [
  560. 'file1.php' => [
  561. 'file1.l1.l2.k1' => $this->value,
  562. 'file1.l1.l2.k2' => $this->value,
  563. ],
  564. 'file2.php' => [
  565. 'file2.l1.l2.k1' => $this->value,
  566. 'file2.l1.l2.k2' => $this->value,
  567. ],
  568. 'file3.php' => [
  569. 'file3.l1.l2._' => $this->value,
  570. 'file3.l1.l2.k1' => $this->value,
  571. ],
  572. ],
  573. 'fr' => [
  574. 'file1.php' => [
  575. 'file1.l1.l2.k1' => $this->value,
  576. 'file1.l1.l2.k2' => $this->value,
  577. ],
  578. 'file2.php' => [
  579. 'file2.l1.l2.k1' => $this->value,
  580. 'file2.l1.l2.k2' => $this->value,
  581. ],
  582. 'file3.php' => [
  583. 'file3.l1.l2._' => $this->value,
  584. 'file3.l1.l2.k1' => $this->value,
  585. ],
  586. ],
  587. ], $data->getData());
  588. }
  589. public function testRemoveKeyWhenKeyIsTheOnlyChild(): void {
  590. $rawData = array_merge($this->referenceData, [
  591. 'fr' => [],
  592. ]);
  593. $data = new I18nData($rawData);
  594. $data->removeKey('file3.l1.l2.k1');
  595. $this->assertEquals([
  596. 'en' => [
  597. 'file1.php' => [
  598. 'file1.l1.l2.k1' => $this->value,
  599. 'file1.l1.l2.k2' => $this->value,
  600. ],
  601. 'file2.php' => [
  602. 'file2.l1.l2._' => $this->value,
  603. 'file2.l1.l2.k1' => $this->value,
  604. 'file2.l1.l2.k2' => $this->value,
  605. ],
  606. 'file3.php' => [
  607. 'file3.l1.l2' => $this->value,
  608. ],
  609. ],
  610. 'fr' => [
  611. 'file1.php' => [
  612. 'file1.l1.l2.k1' => $this->value,
  613. 'file1.l1.l2.k2' => $this->value,
  614. ],
  615. 'file2.php' => [
  616. 'file2.l1.l2._' => $this->value,
  617. 'file2.l1.l2.k1' => $this->value,
  618. 'file2.l1.l2.k2' => $this->value,
  619. ],
  620. 'file3.php' => [
  621. 'file3.l1.l2' => $this->value,
  622. ],
  623. ],
  624. ], $data->getData());
  625. }
  626. public function testIgnore(): void {
  627. $value = $this->getMockBuilder(I18nValue::class)
  628. ->disableOriginalConstructor()
  629. ->getMock();
  630. $value->expects($this->exactly(2))
  631. ->method('unmarkAsIgnore');
  632. $value->expects($this->once())
  633. ->method('markAsIgnore');
  634. $rawData = array_merge($this->referenceData, [
  635. 'fr' => [
  636. 'file1.php' => [
  637. 'file1.l1.l2.k1' => $value,
  638. ],
  639. ],
  640. ]);
  641. $data = new I18nData($rawData);
  642. $data->ignore('file1.l1.l2.k1', 'fr');
  643. $data->ignore('file1.l1.l2.k1', 'fr', true);
  644. $data->ignore('file1.l1.l2.k1', 'fr', false);
  645. }
  646. public function testIgnoreUnmodified(): void {
  647. $value = $this->getMockBuilder(I18nValue::class)
  648. ->disableOriginalConstructor()
  649. ->getMock();
  650. $value->expects($this->exactly(2))
  651. ->method('unmarkAsIgnore');
  652. $value->expects($this->once())
  653. ->method('markAsIgnore');
  654. $this->value->expects($this->atLeast(2))
  655. ->method('equal')
  656. ->with($value)
  657. ->willReturn(true);
  658. $rawData = array_merge($this->referenceData, [
  659. 'fr' => [
  660. 'file1.php' => [
  661. 'file1.l1.l2.k1' => $value,
  662. ],
  663. ],
  664. ]);
  665. $data = new I18nData($rawData);
  666. $data->ignore_unmodified('fr');
  667. $data->ignore_unmodified('fr', true);
  668. $data->ignore_unmodified('fr', false);
  669. }
  670. public function testGetLanguage(): void {
  671. $rawData = array_merge($this->referenceData, [
  672. 'fr' => [],
  673. 'nl' => [],
  674. ]);
  675. $data = new I18nData($rawData);
  676. $this->assertEquals($this->referenceData['en'], $data->getLanguage('en'));
  677. }
  678. public function testGetReferenceLanguage(): void {
  679. $rawData = array_merge($this->referenceData, [
  680. 'fr' => [],
  681. 'nl' => [],
  682. ]);
  683. $data = new I18nData($rawData);
  684. $this->assertEquals($this->referenceData['en'], $data->getReferenceLanguage());
  685. }
  686. }