class PoStreamWriterTest

Same name in other branches
  1. 9 core/tests/Drupal/Tests/Component/Gettext/PoStreamWriterTest.php \Drupal\Tests\Component\Gettext\PoStreamWriterTest
  2. 8.9.x core/tests/Drupal/Tests/Component/Gettext/PoStreamWriterTest.php \Drupal\Tests\Component\Gettext\PoStreamWriterTest
  3. 11.x core/tests/Drupal/Tests/Component/Gettext/PoStreamWriterTest.php \Drupal\Tests\Component\Gettext\PoStreamWriterTest

@coversDefaultClass \Drupal\Component\Gettext\PoStreamWriter @group Gettext

Hierarchy

  • class \Drupal\Tests\Component\Gettext\PoStreamWriterTest extends \PHPUnit\Framework\TestCase uses \Prophecy\PhpUnit\ProphecyTrait

Expanded class hierarchy of PoStreamWriterTest

File

core/tests/Drupal/Tests/Component/Gettext/PoStreamWriterTest.php, line 19

Namespace

Drupal\Tests\Component\Gettext
View source
class PoStreamWriterTest extends TestCase {
    use ProphecyTrait;
    
    /**
     * The PO writer object under test.
     *
     * @var \Drupal\Component\Gettext\PoStreamWriter
     */
    protected $poWriter;
    
    /**
     * The mock po file.
     *
     * @var \org\bovigo\vfs\vfsStreamFile
     */
    protected $poFile;
    
    /**
     * {@inheritdoc}
     */
    protected function setUp() : void {
        parent::setUp();
        $poHeader = $this->prophesize(PoHeader::class);
        $poHeader->__toString()
            ->willReturn('');
        $this->poWriter = new PoStreamWriter();
        $this->poWriter
            ->setHeader($poHeader->reveal());
        $root = vfsStream::setup();
        $this->poFile = new vfsStreamFile('poWriter.po');
        $root->addChild($this->poFile);
    }
    
    /**
     * @covers ::getURI
     */
    public function testGetUriException() : void {
        $this->expectException(\Exception::class);
        $this->expectExceptionMessage('No URI set.');
        $this->poWriter
            ->getURI();
    }
    
    /**
     * @covers ::writeItem
     * @dataProvider providerWriteData
     */
    public function testWriteItem($poContent, $expected, $long) : void {
        if ($long) {
            $this->expectException(\Exception::class);
            $this->expectExceptionMessage('Unable to write data:');
        }
        // Limit the file system quota to make the write fail on long strings.
        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);
    }
    
    /**
     * @return array
     *   - Content to write.
     *   - Written content.
     *   - Content longer than 10 bytes.
     */
    public static function providerWriteData() {
        // cSpell:disable
        return [
            [
                '',
                '',
                FALSE,
            ],
            [
                "\r\n",
                "\r\n",
                FALSE,
            ],
            [
                'write this if you can',
                'write this',
                TRUE,
            ],
            [
                'éáíó>&',
                'éáíó>&',
                FALSE,
            ],
            [
                'éáíó>&<',
                'éáíó>&',
                TRUE,
            ],
            [
                '中文 890',
                '中文 890',
                FALSE,
            ],
            [
                '中文 89012',
                '中文 890',
                TRUE,
            ],
        ];
        // cSpell:enable
    }
    
    /**
     * @covers ::close
     */
    public function testCloseException() : void {
        $this->expectException(\Exception::class);
        $this->expectExceptionMessage('Cannot close stream that is not open.');
        $this->poWriter
            ->close();
    }

}

Members

Title Sort descending Modifiers Object type Summary
PoStreamWriterTest::$poFile protected property The mock po file.
PoStreamWriterTest::$poWriter protected property The PO writer object under test.
PoStreamWriterTest::providerWriteData public static function
PoStreamWriterTest::setUp protected function
PoStreamWriterTest::testCloseException public function @covers ::close
PoStreamWriterTest::testGetUriException public function @covers ::getURI
PoStreamWriterTest::testWriteItem public function @covers ::writeItem
@dataProvider providerWriteData

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.