PHPWord/tests/PhpWord/Element/TextRunTest.php

185 lines
5.6 KiB
PHP
Raw Normal View History

2014-03-09 19:09:22 +01:00
<?php
2014-03-27 23:55:06 +07:00
/**
* This file is part of PHPWord - A pure PHP library for reading and writing
* word processing documents.
*
* PHPWord is free software distributed under the terms of the GNU Lesser
* General Public License version 3 as published by the Free Software Foundation.
*
* For the full copyright and license information, please read the LICENSE
* file that was distributed with this source code. For the full list of
* contributors, visit https://github.com/PHPOffice/PHPWord/contributors.
2014-03-27 23:55:06 +07:00
*
* @see https://github.com/PHPOffice/PHPWord
2022-09-16 11:45:45 +02:00
*
2014-05-04 21:03:28 +04:00
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
2014-03-27 23:55:06 +07:00
*/
2015-11-15 13:33:05 +04:00
namespace PhpOffice\PhpWord\Element;
2014-03-09 19:09:22 +01:00
use PhpOffice\PhpWord\PhpWord;
use PhpOffice\PhpWord\SimpleType\Jc;
use PhpOffice\PhpWord\Style\Paragraph;
2014-03-09 19:09:22 +01:00
2014-03-27 23:55:06 +07:00
/**
2022-09-16 11:45:45 +02:00
* Test class for PhpOffice\PhpWord\Element\TextRun.
2014-03-27 23:55:06 +07:00
*
* @runTestsInSeparateProcesses
*/
class TextRunTest extends \PHPUnit\Framework\TestCase
2014-03-09 15:04:23 -04:00
{
2014-03-30 11:09:14 +07:00
/**
2022-09-16 11:45:45 +02:00
* New instance.
2014-03-30 11:09:14 +07:00
*/
2022-09-16 11:45:45 +02:00
public function testConstruct(): void
2014-03-09 15:04:23 -04:00
{
$oTextRun = new TextRun();
2014-03-09 15:04:23 -04:00
2022-09-16 11:45:45 +02:00
self::assertInstanceOf('PhpOffice\\PhpWord\\Element\\TextRun', $oTextRun);
self::assertCount(0, $oTextRun->getElements());
self::assertInstanceOf('PhpOffice\\PhpWord\\Style\\Paragraph', $oTextRun->getParagraphStyle());
2014-03-09 15:04:23 -04:00
}
2014-03-30 11:09:14 +07:00
/**
2022-09-16 11:45:45 +02:00
* New instance with string.
2014-03-30 11:09:14 +07:00
*/
2022-09-16 11:45:45 +02:00
public function testConstructString(): void
2014-03-09 15:04:23 -04:00
{
$oTextRun = new TextRun('pStyle');
2014-03-09 15:04:23 -04:00
2022-09-16 11:45:45 +02:00
self::assertInstanceOf('PhpOffice\\PhpWord\\Element\\TextRun', $oTextRun);
self::assertCount(0, $oTextRun->getElements());
self::assertEquals('pStyle', $oTextRun->getParagraphStyle());
2014-03-09 15:04:23 -04:00
}
2014-03-30 11:09:14 +07:00
/**
2022-09-16 11:45:45 +02:00
* New instance with array.
2014-03-30 11:09:14 +07:00
*/
2022-09-16 11:45:45 +02:00
public function testConstructArray(): void
2014-03-09 15:04:23 -04:00
{
2022-09-16 11:45:45 +02:00
$oTextRun = new TextRun(['spacing' => 100]);
2014-03-09 15:04:23 -04:00
2022-09-16 11:45:45 +02:00
self::assertInstanceOf('PhpOffice\\PhpWord\\Element\\TextRun', $oTextRun);
self::assertCount(0, $oTextRun->getElements());
self::assertInstanceOf('PhpOffice\\PhpWord\\Style\\Paragraph', $oTextRun->getParagraphStyle());
2014-03-09 15:04:23 -04:00
}
/**
2022-09-16 11:45:45 +02:00
* New instance with object.
*/
2022-09-16 11:45:45 +02:00
public function testConstructObject(): void
{
$oParagraphStyle = new Paragraph();
$oParagraphStyle->setAlignment(Jc::BOTH);
$oTextRun = new TextRun($oParagraphStyle);
2022-09-16 11:45:45 +02:00
self::assertInstanceOf('PhpOffice\\PhpWord\\Element\\TextRun', $oTextRun);
self::assertCount(0, $oTextRun->getElements());
self::assertInstanceOf('PhpOffice\\PhpWord\\Style\\Paragraph', $oTextRun->getParagraphStyle());
self::assertEquals(Jc::BOTH, $oTextRun->getParagraphStyle()->getAlignment());
}
2014-03-30 11:09:14 +07:00
/**
2022-09-16 11:45:45 +02:00
* Add text.
2014-03-30 11:09:14 +07:00
*/
2022-09-16 11:45:45 +02:00
public function testAddText(): void
2014-03-09 15:04:23 -04:00
{
$oTextRun = new TextRun();
2016-06-04 20:06:37 +04:00
$element = $oTextRun->addText('text');
2014-03-09 15:04:23 -04:00
2022-09-16 11:45:45 +02:00
self::assertInstanceOf('PhpOffice\\PhpWord\\Element\\Text', $element);
self::assertCount(1, $oTextRun->getElements());
self::assertEquals('text', $element->getText());
2014-03-09 15:04:23 -04:00
}
2014-03-30 11:09:14 +07:00
/**
2022-09-16 11:45:45 +02:00
* Add text non-UTF8.
2014-03-30 11:09:14 +07:00
*/
2022-09-16 11:45:45 +02:00
public function testAddTextNotUTF8(): void
2014-03-09 15:04:23 -04:00
{
$oTextRun = new TextRun();
2016-06-04 20:06:37 +04:00
$element = $oTextRun->addText(utf8_decode('ééé'));
2014-03-09 15:04:23 -04:00
2022-09-16 11:45:45 +02:00
self::assertInstanceOf('PhpOffice\\PhpWord\\Element\\Text', $element);
self::assertCount(1, $oTextRun->getElements());
self::assertEquals('ééé', $element->getText());
2014-03-09 15:04:23 -04:00
}
2014-03-30 11:09:14 +07:00
/**
2022-09-16 11:45:45 +02:00
* Add link.
2014-03-30 11:09:14 +07:00
*/
2022-09-16 11:45:45 +02:00
public function testAddLink(): void
2014-03-09 15:04:23 -04:00
{
$oTextRun = new TextRun();
$element = $oTextRun->addLink('https://github.com/PHPOffice/PHPWord');
2014-03-09 15:04:23 -04:00
2022-09-16 11:45:45 +02:00
self::assertInstanceOf('PhpOffice\\PhpWord\\Element\\Link', $element);
self::assertCount(1, $oTextRun->getElements());
self::assertEquals('https://github.com/PHPOffice/PHPWord', $element->getSource());
2014-03-09 15:04:23 -04:00
}
2014-03-30 11:09:14 +07:00
/**
2022-09-16 11:45:45 +02:00
* Add link with name.
2014-03-30 11:09:14 +07:00
*/
2022-09-16 11:45:45 +02:00
public function testAddLinkWithName(): void
2014-03-09 15:04:23 -04:00
{
$oTextRun = new TextRun();
2016-06-04 20:06:37 +04:00
$element = $oTextRun->addLink('https://github.com/PHPOffice/PHPWord', 'PHPWord on GitHub');
2014-03-09 15:04:23 -04:00
2022-09-16 11:45:45 +02:00
self::assertInstanceOf('PhpOffice\\PhpWord\\Element\\Link', $element);
self::assertCount(1, $oTextRun->getElements());
self::assertEquals('https://github.com/PHPOffice/PHPWord', $element->getSource());
self::assertEquals('PHPWord on GitHub', $element->getText());
2014-03-09 15:04:23 -04:00
}
2014-03-30 11:09:14 +07:00
/**
2022-09-16 11:45:45 +02:00
* Add text break.
2014-03-30 11:09:14 +07:00
*/
2022-09-16 11:45:45 +02:00
public function testAddTextBreak(): void
2014-03-30 11:09:14 +07:00
{
$oTextRun = new TextRun();
$oTextRun->addTextBreak(2);
2014-03-30 11:09:14 +07:00
2022-09-16 11:45:45 +02:00
self::assertCount(2, $oTextRun->getElements());
2014-03-30 11:09:14 +07:00
}
/**
2022-09-16 11:45:45 +02:00
* Add image.
2014-03-30 11:09:14 +07:00
*/
2022-09-16 11:45:45 +02:00
public function testAddImage(): void
2014-03-09 15:04:23 -04:00
{
$src = __DIR__ . '/../_files/images/earth.jpg';
2014-03-23 12:37:31 -04:00
$oTextRun = new TextRun();
2014-03-09 15:04:23 -04:00
$element = $oTextRun->addImage($src);
2022-09-16 11:45:45 +02:00
self::assertInstanceOf('PhpOffice\\PhpWord\\Element\\Image', $element);
self::assertCount(1, $oTextRun->getElements());
2014-03-09 15:04:23 -04:00
}
2014-03-30 11:09:14 +07:00
/**
2022-09-16 11:45:45 +02:00
* Add footnote.
2014-03-30 11:09:14 +07:00
*/
2022-09-16 11:45:45 +02:00
public function testCreateFootnote(): void
2014-03-09 15:04:23 -04:00
{
$oTextRun = new TextRun();
$oTextRun->setPhpWord(new PhpWord());
$element = $oTextRun->addFootnote();
2014-03-09 15:04:23 -04:00
2022-09-16 11:45:45 +02:00
self::assertInstanceOf('PhpOffice\\PhpWord\\Element\\Footnote', $element);
self::assertCount(1, $oTextRun->getElements());
2014-03-09 15:04:23 -04:00
}
/**
2022-09-16 11:45:45 +02:00
* Get paragraph style.
*/
2022-09-16 11:45:45 +02:00
public function testParagraph(): void
{
$oText = new TextRun('paragraphStyle');
2022-09-16 11:45:45 +02:00
self::assertEquals('paragraphStyle', $oText->getParagraphStyle());
2022-09-16 11:45:45 +02:00
$oText->setParagraphStyle(['alignment' => Jc::CENTER, 'spaceAfter' => 100]);
self::assertInstanceOf('PhpOffice\\PhpWord\\Style\\Paragraph', $oText->getParagraphStyle());
}
}