3 namespace Drupal\Tests\Component\Gettext;
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;
12 * @coversDefaultClass \Drupal\Component\Gettext\PoStreamWriter
15 class PoStreamWriterTest extends TestCase {
18 * The PO writer object under test.
20 * @var \Drupal\Component\Gettext\PoStreamWriter
27 * @var \org\bovigo\vfs\vfsStreamFile
34 protected function setUp() {
37 $this->poWriter = new PoStreamWriter();
39 $root = vfsStream::setup();
40 $this->poFile = new vfsStreamFile('powriter.po');
41 $root->addChild($this->poFile);
47 public function testGetUriException() {
48 if (method_exists($this, 'expectException')) {
49 $this->expectException(\Exception::class, 'No URI set.');
52 $this->setExpectedException(\Exception::class, 'No URI set.');
55 $this->poWriter->getURI();
60 * @dataProvider providerWriteData
62 public function testWriteItem($poContent, $expected, $long) {
64 if (method_exists($this, 'expectException')) {
65 $this->expectException(\Exception::class, 'Unable to write data:');
68 $this->setExpectedException(\Exception::class, 'Unable to write data:');
72 // Limit the file system quota to make the write fail on long strings.
73 vfsStream::setQuota(10);
75 $this->poWriter->setURI($this->poFile->url());
76 $this->poWriter->open();
78 $poItem = $this->prophesize(PoItem::class);
79 $poItem->__toString()->willReturn($poContent);
81 $this->poWriter->writeItem($poItem->reveal());
82 $this->poWriter->close();
83 $this->assertEquals(file_get_contents($this->poFile->url()), $expected);
90 * - Content longer than 10 bytes.
92 public function providerWriteData() {
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],
107 public function testCloseException() {
108 if (method_exists($this, 'expectException')) {
109 $this->expectException(\Exception::class, 'Cannot close stream that is not open.');
112 $this->setExpectedException(\Exception::class, 'Cannot close stream that is not open.');
115 $this->poWriter->close();