2014-02-24 19:17:06 +01:00
|
|
|
<?php
|
2014-03-27 23:55:06 +07:00
|
|
|
/**
|
2014-05-05 13:06:53 +04: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
|
|
|
*
|
2017-11-04 22:44:12 +01: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;
|
2014-02-24 19:17:06 +01:00
|
|
|
|
2022-09-16 11:45:45 +02:00
|
|
|
use Exception;
|
2014-03-31 01:13:02 +07:00
|
|
|
use PhpOffice\PhpWord\Element\Image;
|
2022-09-16 14:09:17 +02:00
|
|
|
use PhpOffice\PhpWord\Media;
|
2014-02-24 19:17:06 +01:00
|
|
|
|
2014-03-27 23:55:06 +07:00
|
|
|
/**
|
2022-09-16 11:45:45 +02:00
|
|
|
* Test class for PhpOffice\PhpWord\Media.
|
2014-03-27 23:55:06 +07:00
|
|
|
*
|
|
|
|
|
* @runTestsInSeparateProcesses
|
|
|
|
|
*/
|
2018-12-30 14:14:27 +01:00
|
|
|
class MediaTest extends AbstractWebServerEmbeddedTest
|
2014-03-02 14:40:58 -05:00
|
|
|
{
|
2014-03-28 13:50:53 +07:00
|
|
|
/**
|
2022-09-16 11:45:45 +02:00
|
|
|
* Get section media elements.
|
2014-03-28 13:50:53 +07:00
|
|
|
*/
|
2022-09-16 11:45:45 +02:00
|
|
|
public function testGetSectionMediaElementsWithNull(): void
|
2014-03-02 14:40:58 -05:00
|
|
|
{
|
2022-09-16 11:45:45 +02:00
|
|
|
self::assertEquals([], Media::getElements('section'));
|
2014-03-02 14:40:58 -05:00
|
|
|
}
|
2014-02-24 19:17:06 +01:00
|
|
|
|
2019-10-23 13:41:35 +05:00
|
|
|
/**
|
2022-09-16 11:45:45 +02:00
|
|
|
* Get header media elements.
|
2019-10-23 13:41:35 +05:00
|
|
|
*/
|
2022-09-16 11:45:45 +02:00
|
|
|
public function testGetHeaderMediaElementsWithNull(): void
|
2019-10-23 13:41:35 +05:00
|
|
|
{
|
2022-09-16 11:45:45 +02:00
|
|
|
self::assertEquals([], Media::getElements('header'));
|
2019-10-23 13:41:35 +05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2022-09-16 11:45:45 +02:00
|
|
|
* Get footer media elements.
|
2019-10-23 13:41:35 +05:00
|
|
|
*/
|
2022-09-16 11:45:45 +02:00
|
|
|
public function testGetFooterMediaElementsWithNull(): void
|
2019-10-23 13:41:35 +05:00
|
|
|
{
|
2022-09-16 11:45:45 +02:00
|
|
|
self::assertEquals([], Media::getElements('footer'));
|
2019-10-23 13:41:35 +05:00
|
|
|
}
|
|
|
|
|
|
2014-03-28 13:50:53 +07:00
|
|
|
/**
|
2022-09-16 11:45:45 +02:00
|
|
|
* Count section media elements.
|
2014-03-28 13:50:53 +07:00
|
|
|
*/
|
2022-09-16 11:45:45 +02:00
|
|
|
public function testCountSectionMediaElementsWithNull(): void
|
2014-03-02 14:40:58 -05:00
|
|
|
{
|
2022-09-16 11:45:45 +02:00
|
|
|
self::assertEquals(0, Media::countElements('section'));
|
2014-03-02 14:40:58 -05:00
|
|
|
}
|
2014-03-12 09:25:21 -04:00
|
|
|
|
|
|
|
|
/**
|
2022-09-16 11:45:45 +02:00
|
|
|
* Add section media element.
|
2014-03-12 09:25:21 -04:00
|
|
|
*/
|
2022-09-16 11:45:45 +02:00
|
|
|
public function testAddSectionMediaElement(): void
|
2014-03-12 09:25:21 -04:00
|
|
|
{
|
2015-02-06 22:28:31 +04:00
|
|
|
$local = __DIR__ . '/_files/images/mars.jpg';
|
|
|
|
|
$object = __DIR__ . '/_files/documents/sheet.xls';
|
2018-12-30 14:14:27 +01:00
|
|
|
$remote = self::getRemoteImageUrl();
|
2014-04-05 19:02:49 +07:00
|
|
|
Media::addElement('section', 'image', $local, new Image($local));
|
|
|
|
|
Media::addElement('section', 'image', $local, new Image($local));
|
|
|
|
|
Media::addElement('section', 'image', $remote, new Image($local));
|
|
|
|
|
Media::addElement('section', 'object', $object);
|
|
|
|
|
Media::addElement('section', 'object', $object);
|
2014-03-28 13:50:53 +07:00
|
|
|
|
2022-09-16 11:45:45 +02:00
|
|
|
self::assertCount(3, Media::getElements('section'));
|
2014-03-28 13:50:53 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2022-09-16 11:45:45 +02:00
|
|
|
* Add section link.
|
2014-03-28 13:50:53 +07:00
|
|
|
*/
|
2022-09-16 11:45:45 +02:00
|
|
|
public function testAddSectionLinkElement(): void
|
2014-03-28 13:50:53 +07:00
|
|
|
{
|
2014-04-05 19:02:49 +07:00
|
|
|
$expected = Media::countElements('section') + 1;
|
|
|
|
|
$actual = Media::addElement('section', 'link', 'http://test.com');
|
2014-03-28 13:50:53 +07:00
|
|
|
|
2022-09-16 11:45:45 +02:00
|
|
|
self::assertEquals($expected, $actual);
|
|
|
|
|
self::assertCount(1, Media::getElements('section', 'link'));
|
2014-03-28 13:50:53 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2022-09-16 11:45:45 +02:00
|
|
|
* Add header media element.
|
2014-03-28 13:50:53 +07:00
|
|
|
*/
|
2022-09-16 11:45:45 +02:00
|
|
|
public function testAddHeaderMediaElement(): void
|
2014-03-28 13:50:53 +07:00
|
|
|
{
|
2015-02-06 22:28:31 +04:00
|
|
|
$local = __DIR__ . '/_files/images/mars.jpg';
|
2018-12-30 14:14:27 +01:00
|
|
|
$remote = self::getRemoteImageUrl();
|
2014-04-05 19:02:49 +07:00
|
|
|
Media::addElement('header1', 'image', $local, new Image($local));
|
|
|
|
|
Media::addElement('header1', 'image', $local, new Image($local));
|
|
|
|
|
Media::addElement('header1', 'image', $remote, new Image($remote));
|
2014-03-28 13:50:53 +07:00
|
|
|
|
2022-09-16 11:45:45 +02:00
|
|
|
self::assertCount(2, Media::getElements('header1'));
|
|
|
|
|
self::assertEmpty(Media::getElements('header2'));
|
2014-03-28 13:50:53 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2022-09-16 11:45:45 +02:00
|
|
|
* Add footer media element and reset media.
|
2014-03-28 13:50:53 +07:00
|
|
|
*/
|
2022-09-16 11:45:45 +02:00
|
|
|
public function testAddFooterMediaElement(): void
|
2014-03-28 13:50:53 +07:00
|
|
|
{
|
2015-02-06 22:28:31 +04:00
|
|
|
$local = __DIR__ . '/_files/images/mars.jpg';
|
2018-12-30 14:14:27 +01:00
|
|
|
$remote = self::getRemoteImageUrl();
|
2014-04-05 19:02:49 +07:00
|
|
|
Media::addElement('footer1', 'image', $local, new Image($local));
|
|
|
|
|
Media::addElement('footer1', 'image', $local, new Image($local));
|
|
|
|
|
Media::addElement('footer1', 'image', $remote, new Image($remote));
|
2014-03-12 09:25:21 -04:00
|
|
|
|
2022-09-16 11:45:45 +02:00
|
|
|
self::assertCount(2, Media::getElements('footer1'));
|
2014-04-05 00:57:50 +07:00
|
|
|
|
2014-04-08 16:09:31 +07:00
|
|
|
Media::resetElements();
|
2022-09-16 11:45:45 +02:00
|
|
|
self::assertCount(0, Media::getElements('footer1'));
|
2014-04-05 19:02:49 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2022-09-16 11:45:45 +02:00
|
|
|
* Add image element exception.
|
2014-04-05 19:02:49 +07:00
|
|
|
*/
|
2022-09-16 11:45:45 +02:00
|
|
|
public function testAddElementImageException(): void
|
2014-04-05 19:02:49 +07:00
|
|
|
{
|
2022-09-16 11:45:45 +02:00
|
|
|
$this->expectException(Exception::class);
|
2022-09-16 11:27:29 +02:00
|
|
|
$this->expectExceptionMessage('Image object not assigned.');
|
2015-02-06 22:28:31 +04:00
|
|
|
Media::addElement('section', 'image', __DIR__ . '/_files/images/mars.jpg');
|
2014-03-12 09:25:21 -04:00
|
|
|
}
|
2014-03-23 17:37:26 +07:00
|
|
|
}
|