276 lines
7.4 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
* @copyright 2010-2014 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
*/
namespace PhpOffice\PhpWord\Tests\Element;
2014-03-09 19:09:22 +01:00
use PhpOffice\PhpWord\Element\Cell;
2014-03-09 19:09:22 +01:00
2014-03-27 23:55:06 +07:00
/**
* Test class for PhpOffice\PhpWord\Element\Cell
2014-03-27 23:55:06 +07:00
*
* @runTestsInSeparateProcesses
*/
2014-03-09 15:04:23 -04:00
class CellTest extends \PHPUnit_Framework_TestCase
{
2014-03-30 11:09:14 +07:00
/**
* New instance
*/
2014-03-09 15:04:23 -04:00
public function testConstruct()
{
2014-05-11 22:54:51 +07:00
$oCell = new Cell();
2014-03-09 15:04:23 -04:00
$this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\Cell', $oCell);
2014-03-09 15:04:23 -04:00
$this->assertEquals($oCell->getWidth(), null);
}
2014-03-30 11:09:14 +07:00
/**
* New instance with array
*/
2014-03-09 15:04:23 -04:00
public function testConstructWithStyleArray()
{
2014-05-11 22:54:51 +07:00
$oCell = new Cell(null, array('valign' => 'center'));
2014-03-09 15:04:23 -04:00
$this->assertInstanceOf('PhpOffice\\PhpWord\\Style\\Cell', $oCell->getStyle());
2014-03-09 15:04:23 -04:00
$this->assertEquals($oCell->getWidth(), null);
}
2014-03-30 11:09:14 +07:00
/**
* Add text
*/
2014-03-09 15:04:23 -04:00
public function testAddText()
{
2014-05-11 22:54:51 +07:00
$oCell = new Cell();
2014-03-09 15:04:23 -04:00
$element = $oCell->addText('text');
$this->assertCount(1, $oCell->getElements());
$this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\Text', $element);
2014-03-09 15:04:23 -04:00
}
2014-03-30 11:09:14 +07:00
/**
* Add non-UTF8
*/
2014-03-09 15:04:23 -04:00
public function testAddTextNotUTF8()
{
2014-05-11 22:54:51 +07:00
$oCell = new Cell();
2014-03-09 15:04:23 -04:00
$element = $oCell->addText(utf8_decode('ééé'));
$this->assertCount(1, $oCell->getElements());
$this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\Text', $element);
2014-03-09 15:04:23 -04:00
$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()
{
2014-05-11 22:54:51 +07:00
$oCell = new Cell();
2014-03-30 11:09:14 +07:00
$element = $oCell->addLink(utf8_decode('ééé'), utf8_decode('ééé'));
2014-03-09 15:04:23 -04:00
$this->assertCount(1, $oCell->getElements());
$this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\Link', $element);
2014-03-09 15:04:23 -04:00
}
2014-03-30 11:09:14 +07:00
/**
* Add text break
*/
2014-03-09 15:04:23 -04:00
public function testAddTextBreak()
{
2014-05-11 22:54:51 +07:00
$oCell = new Cell();
2014-03-09 15:04:23 -04:00
$oCell->addTextBreak();
$this->assertCount(1, $oCell->getElements());
}
2014-03-30 11:09:14 +07:00
/**
* Add list item
*/
2014-03-09 15:04:23 -04:00
public function testAddListItem()
{
2014-05-11 22:54:51 +07:00
$oCell = new Cell();
2014-03-09 15:04:23 -04:00
$element = $oCell->addListItem('text');
$this->assertCount(1, $oCell->getElements());
$this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\ListItem', $element);
2014-03-09 15:04:23 -04:00
$this->assertEquals($element->getTextObject()->getText(), 'text');
}
2014-03-30 11:09:14 +07:00
/**
* Add list item non-UTF8
*/
2014-03-09 15:04:23 -04:00
public function testAddListItemNotUTF8()
{
2014-05-11 22:54:51 +07:00
$oCell = new Cell();
2014-03-09 15:04:23 -04:00
$element = $oCell->addListItem(utf8_decode('ééé'));
$this->assertCount(1, $oCell->getElements());
$this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\ListItem', $element);
2014-03-09 15:04:23 -04:00
$this->assertEquals($element->getTextObject()->getText(), 'ééé');
}
2014-03-30 11:09:14 +07:00
/**
* Add image section
*/
2014-03-09 15:04:23 -04:00
public function testAddImageSection()
{
$src = __DIR__ . "/../_files/images/earth.jpg";
2014-05-11 22:54:51 +07:00
$oCell = new Cell();
$element = $oCell->addImage($src);
2014-03-09 15:04:23 -04:00
$this->assertCount(1, $oCell->getElements());
$this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\Image', $element);
2014-03-09 15:04:23 -04:00
}
2014-03-30 11:09:14 +07:00
/**
* Add image header
*/
2014-03-09 15:04:23 -04:00
public function testAddImageHeader()
{
$src = __DIR__ . "/../_files/images/earth.jpg";
$oCell = new Cell('header', 1);
2014-03-09 15:04:23 -04:00
$element = $oCell->addImage($src);
$this->assertCount(1, $oCell->getElements());
$this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\Image', $element);
2014-03-09 15:04:23 -04:00
}
2014-03-30 11:09:14 +07:00
/**
* Add image footer
*/
2014-03-09 15:04:23 -04:00
public function testAddImageFooter()
{
$src = __DIR__ . "/../_files/images/earth.jpg";
$oCell = new Cell('footer', 1);
2014-03-09 15:04:23 -04:00
$element = $oCell->addImage($src);
$this->assertCount(1, $oCell->getElements());
$this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\Image', $element);
2014-03-09 15:04:23 -04:00
}
2014-03-30 11:09:14 +07:00
/**
* Add image section by URL
*/
public function testAddImageSectionByUrl()
2014-03-09 15:04:23 -04:00
{
2014-05-11 22:54:51 +07:00
$oCell = new Cell();
$element = $oCell->addImage(
'https://assets.mozillalabs.com/Brands-Logos/Thunderbird/logo-only/thunderbird_logo-only_RGB.png'
);
2014-03-09 15:04:23 -04:00
$this->assertCount(1, $oCell->getElements());
$this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\Image', $element);
2014-03-09 15:04:23 -04:00
}
2014-03-30 11:09:14 +07:00
/**
* Add object
*/
2014-03-09 15:04:23 -04:00
public function testAddObjectXLS()
{
$src = __DIR__ . "/../_files/documents/sheet.xls";
2014-05-11 22:54:51 +07:00
$oCell = new Cell();
2014-03-09 15:04:23 -04:00
$element = $oCell->addObject($src);
$this->assertCount(1, $oCell->getElements());
$this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\Object', $element);
2014-03-09 15:04:23 -04:00
}
2014-03-30 11:09:14 +07:00
/**
* Test add object exception
*
* @expectedException \PhpOffice\PhpWord\Exception\InvalidObjectException
2014-03-30 11:09:14 +07:00
*/
public function testAddObjectException()
{
$src = __DIR__ . "/../_files/xsl/passthrough.xsl";
2014-05-11 22:54:51 +07:00
$oCell = new Cell();
$oCell->addObject($src);
2014-03-30 11:09:14 +07:00
}
/**
* Add preserve text
*/
2014-03-09 15:04:23 -04:00
public function testAddPreserveText()
{
2014-05-11 22:54:51 +07:00
$oCell = new Cell();
$oCell->setDocPart('Header', 1);
2014-03-09 15:04:23 -04:00
$element = $oCell->addPreserveText('text');
$this->assertCount(1, $oCell->getElements());
$this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\PreserveText', $element);
2014-03-09 15:04:23 -04:00
}
2014-03-30 11:09:14 +07:00
/**
* Add preserve text non-UTF8
*/
2014-03-09 15:04:23 -04:00
public function testAddPreserveTextNotUTF8()
{
2014-05-11 22:54:51 +07:00
$oCell = new Cell();
$oCell->setDocPart('Header', 1);
2014-03-09 15:04:23 -04:00
$element = $oCell->addPreserveText(utf8_decode('ééé'));
$this->assertCount(1, $oCell->getElements());
$this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\PreserveText', $element);
$this->assertEquals($element->getText(), array('ééé'));
2014-03-09 15:04:23 -04:00
}
2014-03-30 11:09:14 +07:00
/**
* Add preserve text exception
*
* @expectedException \BadMethodCallException
2014-03-30 11:09:14 +07:00
*/
public function testAddPreserveTextException()
{
2014-05-11 22:54:51 +07:00
$oCell = new Cell();
$oCell->setDocPart('Section', 1);
$oCell->addPreserveText('text');
2014-03-30 11:09:14 +07:00
}
/**
* Add text run
*/
2014-03-09 15:04:23 -04:00
public function testCreateTextRun()
{
2014-05-11 22:54:51 +07:00
$oCell = new Cell();
$element = $oCell->addTextRun();
2014-03-09 15:04:23 -04:00
$this->assertCount(1, $oCell->getElements());
$this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\TextRun', $element);
2014-03-09 15:04:23 -04:00
}
2014-03-30 11:09:14 +07:00
/**
* Add check box
*/
public function testAddCheckBox()
{
2014-05-11 22:54:51 +07:00
$oCell = new Cell();
2014-03-30 11:09:14 +07:00
$element = $oCell->addCheckBox(utf8_decode('ééé'), utf8_decode('ééé'));
$this->assertCount(1, $oCell->getElements());
$this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\CheckBox', $element);
}
2014-03-30 11:09:14 +07:00
/**
* Get elements
*/
2014-03-09 15:04:23 -04:00
public function testGetElements()
{
2014-05-11 22:54:51 +07:00
$oCell = new Cell();
2014-03-09 15:04:23 -04:00
$this->assertInternalType('array', $oCell->getElements());
}
}