PHPWord/tests/PhpWordTests/Style/AbstractStyleTest.php

94 lines
3.3 KiB
PHP
Raw Normal View History

2014-04-12 12:57:51 +07:00
<?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.
2014-04-12 12:57:51 +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-04-12 12:57:51 +07:00
*/
2022-09-16 14:09:17 +02:00
namespace PhpOffice\PhpWordTests\Style;
2014-04-12 12:57:51 +07:00
2022-09-16 11:45:45 +02:00
use InvalidArgumentException;
use ReflectionClass;
2014-04-12 12:57:51 +07:00
/**
2022-09-16 11:45:45 +02:00
* Test class for PhpOffice\PhpWord\Style\AbstractStyle.
2014-04-12 12:57:51 +07:00
*
* @runTestsInSeparateProcesses
*/
class AbstractStyleTest extends \PHPUnit\Framework\TestCase
2014-04-12 12:57:51 +07:00
{
/**
2022-09-16 11:45:45 +02:00
* Test set style by array.
2014-04-12 12:57:51 +07:00
*/
2022-09-16 11:45:45 +02:00
public function testSetStyleByArray(): void
2014-04-12 12:57:51 +07:00
{
$stub = $this->getMockForAbstractClass('\PhpOffice\PhpWord\Style\AbstractStyle');
2022-09-16 11:45:45 +02:00
$stub->setStyleByArray(['index' => 1]);
2014-04-12 12:57:51 +07:00
2022-09-16 11:45:45 +02:00
self::assertEquals(1, $stub->getIndex());
2014-04-12 12:57:51 +07:00
}
/**
2022-09-16 11:45:45 +02:00
* Test setBoolVal, setIntVal, setFloatVal, setEnumVal with normal value.
2014-04-12 12:57:51 +07:00
*/
2022-09-16 11:45:45 +02:00
public function testSetValNormal(): void
2014-04-12 12:57:51 +07:00
{
$stub = $this->getMockForAbstractClass('\PhpOffice\PhpWord\Style\AbstractStyle');
2022-09-16 11:45:45 +02:00
self::assertTrue(self::callProtectedMethod($stub, 'setBoolVal', [true, false]));
self::assertEquals(12, self::callProtectedMethod($stub, 'setIntVal', [12, 200]));
self::assertEquals(871.1, self::callProtectedMethod($stub, 'setFloatVal', [871.1, 2.1]));
self::assertEquals(871.1, self::callProtectedMethod($stub, 'setFloatVal', ['871.1', 2.1]));
self::assertEquals('a', self::callProtectedMethod($stub, 'setEnumVal', ['a', ['a', 'b'], 'b']));
2014-04-12 12:57:51 +07:00
}
/**
2022-09-16 11:45:45 +02:00
* Test setBoolVal, setIntVal, setFloatVal, setEnumVal with default value.
2014-04-12 12:57:51 +07:00
*/
2022-09-16 11:45:45 +02:00
public function testSetValDefault(): void
2014-04-12 12:57:51 +07:00
{
$stub = $this->getMockForAbstractClass('\PhpOffice\PhpWord\Style\AbstractStyle');
2022-09-16 11:45:45 +02:00
self::assertNotTrue(self::callProtectedMethod($stub, 'setBoolVal', ['a', false]));
self::assertEquals(200, self::callProtectedMethod($stub, 'setIntVal', ['foo', 200]));
self::assertEquals(2.1, self::callProtectedMethod($stub, 'setFloatVal', ['foo', 2.1]));
self::assertEquals('b', self::callProtectedMethod($stub, 'setEnumVal', [null, ['a', 'b'], 'b']));
2014-05-08 19:25:29 +07:00
}
/**
2022-09-16 11:45:45 +02:00
* Test setEnumVal exception.
2014-05-08 19:25:29 +07:00
*/
2022-09-16 11:45:45 +02:00
public function testSetValEnumException(): void
2014-05-08 19:25:29 +07:00
{
2022-09-16 11:45:45 +02:00
$this->expectException(InvalidArgumentException::class);
2014-05-08 19:25:29 +07:00
$stub = $this->getMockForAbstractClass('\PhpOffice\PhpWord\Style\AbstractStyle');
2022-09-16 11:45:45 +02:00
self::assertEquals('b', self::callProtectedMethod($stub, 'setEnumVal', ['z', ['a', 'b'], 'b']));
2014-04-12 12:57:51 +07:00
}
/**
2022-09-16 11:45:45 +02:00
* Helper function to call protected method.
2014-04-13 23:17:39 +07:00
*
* @param mixed $object
* @param string $method
2014-04-12 12:57:51 +07:00
*/
2022-09-16 11:45:45 +02:00
public static function callProtectedMethod($object, $method, array $args = [])
2014-04-12 12:57:51 +07:00
{
2022-09-16 11:45:45 +02:00
$class = new ReflectionClass(get_class($object));
2014-04-12 12:57:51 +07:00
$method = $class->getMethod($method);
$method->setAccessible(true);
2014-04-12 12:57:51 +07:00
return $method->invokeArgs($object, $args);
}
}