PHPWord/tests/PhpWordTests/MediaTest.php

134 lines
4.1 KiB
PHP
Raw Permalink Normal View History

2014-02-24 19:17:06 +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;
2014-02-24 19:17:06 +01:00
2022-09-16 11:45:45 +02:00
use Exception;
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
*/
class MediaTest extends AbstractWebServerEmbeddedTest
{
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
{
2022-09-16 11:45:45 +02:00
self::assertEquals([], Media::getElements('section'));
}
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
{
2022-09-16 11:45:45 +02:00
self::assertEquals(0, Media::countElements('section'));
}
/**
2022-09-16 11:45:45 +02:00
* Add section media element.
*/
2022-09-16 11:45:45 +02:00
public function testAddSectionMediaElement(): void
{
$local = __DIR__ . '/_files/images/mars.jpg';
$object = __DIR__ . '/_files/documents/sheet.xls';
$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
{
$local = __DIR__ . '/_files/images/mars.jpg';
$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
{
$local = __DIR__ . '/_files/images/mars.jpg';
$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));
2022-09-16 11:45:45 +02:00
self::assertCount(2, Media::getElements('footer1'));
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);
$this->expectExceptionMessage('Image object not assigned.');
Media::addElement('section', 'image', __DIR__ . '/_files/images/mars.jpg');
}
}