Chris@14
|
1 <?php
|
Chris@14
|
2
|
Chris@14
|
3 namespace Drupal\Tests\Component\Gettext;
|
Chris@14
|
4
|
Chris@14
|
5 use Drupal\Component\Gettext\PoItem;
|
Chris@14
|
6 use Drupal\Component\Gettext\PoStreamWriter;
|
Chris@14
|
7 use org\bovigo\vfs\vfsStream;
|
Chris@14
|
8 use org\bovigo\vfs\vfsStreamFile;
|
Chris@14
|
9 use PHPUnit\Framework\TestCase;
|
Chris@14
|
10
|
Chris@14
|
11 /**
|
Chris@14
|
12 * @coversDefaultClass \Drupal\Component\Gettext\PoStreamWriter
|
Chris@14
|
13 * @group Gettext
|
Chris@14
|
14 */
|
Chris@14
|
15 class PoStreamWriterTest extends TestCase {
|
Chris@14
|
16
|
Chris@14
|
17 /**
|
Chris@14
|
18 * The PO writer object under test.
|
Chris@14
|
19 *
|
Chris@14
|
20 * @var \Drupal\Component\Gettext\PoStreamWriter
|
Chris@14
|
21 */
|
Chris@14
|
22 protected $poWriter;
|
Chris@14
|
23
|
Chris@14
|
24 /**
|
Chris@14
|
25 * The mock po file.
|
Chris@14
|
26 *
|
Chris@14
|
27 * @var \org\bovigo\vfs\vfsStreamFile
|
Chris@14
|
28 */
|
Chris@14
|
29 protected $poFile;
|
Chris@14
|
30
|
Chris@14
|
31 /**
|
Chris@14
|
32 * {@inheritdoc}
|
Chris@14
|
33 */
|
Chris@14
|
34 protected function setUp() {
|
Chris@14
|
35 parent::setUp();
|
Chris@14
|
36
|
Chris@14
|
37 $this->poWriter = new PoStreamWriter();
|
Chris@14
|
38
|
Chris@14
|
39 $root = vfsStream::setup();
|
Chris@14
|
40 $this->poFile = new vfsStreamFile('powriter.po');
|
Chris@14
|
41 $root->addChild($this->poFile);
|
Chris@14
|
42 }
|
Chris@14
|
43
|
Chris@14
|
44 /**
|
Chris@14
|
45 * @covers ::getURI
|
Chris@14
|
46 */
|
Chris@14
|
47 public function testGetUriException() {
|
Chris@14
|
48 if (method_exists($this, 'expectException')) {
|
Chris@14
|
49 $this->expectException(\Exception::class, 'No URI set.');
|
Chris@14
|
50 }
|
Chris@14
|
51 else {
|
Chris@14
|
52 $this->setExpectedException(\Exception::class, 'No URI set.');
|
Chris@14
|
53 }
|
Chris@14
|
54
|
Chris@14
|
55 $this->poWriter->getURI();
|
Chris@14
|
56 }
|
Chris@14
|
57
|
Chris@14
|
58 /**
|
Chris@14
|
59 * @covers ::writeItem
|
Chris@14
|
60 * @dataProvider providerWriteData
|
Chris@14
|
61 */
|
Chris@14
|
62 public function testWriteItem($poContent, $expected, $long) {
|
Chris@14
|
63 if ($long) {
|
Chris@14
|
64 if (method_exists($this, 'expectException')) {
|
Chris@14
|
65 $this->expectException(\Exception::class, 'Unable to write data:');
|
Chris@14
|
66 }
|
Chris@14
|
67 else {
|
Chris@14
|
68 $this->setExpectedException(\Exception::class, 'Unable to write data:');
|
Chris@14
|
69 }
|
Chris@14
|
70 }
|
Chris@14
|
71
|
Chris@14
|
72 // Limit the file system quota to make the write fail on long strings.
|
Chris@14
|
73 vfsStream::setQuota(10);
|
Chris@14
|
74
|
Chris@14
|
75 $this->poWriter->setURI($this->poFile->url());
|
Chris@14
|
76 $this->poWriter->open();
|
Chris@14
|
77
|
Chris@14
|
78 $poItem = $this->prophesize(PoItem::class);
|
Chris@14
|
79 $poItem->__toString()->willReturn($poContent);
|
Chris@14
|
80
|
Chris@14
|
81 $this->poWriter->writeItem($poItem->reveal());
|
Chris@14
|
82 $this->poWriter->close();
|
Chris@14
|
83 $this->assertEquals(file_get_contents($this->poFile->url()), $expected);
|
Chris@14
|
84 }
|
Chris@14
|
85
|
Chris@14
|
86 /**
|
Chris@14
|
87 * @return array
|
Chris@14
|
88 * - Content to write.
|
Chris@14
|
89 * - Written content.
|
Chris@14
|
90 * - Content longer than 10 bytes.
|
Chris@14
|
91 */
|
Chris@14
|
92 public function providerWriteData() {
|
Chris@14
|
93 return [
|
Chris@14
|
94 ['', '', FALSE],
|
Chris@14
|
95 ["\r\n", "\r\n", FALSE],
|
Chris@14
|
96 ['write this if you can', 'write this', TRUE],
|
Chris@14
|
97 ['éáíó>&', 'éáíó>&', FALSE],
|
Chris@14
|
98 ['éáíó>&<', 'éáíó>&', TRUE],
|
Chris@14
|
99 ['中文 890', '中文 890', FALSE],
|
Chris@14
|
100 ['中文 89012', '中文 890', TRUE],
|
Chris@14
|
101 ];
|
Chris@14
|
102 }
|
Chris@14
|
103
|
Chris@14
|
104 /**
|
Chris@14
|
105 * @covers ::close
|
Chris@14
|
106 */
|
Chris@14
|
107 public function testCloseException() {
|
Chris@14
|
108 if (method_exists($this, 'expectException')) {
|
Chris@14
|
109 $this->expectException(\Exception::class, 'Cannot close stream that is not open.');
|
Chris@14
|
110 }
|
Chris@14
|
111 else {
|
Chris@14
|
112 $this->setExpectedException(\Exception::class, 'Cannot close stream that is not open.');
|
Chris@14
|
113 }
|
Chris@14
|
114
|
Chris@14
|
115 $this->poWriter->close();
|
Chris@14
|
116 }
|
Chris@14
|
117
|
Chris@14
|
118 }
|