274 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
*
* @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 11:45:45 +02:00
use BadMethodCallException;
2022-09-16 14:09:17 +02:00
use PhpOffice\PhpWord\Element\Cell;
use PhpOffice\PhpWordTests\AbstractWebServerEmbeddedTest;
2014-03-27 23:55:06 +07:00
/**
2022-09-16 11:45:45 +02:00
* Test class for PhpOffice\PhpWord\Element\Cell.
2014-03-27 23:55:06 +07:00
*
* @runTestsInSeparateProcesses
*/
class CellTest extends AbstractWebServerEmbeddedTest
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-05-11 22:54:51 +07:00
$oCell = new Cell();
2014-03-09 15:04:23 -04:00
2022-09-16 11:45:45 +02:00
self::assertInstanceOf('PhpOffice\\PhpWord\\Element\\Cell', $oCell);
self::assertNull($oCell->getWidth());
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 testConstructWithStyleArray(): void
2014-03-09 15:04:23 -04:00
{
2022-09-16 11:45:45 +02:00
$oCell = new Cell(null, ['valign' => 'center']);
2014-03-09 15:04:23 -04:00
2022-09-16 11:45:45 +02:00
self::assertInstanceOf('PhpOffice\\PhpWord\\Style\\Cell', $oCell->getStyle());
self::assertNull($oCell->getWidth());
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.
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-05-11 22:54:51 +07:00
$oCell = new Cell();
2016-06-04 20:06:37 +04:00
$element = $oCell->addText('text');
2014-03-09 15:04:23 -04:00
2022-09-16 11:45:45 +02:00
self::assertCount(1, $oCell->getElements());
self::assertInstanceOf('PhpOffice\\PhpWord\\Element\\Text', $element);
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 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-05-11 22:54:51 +07:00
$oCell = new Cell();
2016-06-04 20:06:37 +04:00
$element = $oCell->addText(utf8_decode('ééé'));
2014-03-09 15:04:23 -04:00
2022-09-16 11:45:45 +02:00
self::assertCount(1, $oCell->getElements());
self::assertInstanceOf('PhpOffice\\PhpWord\\Element\\Text', $element);
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-05-11 22:54:51 +07:00
$oCell = new Cell();
2016-06-04 20:06:37 +04:00
$element = $oCell->addLink(utf8_decode('ééé'), utf8_decode('ééé'));
2014-03-09 15:04:23 -04:00
2022-09-16 11:45:45 +02:00
self::assertCount(1, $oCell->getElements());
self::assertInstanceOf('PhpOffice\\PhpWord\\Element\\Link', $element);
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-09 15:04:23 -04:00
{
2014-05-11 22:54:51 +07:00
$oCell = new Cell();
2014-03-09 15:04:23 -04:00
$oCell->addTextBreak();
2022-09-16 11:45:45 +02:00
self::assertCount(1, $oCell->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 list item.
2014-03-30 11:09:14 +07:00
*/
2022-09-16 11:45:45 +02:00
public function testAddListItem(): void
2014-03-09 15:04:23 -04:00
{
2014-05-11 22:54:51 +07:00
$oCell = new Cell();
2016-06-04 20:06:37 +04:00
$element = $oCell->addListItem('text');
2014-03-09 15:04:23 -04:00
2022-09-16 11:45:45 +02:00
self::assertCount(1, $oCell->getElements());
self::assertInstanceOf('PhpOffice\\PhpWord\\Element\\ListItem', $element);
self::assertEquals('text', $element->getTextObject()->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 list item non-UTF8.
2014-03-30 11:09:14 +07:00
*/
2022-09-16 11:45:45 +02:00
public function testAddListItemNotUTF8(): void
2014-03-09 15:04:23 -04:00
{
2014-05-11 22:54:51 +07:00
$oCell = new Cell();
2016-06-04 20:06:37 +04:00
$element = $oCell->addListItem(utf8_decode('ééé'));
2014-03-09 15:04:23 -04:00
2022-09-16 11:45:45 +02:00
self::assertCount(1, $oCell->getElements());
self::assertInstanceOf('PhpOffice\\PhpWord\\Element\\ListItem', $element);
self::assertEquals('ééé', $element->getTextObject()->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 image section.
2014-03-30 11:09:14 +07:00
*/
2022-09-16 11:45:45 +02:00
public function testAddImageSection(): void
2014-03-09 15:04:23 -04:00
{
$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
2022-09-16 11:45:45 +02:00
self::assertCount(1, $oCell->getElements());
self::assertInstanceOf('PhpOffice\\PhpWord\\Element\\Image', $element);
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 image header.
2014-03-30 11:09:14 +07:00
*/
2022-09-16 11:45:45 +02:00
public function testAddImageHeader(): void
2014-03-09 15:04:23 -04:00
{
$src = __DIR__ . '/../_files/images/earth.jpg';
$oCell = new Cell('header', 1);
2014-03-09 15:04:23 -04:00
$element = $oCell->addImage($src);
2022-09-16 11:45:45 +02:00
self::assertCount(1, $oCell->getElements());
self::assertInstanceOf('PhpOffice\\PhpWord\\Element\\Image', $element);
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 image footer.
2014-03-30 11:09:14 +07:00
*/
2022-09-16 11:45:45 +02:00
public function testAddImageFooter(): void
2014-03-09 15:04:23 -04:00
{
$src = __DIR__ . '/../_files/images/earth.jpg';
$oCell = new Cell('footer', 1);
2014-03-09 15:04:23 -04:00
$element = $oCell->addImage($src);
2022-09-16 11:45:45 +02:00
self::assertCount(1, $oCell->getElements());
self::assertInstanceOf('PhpOffice\\PhpWord\\Element\\Image', $element);
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 image section by URL.
2014-03-30 11:09:14 +07:00
*/
2022-09-16 11:45:45 +02:00
public function testAddImageSectionByUrl(): void
2014-03-09 15:04:23 -04:00
{
2014-05-11 22:54:51 +07:00
$oCell = new Cell();
$element = $oCell->addImage(self::getRemoteGifImageUrl());
2014-03-09 15:04:23 -04:00
2022-09-16 11:45:45 +02:00
self::assertCount(1, $oCell->getElements());
self::assertInstanceOf('PhpOffice\\PhpWord\\Element\\Image', $element);
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 object.
2014-03-30 11:09:14 +07:00
*/
2022-09-16 11:45:45 +02:00
public function testAddObjectXLS(): void
2014-03-09 15:04:23 -04:00
{
$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);
2022-09-16 11:45:45 +02:00
self::assertCount(1, $oCell->getElements());
self::assertInstanceOf('PhpOffice\\PhpWord\\Element\\OLEObject', $element);
2014-03-09 15:04:23 -04:00
}
2014-03-30 11:09:14 +07:00
/**
2022-09-16 11:45:45 +02:00
* Test add object exception.
2014-03-30 11:09:14 +07:00
*/
2022-09-16 11:45:45 +02:00
public function testAddObjectException(): void
2014-03-30 11:09:14 +07:00
{
$this->expectException(\PhpOffice\PhpWord\Exception\InvalidObjectException::class);
$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
}
/**
2022-09-16 11:45:45 +02:00
* Add preserve text.
2014-03-30 11:09:14 +07:00
*/
2022-09-16 11:45:45 +02:00
public function testAddPreserveText(): void
2014-03-09 15:04:23 -04:00
{
2014-05-11 22:54:51 +07:00
$oCell = new Cell();
$oCell->setDocPart('Header', 1);
2016-06-04 20:06:37 +04:00
$element = $oCell->addPreserveText('text');
2014-03-09 15:04:23 -04:00
2022-09-16 11:45:45 +02:00
self::assertCount(1, $oCell->getElements());
self::assertInstanceOf('PhpOffice\\PhpWord\\Element\\PreserveText', $element);
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 preserve text non-UTF8.
2014-03-30 11:09:14 +07:00
*/
2022-09-16 11:45:45 +02:00
public function testAddPreserveTextNotUTF8(): void
2014-03-09 15:04:23 -04:00
{
2014-05-11 22:54:51 +07:00
$oCell = new Cell();
$oCell->setDocPart('Header', 1);
2016-06-04 20:06:37 +04:00
$element = $oCell->addPreserveText(utf8_decode('ééé'));
2014-03-09 15:04:23 -04:00
2022-09-16 11:45:45 +02:00
self::assertCount(1, $oCell->getElements());
self::assertInstanceOf('PhpOffice\\PhpWord\\Element\\PreserveText', $element);
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 preserve text exception.
2014-03-30 11:09:14 +07:00
*/
2022-09-16 11:45:45 +02:00
public function testAddPreserveTextException(): void
2014-03-30 11:09:14 +07:00
{
2022-09-16 11:45:45 +02:00
$this->expectException(BadMethodCallException::class);
2014-05-11 22:54:51 +07:00
$oCell = new Cell();
2019-06-14 00:19:44 +02:00
$oCell->setDocPart('TextRun', 1);
2016-06-04 20:06:37 +04:00
$oCell->addPreserveText('text');
2014-03-30 11:09:14 +07:00
}
/**
2022-09-16 11:45:45 +02:00
* Add text run.
2014-03-30 11:09:14 +07:00
*/
2022-09-16 11:45:45 +02:00
public function testCreateTextRun(): void
2014-03-09 15:04:23 -04:00
{
2014-05-11 22:54:51 +07:00
$oCell = new Cell();
$element = $oCell->addTextRun();
2014-03-09 15:04:23 -04:00
2022-09-16 11:45:45 +02:00
self::assertCount(1, $oCell->getElements());
self::assertInstanceOf('PhpOffice\\PhpWord\\Element\\TextRun', $element);
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 check box.
2014-03-30 11:09:14 +07:00
*/
2022-09-16 11:45:45 +02:00
public function testAddCheckBox(): void
{
2014-05-11 22:54:51 +07:00
$oCell = new Cell();
2016-06-04 20:06:37 +04:00
$element = $oCell->addCheckBox(utf8_decode('ééé'), utf8_decode('ééé'));
2022-09-16 11:45:45 +02:00
self::assertCount(1, $oCell->getElements());
self::assertInstanceOf('PhpOffice\\PhpWord\\Element\\CheckBox', $element);
}
2014-03-30 11:09:14 +07:00
/**
2022-09-16 11:45:45 +02:00
* Get elements.
2014-03-30 11:09:14 +07:00
*/
2022-09-16 11:45:45 +02:00
public function testGetElements(): void
2014-03-09 15:04:23 -04:00
{
2014-05-11 22:54:51 +07:00
$oCell = new Cell();
2014-03-09 15:04:23 -04:00
2022-09-16 11:45:45 +02:00
self::assertIsArray($oCell->getElements());
2014-03-09 15:04:23 -04:00
}
}