PHPWord/tests/PhpWord/Tests/Element/TextRunTest.php

149 lines
4.0 KiB
PHP
Raw Normal View History

2014-03-09 19:09:22 +01:00
<?php
2014-03-27 23:55:06 +07:00
/**
* PHPWord
*
* @link https://github.com/PHPOffice/PHPWord
* @copyright 2014 PHPWord
* @license http://www.gnu.org/licenses/lgpl.txt LGPL
2014-03-27 23:55:06 +07:00
*/
namespace PhpOffice\PhpWord\Tests\Element;
2014-03-09 19:09:22 +01:00
use PhpOffice\PhpWord\PhpWord;
use PhpOffice\PhpWord\Element\TextRun;
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->assertEquals($oTextRun->getParagraphStyle(), null);
}
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($oTextRun->getParagraphStyle(), 'pStyle');
}
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();
2014-03-09 15:04:23 -04:00
$element = $oTextRun->addText('text');
$this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\Text', $element);
2014-03-09 15:04:23 -04:00
$this->assertCount(1, $oTextRun->getElements());
$this->assertEquals($element->getText(), 'text');
}
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();
2014-03-09 15:04:23 -04:00
$element = $oTextRun->addText(utf8_decode('ééé'));
$this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\Text', $element);
2014-03-09 15:04:23 -04:00
$this->assertCount(1, $oTextRun->getElements());
$this->assertEquals($element->getText(), 'ééé');
}
2014-03-30 11:09:14 +07:00
/**
* Add link
*/
2014-03-09 15:04:23 -04:00
public function testAddLink()
{
$oTextRun = new TextRun();
2014-03-09 15:04:23 -04:00
$element = $oTextRun->addLink('http://www.google.fr');
$this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\Link', $element);
2014-03-09 15:04:23 -04:00
$this->assertCount(1, $oTextRun->getElements());
2014-04-26 16:05:29 +07:00
$this->assertEquals($element->getTarget(), 'http://www.google.fr');
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();
2014-03-09 15:04:23 -04:00
$element = $oTextRun->addLink('http://www.google.fr', utf8_decode('ééé'));
$this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\Link', $element);
2014-03-09 15:04:23 -04:00
$this->assertCount(1, $oTextRun->getElements());
2014-04-26 16:05:29 +07:00
$this->assertEquals($element->getTarget(), 'http://www.google.fr');
$this->assertEquals($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();
$element = $oTextRun->addTextBreak(2);
$this->assertCount(2, $oTextRun->getElements());
}
/**
* Add image
*/
2014-03-09 15:04:23 -04:00
public function testAddImage()
{
2014-03-23 12:37:31 -04:00
$src = __DIR__ . "/../_files/images/earth.jpg";
$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());
}
}