Use embedded http server to test loading of remote images

This commit is contained in:
troosan 2018-12-30 14:14:27 +01:00
parent 7b7d4e4936
commit c408ac5d50
11 changed files with 119 additions and 20 deletions

View File

@ -3,6 +3,15 @@ Change Log
All notable changes to this project will be documented in this file. All notable changes to this project will be documented in this file.
This project adheres to [Semantic Versioning](http://semver.org/). This project adheres to [Semantic Versioning](http://semver.org/).
v0.17.0 (?? ??? 2019)
----------------------
### Added
### Fixed
### Miscelaneous
- Use embedded http server to test loading of remote images @troosan #
v0.16.0 (30 dec 2018) v0.16.0 (30 dec 2018)
---------------------- ----------------------
### Added ### Added

View File

@ -17,12 +17,14 @@
namespace PhpOffice\PhpWord\Element; namespace PhpOffice\PhpWord\Element;
use PhpOffice\PhpWord\AbstractWebServerEmbeddedTest;
/** /**
* Test class for PhpOffice\PhpWord\Element\Cell * Test class for PhpOffice\PhpWord\Element\Cell
* *
* @runTestsInSeparateProcesses * @runTestsInSeparateProcesses
*/ */
class CellTest extends \PHPUnit\Framework\TestCase class CellTest extends AbstractWebServerEmbeddedTest
{ {
/** /**
* New instance * New instance
@ -165,7 +167,7 @@ class CellTest extends \PHPUnit\Framework\TestCase
public function testAddImageSectionByUrl() public function testAddImageSectionByUrl()
{ {
$oCell = new Cell(); $oCell = new Cell();
$element = $oCell->addImage('http://php.net/images/logos/php-med-trans-light.gif'); $element = $oCell->addImage(self::getRemoteGifImageUrl());
$this->assertCount(1, $oCell->getElements()); $this->assertCount(1, $oCell->getElements());
$this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\Image', $element); $this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\Image', $element);

View File

@ -17,12 +17,14 @@
namespace PhpOffice\PhpWord\Element; namespace PhpOffice\PhpWord\Element;
use PhpOffice\PhpWord\AbstractWebServerEmbeddedTest;
/** /**
* Test class for PhpOffice\PhpWord\Element\Footer * Test class for PhpOffice\PhpWord\Element\Footer
* *
* @runTestsInSeparateProcesses * @runTestsInSeparateProcesses
*/ */
class FooterTest extends \PHPUnit\Framework\TestCase class FooterTest extends AbstractWebServerEmbeddedTest
{ {
/** /**
* New instance * New instance
@ -116,7 +118,7 @@ class FooterTest extends \PHPUnit\Framework\TestCase
public function testAddImageByUrl() public function testAddImageByUrl()
{ {
$oFooter = new Footer(1); $oFooter = new Footer(1);
$element = $oFooter->addImage('http://php.net/images/logos/php-med-trans-light.gif'); $element = $oFooter->addImage(self::getRemoteGifImageUrl());
$this->assertCount(1, $oFooter->getElements()); $this->assertCount(1, $oFooter->getElements());
$this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\Image', $element); $this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\Image', $element);

View File

@ -17,12 +17,14 @@
namespace PhpOffice\PhpWord\Element; namespace PhpOffice\PhpWord\Element;
use PhpOffice\PhpWord\AbstractWebServerEmbeddedTest;
/** /**
* Test class for PhpOffice\PhpWord\Element\Header * Test class for PhpOffice\PhpWord\Element\Header
* *
* @runTestsInSeparateProcesses * @runTestsInSeparateProcesses
*/ */
class HeaderTest extends \PHPUnit\Framework\TestCase class HeaderTest extends AbstractWebServerEmbeddedTest
{ {
/** /**
* New instance * New instance
@ -125,7 +127,7 @@ class HeaderTest extends \PHPUnit\Framework\TestCase
public function testAddImageByUrl() public function testAddImageByUrl()
{ {
$oHeader = new Header(1); $oHeader = new Header(1);
$element = $oHeader->addImage('http://php.net/images/logos/php-med-trans-light.gif'); $element = $oHeader->addImage(self::getRemoteGifImageUrl());
$this->assertCount(1, $oHeader->getElements()); $this->assertCount(1, $oHeader->getElements());
$this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\Image', $element); $this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\Image', $element);

View File

@ -17,6 +17,7 @@
namespace PhpOffice\PhpWord\Element; namespace PhpOffice\PhpWord\Element;
use PhpOffice\PhpWord\AbstractWebServerEmbeddedTest;
use PhpOffice\PhpWord\SimpleType\Jc; use PhpOffice\PhpWord\SimpleType\Jc;
/** /**
@ -24,7 +25,7 @@ use PhpOffice\PhpWord\SimpleType\Jc;
* *
* @runTestsInSeparateProcesses * @runTestsInSeparateProcesses
*/ */
class ImageTest extends \PHPUnit\Framework\TestCase class ImageTest extends AbstractWebServerEmbeddedTest
{ {
/** /**
* New instance * New instance
@ -131,7 +132,7 @@ class ImageTest extends \PHPUnit\Framework\TestCase
*/ */
public function testUnsupportedImage() public function testUnsupportedImage()
{ {
//disable ssl verification, never do this in real application, you should pass the certiciate instead!!! //disable ssl verification, never do this in real application, you should pass the certificiate instead!!!
$arrContextOptions = array( $arrContextOptions = array(
'ssl' => array( 'ssl' => array(
'verify_peer' => false, 'verify_peer' => false,
@ -139,7 +140,7 @@ class ImageTest extends \PHPUnit\Framework\TestCase
), ),
); );
stream_context_set_default($arrContextOptions); stream_context_set_default($arrContextOptions);
$object = new Image('https://samples.libav.org/image-samples/RACECAR.BMP'); $object = new Image(self::getRemoteBmpImageUrl());
$object->getSource(); $object->getSource();
} }
@ -215,7 +216,7 @@ class ImageTest extends \PHPUnit\Framework\TestCase
*/ */
public function testConstructFromGd() public function testConstructFromGd()
{ {
$source = 'http://php.net/images/logos/php-icon.png'; $source = self::getRemoteImageUrl();
$image = new Image($source); $image = new Image($source);
$this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\Image', $image); $this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\Image', $image);

View File

@ -24,7 +24,7 @@ use PhpOffice\PhpWord\Element\Image;
* *
* @runTestsInSeparateProcesses * @runTestsInSeparateProcesses
*/ */
class MediaTest extends \PHPUnit\Framework\TestCase class MediaTest extends AbstractWebServerEmbeddedTest
{ {
/** /**
* Get section media elements * Get section media elements
@ -49,7 +49,7 @@ class MediaTest extends \PHPUnit\Framework\TestCase
{ {
$local = __DIR__ . '/_files/images/mars.jpg'; $local = __DIR__ . '/_files/images/mars.jpg';
$object = __DIR__ . '/_files/documents/sheet.xls'; $object = __DIR__ . '/_files/documents/sheet.xls';
$remote = 'http://php.net/images/logos/php-med-trans-light.gif'; $remote = self::getRemoteImageUrl();
Media::addElement('section', 'image', $local, new Image($local)); Media::addElement('section', 'image', $local, new Image($local));
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', 'image', $remote, new Image($local));
@ -77,7 +77,7 @@ class MediaTest extends \PHPUnit\Framework\TestCase
public function testAddHeaderMediaElement() public function testAddHeaderMediaElement()
{ {
$local = __DIR__ . '/_files/images/mars.jpg'; $local = __DIR__ . '/_files/images/mars.jpg';
$remote = 'http://php.net/images/logos/php-med-trans-light.gif'; $remote = self::getRemoteImageUrl();
Media::addElement('header1', 'image', $local, new Image($local)); Media::addElement('header1', 'image', $local, new Image($local));
Media::addElement('header1', 'image', $local, new Image($local)); Media::addElement('header1', 'image', $local, new Image($local));
Media::addElement('header1', 'image', $remote, new Image($remote)); Media::addElement('header1', 'image', $remote, new Image($remote));
@ -92,7 +92,7 @@ class MediaTest extends \PHPUnit\Framework\TestCase
public function testAddFooterMediaElement() public function testAddFooterMediaElement()
{ {
$local = __DIR__ . '/_files/images/mars.jpg'; $local = __DIR__ . '/_files/images/mars.jpg';
$remote = 'http://php.net/images/logos/php-med-trans-light.gif'; $remote = self::getRemoteImageUrl();
Media::addElement('footer1', 'image', $local, new Image($local)); Media::addElement('footer1', 'image', $local, new Image($local));
Media::addElement('footer1', 'image', $local, new Image($local)); Media::addElement('footer1', 'image', $local, new Image($local));
Media::addElement('footer1', 'image', $remote, new Image($remote)); Media::addElement('footer1', 'image', $remote, new Image($remote));

View File

@ -17,6 +17,7 @@
namespace PhpOffice\PhpWord\Shared; namespace PhpOffice\PhpWord\Shared;
use PhpOffice\PhpWord\AbstractWebServerEmbeddedTest;
use PhpOffice\PhpWord\Element\Section; use PhpOffice\PhpWord\Element\Section;
use PhpOffice\PhpWord\SimpleType\Jc; use PhpOffice\PhpWord\SimpleType\Jc;
use PhpOffice\PhpWord\SimpleType\LineSpacingRule; use PhpOffice\PhpWord\SimpleType\LineSpacingRule;
@ -27,7 +28,7 @@ use PhpOffice\PhpWord\TestHelperDOCX;
* Test class for PhpOffice\PhpWord\Shared\Html * Test class for PhpOffice\PhpWord\Shared\Html
* @coversDefaultClass \PhpOffice\PhpWord\Shared\Html * @coversDefaultClass \PhpOffice\PhpWord\Shared\Html
*/ */
class HtmlTest extends \PHPUnit\Framework\TestCase class HtmlTest extends AbstractWebServerEmbeddedTest
{ {
/** /**
* Test unit conversion functions with various numbers * Test unit conversion functions with various numbers
@ -487,7 +488,7 @@ class HtmlTest extends \PHPUnit\Framework\TestCase
*/ */
public function testParseRemoteImage() public function testParseRemoteImage()
{ {
$src = 'https://phpword.readthedocs.io/en/latest/_images/phpword.png'; $src = self::getRemoteImageUrl();
$phpWord = new \PhpOffice\PhpWord\PhpWord(); $phpWord = new \PhpOffice\PhpWord\PhpWord();
$section = $phpWord->addSection(); $section = $phpWord->addSection();

View File

@ -17,6 +17,7 @@
namespace PhpOffice\PhpWord\Writer; namespace PhpOffice\PhpWord\Writer;
use PhpOffice\PhpWord\AbstractWebServerEmbeddedTest;
use PhpOffice\PhpWord\PhpWord; use PhpOffice\PhpWord\PhpWord;
use PhpOffice\PhpWord\Settings; use PhpOffice\PhpWord\Settings;
use PhpOffice\PhpWord\SimpleType\Jc; use PhpOffice\PhpWord\SimpleType\Jc;
@ -26,7 +27,7 @@ use PhpOffice\PhpWord\SimpleType\Jc;
* *
* @runTestsInSeparateProcesses * @runTestsInSeparateProcesses
*/ */
class HTMLTest extends \PHPUnit\Framework\TestCase class HTMLTest extends AbstractWebServerEmbeddedTest
{ {
/** /**
* Construct * Construct
@ -57,7 +58,7 @@ class HTMLTest extends \PHPUnit\Framework\TestCase
{ {
$localImage = __DIR__ . '/../_files/images/PhpWord.png'; $localImage = __DIR__ . '/../_files/images/PhpWord.png';
$archiveImage = 'zip://' . __DIR__ . '/../_files/documents/reader.docx#word/media/image1.jpeg'; $archiveImage = 'zip://' . __DIR__ . '/../_files/documents/reader.docx#word/media/image1.jpeg';
$gdImage = 'http://php.net/images/logos/php-med-trans-light.gif'; $gdImage = self::getRemoteGifImageUrl();
$objectSrc = __DIR__ . '/../_files/documents/sheet.xls'; $objectSrc = __DIR__ . '/../_files/documents/sheet.xls';
$file = __DIR__ . '/../_files/temp.html'; $file = __DIR__ . '/../_files/temp.html';

View File

@ -17,6 +17,7 @@
namespace PhpOffice\PhpWord\Writer; namespace PhpOffice\PhpWord\Writer;
use PhpOffice\PhpWord\AbstractWebServerEmbeddedTest;
use PhpOffice\PhpWord\PhpWord; use PhpOffice\PhpWord\PhpWord;
use PhpOffice\PhpWord\SimpleType\Jc; use PhpOffice\PhpWord\SimpleType\Jc;
use PhpOffice\PhpWord\TestHelperDOCX; use PhpOffice\PhpWord\TestHelperDOCX;
@ -26,7 +27,7 @@ use PhpOffice\PhpWord\TestHelperDOCX;
* *
* @runTestsInSeparateProcesses * @runTestsInSeparateProcesses
*/ */
class Word2007Test extends \PHPUnit\Framework\TestCase class Word2007Test extends AbstractWebServerEmbeddedTest
{ {
/** /**
* Tear down after each test * Tear down after each test
@ -75,7 +76,7 @@ class Word2007Test extends \PHPUnit\Framework\TestCase
public function testSave() public function testSave()
{ {
$localImage = __DIR__ . '/../_files/images/earth.jpg'; $localImage = __DIR__ . '/../_files/images/earth.jpg';
$remoteImage = 'http://php.net/images/logos/new-php-logo.png'; $remoteImage = self::getRemoteGifImageUrl();
$phpWord = new PhpWord(); $phpWord = new PhpWord();
$phpWord->addFontStyle('Font', array('size' => 11)); $phpWord->addFontStyle('Font', array('size' => 11));
$phpWord->addParagraphStyle('Paragraph', array('alignment' => Jc::CENTER)); $phpWord->addParagraphStyle('Paragraph', array('alignment' => Jc::CENTER));

Binary file not shown.

After

Width:  |  Height:  |  Size: 10 KiB

View File

@ -0,0 +1,80 @@
<?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
* @copyright 2010-2018 PHPWord contributors
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
*/
namespace PhpOffice\PhpWord;
use Symfony\Component\Process\Process;
abstract class AbstractWebServerEmbeddedTest extends \PHPUnit\Framework\TestCase
{
private static $httpServer;
public static function setUpBeforeClass()
{
if (self::isBuiltinServerSupported()) {
self::$httpServer = new Process('php -S localhost:8080 -t tests/PhpWord/_files');
self::$httpServer->start();
while (!self::$httpServer->isRunning()) {
usleep(1000);
}
}
}
public static function tearDownAfterClass()
{
if (self::isBuiltinServerSupported()) {
self::$httpServer->stop();
}
}
protected static function getBaseUrl()
{
return 'http://localhost:8080';
}
protected static function getRemoteImageUrl()
{
if (self::$httpServer) {
return self::getBaseUrl() . '/images/new-php-logo.png';
}
return 'http://php.net/images/logos/new-php-logo.png';
}
protected static function getRemoteGifImageUrl()
{
if (self::$httpServer) {
return self::getBaseUrl() . '/images/mario.gif';
}
return 'http://php.net/images/logos/php-med-trans-light.gif';
}
protected static function getRemoteBmpImageUrl()
{
if (self::$httpServer) {
return self::getBaseUrl() . '/images/duke_nukem.bmp';
}
return 'https://samples.libav.org/image-samples/RACECAR.BMP';
}
private static function isBuiltinServerSupported()
{
return version_compare(PHP_VERSION, '5.4.0', '>=');
}
}