PHPWord/tests/PhpWord/Element/TextRunTest.php

156 lines
4.5 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
*
* @link https://github.com/PHPOffice/PHPWord
2016-07-31 12:35:06 +04:00
* @copyright 2010-2016 PHPWord contributors
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;
2014-03-09 19:09:22 +01:00
2014-03-27 23:55:06 +07:00
/**
* Test class for PhpOffice\PhpWord\Element\TextRun
2014-03-27 23:55:06 +07:00
*
* @runTestsInSeparateProcesses
*/
2014-03-09 15:04:23 -04:00
class TextRunTest extends \PHPUnit_Framework_TestCase
{
2014-03-30 11:09:14 +07:00
/**
* New instance
*/
2014-03-09 15:04:23 -04:00
public function testConstructNull()
{
$oTextRun = new TextRun();
2014-03-09 15:04:23 -04:00
$this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\TextRun', $oTextRun);
2014-03-09 15:04:23 -04:00
$this->assertCount(0, $oTextRun->getElements());
$this->assertNull($oTextRun->getParagraphStyle());
2014-03-09 15:04:23 -04:00
}
2014-03-30 11:09:14 +07:00
/**
* New instance with string
*/
2014-03-09 15:04:23 -04:00
public function testConstructString()
{
$oTextRun = new TextRun('pStyle');
2014-03-09 15:04:23 -04:00
$this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\TextRun', $oTextRun);
2014-03-09 15:04:23 -04:00
$this->assertCount(0, $oTextRun->getElements());
$this->assertEquals('pStyle', $oTextRun->getParagraphStyle());
2014-03-09 15:04:23 -04:00
}
2014-03-30 11:09:14 +07:00
/**
* New instance with array
*/
2014-03-09 15:04:23 -04:00
public function testConstructArray()
{
$oTextRun = new TextRun(array('spacing' => 100));
2014-03-09 15:04:23 -04:00
$this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\TextRun', $oTextRun);
2014-03-09 15:04:23 -04:00
$this->assertCount(0, $oTextRun->getElements());
$this->assertInstanceOf('PhpOffice\\PhpWord\\Style\\Paragraph', $oTextRun->getParagraphStyle());
2014-03-09 15:04:23 -04:00
}
2014-03-30 11:09:14 +07:00
/**
* Add text
*/
2014-03-09 15:04:23 -04:00
public function testAddText()
{
$oTextRun = new TextRun();
2016-06-04 20:06:37 +04:00
$element = $oTextRun->addText('text');
2014-03-09 15:04:23 -04:00
$this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\Text', $element);
2014-03-09 15:04:23 -04:00
$this->assertCount(1, $oTextRun->getElements());
2016-06-04 20:06:37 +04:00
$this->assertEquals('text', $element->getText());
2014-03-09 15:04:23 -04:00
}
2014-03-30 11:09:14 +07:00
/**
* Add text non-UTF8
*/
2014-03-09 15:04:23 -04:00
public function testAddTextNotUTF8()
{
$oTextRun = new TextRun();
2016-06-04 20:06:37 +04:00
$element = $oTextRun->addText(utf8_decode('ééé'));
2014-03-09 15:04:23 -04:00
$this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\Text', $element);
2014-03-09 15:04:23 -04:00
$this->assertCount(1, $oTextRun->getElements());
2016-06-04 20:06:37 +04:00
$this->assertEquals('ééé', $element->getText());
2014-03-09 15:04:23 -04:00
}
2014-03-30 11:09:14 +07:00
/**
* Add link
*/
2014-03-09 15:04:23 -04:00
public function testAddLink()
{
$oTextRun = new TextRun();
$element = $oTextRun->addLink('https://github.com/PHPOffice/PHPWord');
2014-03-09 15:04:23 -04:00
$this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\Link', $element);
2014-03-09 15:04:23 -04:00
$this->assertCount(1, $oTextRun->getElements());
$this->assertEquals('https://github.com/PHPOffice/PHPWord', $element->getSource());
2014-03-09 15:04:23 -04:00
}
2014-03-30 11:09:14 +07:00
/**
* Add link with name
*/
2014-03-09 15:04:23 -04:00
public function testAddLinkWithName()
{
$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
$this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\Link', $element);
2014-03-09 15:04:23 -04:00
$this->assertCount(1, $oTextRun->getElements());
$this->assertEquals('https://github.com/PHPOffice/PHPWord', $element->getSource());
2016-06-04 20:06:37 +04:00
$this->assertEquals('PHPWord on GitHub', $element->getText());
2014-03-09 15:04:23 -04:00
}
2014-03-30 11:09:14 +07:00
/**
* Add text break
*/
public function testAddTextBreak()
{
$oTextRun = new TextRun();
$oTextRun->addTextBreak(2);
2014-03-30 11:09:14 +07:00
$this->assertCount(2, $oTextRun->getElements());
}
/**
* Add image
*/
2014-03-09 15:04:23 -04:00
public function testAddImage()
{
$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);
$this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\Image', $element);
2014-03-09 15:04:23 -04:00
$this->assertCount(1, $oTextRun->getElements());
}
2014-03-30 11:09:14 +07:00
/**
* Add footnote
*/
2014-03-09 15:04:23 -04:00
public function testCreateFootnote()
{
$oTextRun = new TextRun();
$oTextRun->setPhpWord(new PhpWord());
$element = $oTextRun->addFootnote();
2014-03-09 15:04:23 -04:00
$this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\Footnote', $element);
2014-03-09 15:04:23 -04:00
$this->assertCount(1, $oTextRun->getElements());
}
}