PHPWord/tests/PhpWordTests/Element/CommentTest.php

105 lines
3.3 KiB
PHP
Raw Normal View History

<?php
/**
* 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.
*
* @see https://github.com/PHPOffice/PHPWord
2022-09-16 11:45:45 +02:00
*
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
*/
2022-09-16 14:09:17 +02:00
namespace PhpOffice\PhpWordTests\Element;
2022-09-16 11:45:45 +02:00
use DateTime;
use InvalidArgumentException;
2022-09-16 14:09:17 +02:00
use PhpOffice\PhpWord\Element\Comment;
use PhpOffice\PhpWord\Element\Text;
2022-09-16 11:45:45 +02:00
/**
2022-09-16 11:45:45 +02:00
* Test class for PhpOffice\PhpWord\Element\Header.
*
* @runTestsInSeparateProcesses
*/
class CommentTest extends \PHPUnit\Framework\TestCase
{
/**
2022-09-16 11:45:45 +02:00
* New instance.
*/
2022-09-16 11:45:45 +02:00
public function testConstructDefault(): void
{
$author = 'Test User';
2022-09-16 11:45:45 +02:00
$date = new DateTime('2000-01-01');
$initials = 'default_user';
$oComment = new Comment($author, $date, $initials);
$oText = new Text('dummy text');
$oComment->setStartElement($oText);
$oComment->setEndElement($oText);
2022-09-16 11:45:45 +02:00
self::assertInstanceOf('PhpOffice\\PhpWord\\Element\\Comment', $oComment);
self::assertEquals($author, $oComment->getAuthor());
self::assertEquals($date, $oComment->getDate());
self::assertEquals($initials, $oComment->getInitials());
self::assertEquals($oText, $oComment->getStartElement());
self::assertEquals($oText, $oComment->getEndElement());
}
/**
2022-09-16 11:45:45 +02:00
* Add text.
*/
2022-09-16 11:45:45 +02:00
public function testAddText(): void
{
2022-09-16 11:45:45 +02:00
$oComment = new Comment('Test User', new DateTime(), 'my_initials');
$element = $oComment->addText('text');
2022-09-16 11:45:45 +02:00
self::assertInstanceOf('PhpOffice\\PhpWord\\Element\\Text', $element);
self::assertCount(1, $oComment->getElements());
self::assertEquals('text', $element->getText());
}
/**
2022-09-16 11:45:45 +02:00
* Get elements.
*/
2022-09-16 11:45:45 +02:00
public function testGetElements(): void
{
2022-09-16 11:45:45 +02:00
$oComment = new Comment('Test User', new DateTime(), 'my_initials');
2022-09-16 11:45:45 +02:00
self::assertIsArray($oComment->getElements());
}
/**
2022-09-16 11:45:45 +02:00
* Set/get relation Id.
*/
2022-09-16 11:45:45 +02:00
public function testRelationId(): void
{
2022-09-16 11:45:45 +02:00
$oComment = new Comment('Test User', new DateTime(), 'my_initials');
2022-09-16 11:45:45 +02:00
$iVal = mt_rand(1, 1000);
$oComment->setRelationId($iVal);
2022-09-16 11:45:45 +02:00
self::assertEquals($iVal, $oComment->getRelationId());
}
2022-09-16 11:45:45 +02:00
public function testExceptionOnCommentStartOnComment(): void
{
2022-09-16 11:45:45 +02:00
$this->expectException(InvalidArgumentException::class);
$dummyComment = new Comment('Test User', new DateTime(), 'my_initials');
$oComment = new Comment('Test User', new DateTime(), 'my_initials');
$oComment->setCommentRangeStart($dummyComment);
}
2022-09-16 11:45:45 +02:00
public function testExceptionOnCommentEndOnComment(): void
{
2022-09-16 11:45:45 +02:00
$this->expectException(InvalidArgumentException::class);
$dummyComment = new Comment('Test User', new DateTime(), 'my_initials');
$oComment = new Comment('Test User', new DateTime(), 'my_initials');
$oComment->setCommentRangeEnd($dummyComment);
}
}