I18nDataTest.php 20 KB

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