2014-03-09 19:09:22 +01:00
|
|
|
<?php
|
2014-03-27 23:55:06 +07:00
|
|
|
/**
|
2014-05-05 13:06:53 +04: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
|
|
|
*
|
2017-11-04 22:44:12 +01: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
|
|
|
*/
|
|
|
|
|
|
2022-09-16 14:09:17 +02:00
|
|
|
namespace PhpOffice\PhpWordTests\Element;
|
2014-03-09 19:09:22 +01:00
|
|
|
|
2022-09-16 14:09:17 +02:00
|
|
|
use PhpOffice\PhpWord\Element\TextRun;
|
2014-05-05 12:38:31 +04:00
|
|
|
use PhpOffice\PhpWord\PhpWord;
|
2017-12-21 00:03:52 +01:00
|
|
|
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
|
|
|
|
|
*/
|
2017-11-09 06:36:47 -02:00
|
|
|
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
|
|
|
{
|
2014-03-18 17:26:03 +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
|
|
|
{
|
2014-03-18 17:26:03 +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
|
|
|
}
|
|
|
|
|
|
2017-12-21 00:03:52 +01:00
|
|
|
/**
|
2022-09-16 11:45:45 +02:00
|
|
|
* New instance with object.
|
2017-12-21 00:03:52 +01:00
|
|
|
*/
|
2022-09-16 11:45:45 +02:00
|
|
|
public function testConstructObject(): void
|
2017-12-21 00:03:52 +01:00
|
|
|
{
|
|
|
|
|
$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());
|
2017-12-21 00:03:52 +01:00
|
|
|
}
|
|
|
|
|
|
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
|
|
|
{
|
2014-03-18 17:26:03 +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
|
|
|
{
|
2014-03-18 17:26:03 +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
|
|
|
{
|
2014-03-18 17:26:03 +04:00
|
|
|
$oTextRun = new TextRun();
|
2015-02-06 22:28:31 +04:00
|
|
|
$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
|
|
|
{
|
2014-03-18 17:26:03 +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();
|
2014-05-04 22:53:56 +07:00
|
|
|
$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
|
|
|
{
|
2015-02-06 22:28:31 +04:00
|
|
|
$src = __DIR__ . '/../_files/images/earth.jpg';
|
2014-03-23 12:37:31 -04:00
|
|
|
|
2014-03-18 17:26:03 +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
|
|
|
{
|
2014-03-18 17:26:03 +04:00
|
|
|
$oTextRun = new TextRun();
|
2014-05-04 00:57:44 +07:00
|
|
|
$oTextRun->setPhpWord(new PhpWord());
|
2014-04-02 11:02:56 +07:00
|
|
|
$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
|
|
|
}
|
2017-12-21 00:03:52 +01:00
|
|
|
|
|
|
|
|
/**
|
2022-09-16 11:45:45 +02:00
|
|
|
* Get paragraph style.
|
2017-12-21 00:03:52 +01:00
|
|
|
*/
|
2022-09-16 11:45:45 +02:00
|
|
|
public function testParagraph(): void
|
2017-12-21 00:03:52 +01:00
|
|
|
{
|
|
|
|
|
$oText = new TextRun('paragraphStyle');
|
2022-09-16 11:45:45 +02:00
|
|
|
self::assertEquals('paragraphStyle', $oText->getParagraphStyle());
|
2017-12-21 00:03:52 +01:00
|
|
|
|
2022-09-16 11:45:45 +02:00
|
|
|
$oText->setParagraphStyle(['alignment' => Jc::CENTER, 'spaceAfter' => 100]);
|
|
|
|
|
self::assertInstanceOf('PhpOffice\\PhpWord\\Style\\Paragraph', $oText->getParagraphStyle());
|
2017-12-21 00:03:52 +01:00
|
|
|
}
|
2014-03-11 21:44:54 +07:00
|
|
|
}
|