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