Merge pull request #1544 from troosan/avoid_remote_image_loading_during_tests

Use embedded http server to test loading of remote images
This commit is contained in:
troosan 2019-01-02 14:36:54 +01:00 committed by GitHub
commit 4d76baea42
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
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.
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)
----------------------
### Added

View File

@ -17,12 +17,14 @@
namespace PhpOffice\PhpWord\Element;
use PhpOffice\PhpWord\AbstractWebServerEmbeddedTest;
/**
* Test class for PhpOffice\PhpWord\Element\Cell
*
* @runTestsInSeparateProcesses
*/
class CellTest extends \PHPUnit\Framework\TestCase
class CellTest extends AbstractWebServerEmbeddedTest
{
/**
* New instance
@ -165,7 +167,7 @@ class CellTest extends \PHPUnit\Framework\TestCase
public function testAddImageSectionByUrl()
{
$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->assertInstanceOf('PhpOffice\\PhpWord\\Element\\Image', $element);

View File

@ -17,12 +17,14 @@
namespace PhpOffice\PhpWord\Element;
use PhpOffice\PhpWord\AbstractWebServerEmbeddedTest;
/**
* Test class for PhpOffice\PhpWord\Element\Footer
*
* @runTestsInSeparateProcesses
*/
class FooterTest extends \PHPUnit\Framework\TestCase
class FooterTest extends AbstractWebServerEmbeddedTest
{
/**
* New instance
@ -116,7 +118,7 @@ class FooterTest extends \PHPUnit\Framework\TestCase
public function testAddImageByUrl()
{
$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->assertInstanceOf('PhpOffice\\PhpWord\\Element\\Image', $element);

View File

@ -17,12 +17,14 @@
namespace PhpOffice\PhpWord\Element;
use PhpOffice\PhpWord\AbstractWebServerEmbeddedTest;
/**
* Test class for PhpOffice\PhpWord\Element\Header
*
* @runTestsInSeparateProcesses
*/
class HeaderTest extends \PHPUnit\Framework\TestCase
class HeaderTest extends AbstractWebServerEmbeddedTest
{
/**
* New instance
@ -125,7 +127,7 @@ class HeaderTest extends \PHPUnit\Framework\TestCase
public function testAddImageByUrl()
{
$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->assertInstanceOf('PhpOffice\\PhpWord\\Element\\Image', $element);

View File

@ -17,6 +17,7 @@
namespace PhpOffice\PhpWord\Element;
use PhpOffice\PhpWord\AbstractWebServerEmbeddedTest;
use PhpOffice\PhpWord\SimpleType\Jc;
/**
@ -24,7 +25,7 @@ use PhpOffice\PhpWord\SimpleType\Jc;
*
* @runTestsInSeparateProcesses
*/
class ImageTest extends \PHPUnit\Framework\TestCase
class ImageTest extends AbstractWebServerEmbeddedTest
{
/**
* New instance
@ -131,7 +132,7 @@ class ImageTest extends \PHPUnit\Framework\TestCase
*/
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(
'ssl' => array(
'verify_peer' => false,
@ -139,7 +140,7 @@ class ImageTest extends \PHPUnit\Framework\TestCase
),
);
stream_context_set_default($arrContextOptions);
$object = new Image('https://samples.libav.org/image-samples/RACECAR.BMP');
$object = new Image(self::getRemoteBmpImageUrl());
$object->getSource();
}
@ -215,7 +216,7 @@ class ImageTest extends \PHPUnit\Framework\TestCase
*/
public function testConstructFromGd()
{
$source = 'http://php.net/images/logos/php-icon.png';
$source = self::getRemoteImageUrl();
$image = new Image($source);
$this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\Image', $image);

View File

@ -24,7 +24,7 @@ use PhpOffice\PhpWord\Element\Image;
*
* @runTestsInSeparateProcesses
*/
class MediaTest extends \PHPUnit\Framework\TestCase
class MediaTest extends AbstractWebServerEmbeddedTest
{
/**
* Get section media elements
@ -49,7 +49,7 @@ class MediaTest extends \PHPUnit\Framework\TestCase
{
$local = __DIR__ . '/_files/images/mars.jpg';
$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', $remote, new Image($local));
@ -77,7 +77,7 @@ class MediaTest extends \PHPUnit\Framework\TestCase
public function testAddHeaderMediaElement()
{
$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', $remote, new Image($remote));
@ -92,7 +92,7 @@ class MediaTest extends \PHPUnit\Framework\TestCase
public function testAddFooterMediaElement()
{
$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', $remote, new Image($remote));

View File

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

View File

@ -17,6 +17,7 @@
namespace PhpOffice\PhpWord\Writer;
use PhpOffice\PhpWord\AbstractWebServerEmbeddedTest;
use PhpOffice\PhpWord\PhpWord;
use PhpOffice\PhpWord\Settings;
use PhpOffice\PhpWord\SimpleType\Jc;
@ -26,7 +27,7 @@ use PhpOffice\PhpWord\SimpleType\Jc;
*
* @runTestsInSeparateProcesses
*/
class HTMLTest extends \PHPUnit\Framework\TestCase
class HTMLTest extends AbstractWebServerEmbeddedTest
{
/**
* Construct
@ -57,7 +58,7 @@ class HTMLTest extends \PHPUnit\Framework\TestCase
{
$localImage = __DIR__ . '/../_files/images/PhpWord.png';
$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';
$file = __DIR__ . '/../_files/temp.html';

View File

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