View source
<?php
namespace Drupal\Tests\Component\Gettext;
use Drupal\Component\Gettext\PoItem;
use Drupal\Component\Gettext\PoStreamWriter;
use Drupal\Tests\PhpUnitCompatibilityTrait;
use org\bovigo\vfs\vfsStream;
use org\bovigo\vfs\vfsStreamFile;
use PHPUnit\Framework\TestCase;
class PoStreamWriterTest extends TestCase {
use PhpUnitCompatibilityTrait;
protected $poWriter;
protected $poFile;
protected function setUp() : void {
parent::setUp();
$this->poWriter = new PoStreamWriter();
$root = vfsStream::setup();
$this->poFile = new vfsStreamFile('powriter.po');
$root
->addChild($this->poFile);
}
public function testGetUriException() {
$this
->expectException(\Exception::class, 'No URI set.');
$this->poWriter
->getURI();
}
public function testWriteItem($poContent, $expected, $long) {
if ($long) {
$this
->expectException(\Exception::class, 'Unable to write data:');
}
vfsStream::setQuota(10);
$this->poWriter
->setURI($this->poFile
->url());
$this->poWriter
->open();
$poItem = $this
->prophesize(PoItem::class);
$poItem
->__toString()
->willReturn($poContent);
$this->poWriter
->writeItem($poItem
->reveal());
$this->poWriter
->close();
$this
->assertEquals(file_get_contents($this->poFile
->url()), $expected);
}
public function providerWriteData() {
return [
[
'',
'',
FALSE,
],
[
"\r\n",
"\r\n",
FALSE,
],
[
'write this if you can',
'write this',
TRUE,
],
[
'éáíó>&',
'éáíó>&',
FALSE,
],
[
'éáíó>&<',
'éáíó>&',
TRUE,
],
[
'中文 890',
'中文 890',
FALSE,
],
[
'中文 89012',
'中文 890',
TRUE,
],
];
}
public function testCloseException() {
$this
->expectException(\Exception::class, 'Cannot close stream that is not open.');
$this->poWriter
->close();
}
}