mirror of
https://ghproxy.net/https://github.com/elementor/wp2static.git
synced 2025-10-04 01:36:51 +08:00
inject AssetDownloader dependency;allow WP mocks;update tests
This commit is contained in:
parent
88ec8f2ffb
commit
9597a8f93a
9 changed files with 299 additions and 36 deletions
|
@ -40,7 +40,8 @@
|
|||
"phpunit/phpunit": "*",
|
||||
"dealerdirect/phpcodesniffer-composer-installer": "*",
|
||||
"wp-coding-standards/wpcs": "*",
|
||||
"jakub-onderka/php-parallel-lint": "*"
|
||||
"jakub-onderka/php-parallel-lint": "*",
|
||||
"10up/wp_mock": "^0.4.2"
|
||||
},
|
||||
"autoload": {
|
||||
"psr-4": {
|
||||
|
|
197
composer.lock
generated
197
composer.lock
generated
|
@ -4,7 +4,7 @@
|
|||
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
|
||||
"This file is @generated automatically"
|
||||
],
|
||||
"content-hash": "facd34804fc509e9567533043dec4353",
|
||||
"content-hash": "7b23d86c106e1816deb8725fb963b50e",
|
||||
"packages": [
|
||||
{
|
||||
"name": "pear/net_url2",
|
||||
|
@ -117,6 +117,88 @@
|
|||
}
|
||||
],
|
||||
"packages-dev": [
|
||||
{
|
||||
"name": "10up/wp_mock",
|
||||
"version": "0.4.2",
|
||||
"source": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/10up/wp_mock.git",
|
||||
"reference": "9019226eb50df275aa86bb15bfc98a619601ee49"
|
||||
},
|
||||
"dist": {
|
||||
"type": "zip",
|
||||
"url": "https://api.github.com/repos/10up/wp_mock/zipball/9019226eb50df275aa86bb15bfc98a619601ee49",
|
||||
"reference": "9019226eb50df275aa86bb15bfc98a619601ee49",
|
||||
"shasum": ""
|
||||
},
|
||||
"require": {
|
||||
"antecedent/patchwork": "^2.1",
|
||||
"mockery/mockery": "^1.0",
|
||||
"php": ">=7.1",
|
||||
"phpunit/phpunit": ">=7.0"
|
||||
},
|
||||
"require-dev": {
|
||||
"behat/behat": "^3.0",
|
||||
"php-coveralls/php-coveralls": "^2.1",
|
||||
"sebastian/comparator": ">=1.2.3"
|
||||
},
|
||||
"type": "library",
|
||||
"autoload": {
|
||||
"psr-4": {
|
||||
"WP_Mock\\": "./php/WP_Mock"
|
||||
},
|
||||
"classmap": [
|
||||
"php/WP_Mock.php"
|
||||
]
|
||||
},
|
||||
"notification-url": "https://packagist.org/downloads/",
|
||||
"license": [
|
||||
"GPL-2.0-or-later"
|
||||
],
|
||||
"description": "A mocking library to take the pain out of unit testing for WordPress",
|
||||
"time": "2019-03-16T03:44:39+00:00"
|
||||
},
|
||||
{
|
||||
"name": "antecedent/patchwork",
|
||||
"version": "2.1.8",
|
||||
"source": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/antecedent/patchwork.git",
|
||||
"reference": "3bb81ace3914c220aa273d1c0603d5e1b454c0d7"
|
||||
},
|
||||
"dist": {
|
||||
"type": "zip",
|
||||
"url": "https://api.github.com/repos/antecedent/patchwork/zipball/3bb81ace3914c220aa273d1c0603d5e1b454c0d7",
|
||||
"reference": "3bb81ace3914c220aa273d1c0603d5e1b454c0d7",
|
||||
"shasum": ""
|
||||
},
|
||||
"require": {
|
||||
"php": ">=5.4.0"
|
||||
},
|
||||
"type": "library",
|
||||
"notification-url": "https://packagist.org/downloads/",
|
||||
"license": [
|
||||
"MIT"
|
||||
],
|
||||
"authors": [
|
||||
{
|
||||
"name": "Ignas Rudaitis",
|
||||
"email": "ignas.rudaitis@gmail.com"
|
||||
}
|
||||
],
|
||||
"description": "Method redefinition (monkey-patching) functionality for PHP.",
|
||||
"homepage": "http://patchwork2.org/",
|
||||
"keywords": [
|
||||
"aop",
|
||||
"aspect",
|
||||
"interception",
|
||||
"monkeypatching",
|
||||
"redefinition",
|
||||
"runkit",
|
||||
"testing"
|
||||
],
|
||||
"time": "2018-02-19T18:52:50+00:00"
|
||||
},
|
||||
{
|
||||
"name": "dealerdirect/phpcodesniffer-composer-installer",
|
||||
"version": "v0.5.0",
|
||||
|
@ -280,6 +362,54 @@
|
|||
},
|
||||
"time": "2019-06-01T12:43:35+00:00"
|
||||
},
|
||||
{
|
||||
"name": "hamcrest/hamcrest-php",
|
||||
"version": "v2.0.0",
|
||||
"source": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/hamcrest/hamcrest-php.git",
|
||||
"reference": "776503d3a8e85d4f9a1148614f95b7a608b046ad"
|
||||
},
|
||||
"dist": {
|
||||
"type": "zip",
|
||||
"url": "https://api.github.com/repos/hamcrest/hamcrest-php/zipball/776503d3a8e85d4f9a1148614f95b7a608b046ad",
|
||||
"reference": "776503d3a8e85d4f9a1148614f95b7a608b046ad",
|
||||
"shasum": ""
|
||||
},
|
||||
"require": {
|
||||
"php": "^5.3|^7.0"
|
||||
},
|
||||
"replace": {
|
||||
"cordoval/hamcrest-php": "*",
|
||||
"davedevelopment/hamcrest-php": "*",
|
||||
"kodova/hamcrest-php": "*"
|
||||
},
|
||||
"require-dev": {
|
||||
"phpunit/php-file-iterator": "1.3.3",
|
||||
"phpunit/phpunit": "~4.0",
|
||||
"satooshi/php-coveralls": "^1.0"
|
||||
},
|
||||
"type": "library",
|
||||
"extra": {
|
||||
"branch-alias": {
|
||||
"dev-master": "2.0-dev"
|
||||
}
|
||||
},
|
||||
"autoload": {
|
||||
"classmap": [
|
||||
"hamcrest"
|
||||
]
|
||||
},
|
||||
"notification-url": "https://packagist.org/downloads/",
|
||||
"license": [
|
||||
"BSD"
|
||||
],
|
||||
"description": "This is the PHP port of Hamcrest Matchers",
|
||||
"keywords": [
|
||||
"test"
|
||||
],
|
||||
"time": "2016-01-20T08:20:44+00:00"
|
||||
},
|
||||
{
|
||||
"name": "jakub-onderka/php-parallel-lint",
|
||||
"version": "v1.0.0",
|
||||
|
@ -328,6 +458,71 @@
|
|||
"homepage": "https://github.com/JakubOnderka/PHP-Parallel-Lint",
|
||||
"time": "2018-02-24T15:31:20+00:00"
|
||||
},
|
||||
{
|
||||
"name": "mockery/mockery",
|
||||
"version": "1.2.2",
|
||||
"source": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/mockery/mockery.git",
|
||||
"reference": "0eb0b48c3f07b3b89f5169ce005b7d05b18cf1d2"
|
||||
},
|
||||
"dist": {
|
||||
"type": "zip",
|
||||
"url": "https://api.github.com/repos/mockery/mockery/zipball/0eb0b48c3f07b3b89f5169ce005b7d05b18cf1d2",
|
||||
"reference": "0eb0b48c3f07b3b89f5169ce005b7d05b18cf1d2",
|
||||
"shasum": ""
|
||||
},
|
||||
"require": {
|
||||
"hamcrest/hamcrest-php": "~2.0",
|
||||
"lib-pcre": ">=7.0",
|
||||
"php": ">=5.6.0"
|
||||
},
|
||||
"require-dev": {
|
||||
"phpunit/phpunit": "~5.7.10|~6.5|~7.0|~8.0"
|
||||
},
|
||||
"type": "library",
|
||||
"extra": {
|
||||
"branch-alias": {
|
||||
"dev-master": "1.0.x-dev"
|
||||
}
|
||||
},
|
||||
"autoload": {
|
||||
"psr-0": {
|
||||
"Mockery": "library/"
|
||||
}
|
||||
},
|
||||
"notification-url": "https://packagist.org/downloads/",
|
||||
"license": [
|
||||
"BSD-3-Clause"
|
||||
],
|
||||
"authors": [
|
||||
{
|
||||
"name": "Pádraic Brady",
|
||||
"email": "padraic.brady@gmail.com",
|
||||
"homepage": "http://blog.astrumfutura.com"
|
||||
},
|
||||
{
|
||||
"name": "Dave Marshall",
|
||||
"email": "dave.marshall@atstsolutions.co.uk",
|
||||
"homepage": "http://davedevelopment.co.uk"
|
||||
}
|
||||
],
|
||||
"description": "Mockery is a simple yet flexible PHP mock object framework",
|
||||
"homepage": "https://github.com/mockery/mockery",
|
||||
"keywords": [
|
||||
"BDD",
|
||||
"TDD",
|
||||
"library",
|
||||
"mock",
|
||||
"mock objects",
|
||||
"mockery",
|
||||
"stub",
|
||||
"test",
|
||||
"test double",
|
||||
"testing"
|
||||
],
|
||||
"time": "2019-02-13T09:37:52+00:00"
|
||||
},
|
||||
{
|
||||
"name": "myclabs/deep-copy",
|
||||
"version": "1.9.1",
|
||||
|
|
5
phpunit.xml
Normal file
5
phpunit.xml
Normal file
|
@ -0,0 +1,5 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<phpunit
|
||||
bootstrap="tests/unit/bootstrap.php"
|
||||
>
|
||||
</phpunit>
|
|
@ -9,9 +9,16 @@ class AssetDownloader {
|
|||
private $site_url;
|
||||
private $crawlable_filetypes;
|
||||
|
||||
public function __construct() {
|
||||
$plugin = Controller::getInstance();
|
||||
$this->settings = $plugin->options->getSettings( true );
|
||||
public function __construct(
|
||||
$ch,
|
||||
string $site_url,
|
||||
array $crawlable_filetypes,
|
||||
array $settings
|
||||
) {
|
||||
$this->ch = $ch;
|
||||
$this->site_url = $site_url;
|
||||
$this->crawlable_filetypes = $crawlable_filetypes;
|
||||
$this->settings = $settings;
|
||||
}
|
||||
|
||||
/*
|
||||
|
|
|
@ -6,6 +6,33 @@ use DOMElement;
|
|||
|
||||
class ImgSrcSetProcessor {
|
||||
|
||||
private $site_url;
|
||||
private $site_url_host;
|
||||
private $page_url;
|
||||
private $rewrite_rules;
|
||||
private $includeDiscoveredAssets;
|
||||
|
||||
/**
|
||||
* ImgSrcSetProcessor constructor
|
||||
*
|
||||
* @param mixed[] $rewrite_rules URL rewrite rules
|
||||
*/
|
||||
public function __construct(
|
||||
string $site_url,
|
||||
string $site_url_host,
|
||||
string $page_url,
|
||||
array $rewrite_rules,
|
||||
bool $includeDiscoveredAssets,
|
||||
AssetDownloader $asset_downloader
|
||||
) {
|
||||
$this->site_url = $site_url;
|
||||
$this->site_url_host = $site_url_host;
|
||||
$this->page_url = $page_url;
|
||||
$this->rewrite_rules = $rewrite_rules;
|
||||
$this->includeDiscoveredAssets = $includeDiscoveredAssets;
|
||||
$this->asset_downloader = $asset_downloader;
|
||||
}
|
||||
|
||||
public function processImageSrcSet( DOMElement $element ) : void {
|
||||
if ( ! $element->hasAttribute( 'srcset' ) ) {
|
||||
return;
|
||||
|
@ -29,7 +56,15 @@ class ImgSrcSetProcessor {
|
|||
$url = $pieces[0];
|
||||
$dimension = $pieces[1];
|
||||
|
||||
$url_rewriter = new URLRewriter();
|
||||
$url_rewriter = new URLRewriter(
|
||||
$this->site_url,
|
||||
$this->site_url_host,
|
||||
$this->page_url,
|
||||
$this->rewrite_rules,
|
||||
$this->includeDiscoveredAssets,
|
||||
$this->asset_downloader,
|
||||
);
|
||||
|
||||
$url = $url_rewriter->rewriteLocalURL( $url );
|
||||
|
||||
$new_src_set[] = "{$url} {$dimension}";
|
||||
|
|
|
@ -10,7 +10,8 @@ class URLRewriter {
|
|||
private $site_url_host;
|
||||
private $page_url;
|
||||
private $rewrite_rules;
|
||||
private $settings;
|
||||
private $includeDiscoveredAssets;
|
||||
private $asset_downloader;
|
||||
|
||||
/**
|
||||
* URLRewriter constructor
|
||||
|
@ -22,13 +23,15 @@ class URLRewriter {
|
|||
string $site_url_host,
|
||||
string $page_url,
|
||||
array $rewrite_rules,
|
||||
bool $includeDiscoveredAssets
|
||||
bool $includeDiscoveredAssets,
|
||||
AssetDownloader $asset_downloader
|
||||
) {
|
||||
$this->site_url = $site_url;
|
||||
$this->site_url_host = $site_url_host;
|
||||
$this->page_url = $page_url;
|
||||
$this->rewrite_rules = $rewrite_rules;
|
||||
$this->includeDiscoveredAssets = $includeDiscoveredAssets;
|
||||
$this->asset_downloader = $asset_downloader;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -130,8 +133,8 @@ class URLRewriter {
|
|||
// considering caching, ie, build array here
|
||||
// exclude Excludes, already crawled lists
|
||||
// then iterate just the ones not already on disk
|
||||
$asset_downloader = new AssetDownloader();
|
||||
$asset_downloader->downloadAsset( $url, $extension );
|
||||
|
||||
$this->asset_downloader->downloadAsset( $url, $extension );
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -5,22 +5,11 @@ namespace WP2Static;
|
|||
use PHPUnit\Framework\TestCase;
|
||||
use DOMDocument;
|
||||
|
||||
// mock URLRewriter TODO: pass rewrite rules to URLRewriter when updated
|
||||
class URLRewriter {
|
||||
public function rewriteLocalURL( $url ) {
|
||||
$url = str_replace(
|
||||
'https://somedomain.com/',
|
||||
'https://newdomain.com/',
|
||||
$url
|
||||
);
|
||||
|
||||
return $url;
|
||||
}
|
||||
}
|
||||
|
||||
final class ImgSrcSetProcessorTest extends TestCase{
|
||||
|
||||
public function testprocessImageSrcSet() {
|
||||
// mock the AssetDownloader class which is passed to processor
|
||||
$asset_downloader = $this->createMock(AssetDownloader::class);
|
||||
|
||||
$dom = new DOMDocument('1.0', 'utf-8');
|
||||
|
||||
// create parent <head>
|
||||
|
@ -49,18 +38,34 @@ final class ImgSrcSetProcessorTest extends TestCase{
|
|||
$html->appendChild( $body );
|
||||
$dom->appendChild( $html );
|
||||
|
||||
$srcset_processor = new ImgSrcSetProcessor();
|
||||
$srcset_processor = new ImgSrcSetProcessor(
|
||||
'https://somedomain.com/', // site_url
|
||||
'somedomain.com', // site_url_host
|
||||
'https://somedomain.com/apage', // $page_url
|
||||
[
|
||||
'site_url_patterns' => [
|
||||
'https://somedomain.com',
|
||||
],
|
||||
'destination_url_patterns' => [
|
||||
'https://google.com',
|
||||
],
|
||||
], // $rewrite_rules,
|
||||
true, // $includeDiscoveredAssets,
|
||||
$asset_downloader
|
||||
);
|
||||
$srcset_processor->processImageSrcSet( $img );
|
||||
|
||||
$result = $dom->saveHtml();
|
||||
|
||||
$this->assertEquals(
|
||||
'<html><head> <link rel="canonical" href="https://somedomain.com/alink"></head><body> <a href="https://somedomain.com/alink">Some anchor text</a><img href="https://somedomain.com/alink" srcset="https://newdomain.com/image-1x.png 1x,https://newdomain.com/image-2x.png 2x,https://newdomain.com/image-3x.png 3x,https://newdomain.com/image-4x.png 4x"></body></html>' . PHP_EOL,
|
||||
'<html><head> <link rel="canonical" href="https://somedomain.com/alink"></head><body> <a href="https://somedomain.com/alink">Some anchor text</a><img href="https://somedomain.com/alink" srcset="https://google.com/image-1x.png 1x,https://google.com/image-2x.png 2x,https://google.com/image-3x.png 3x,https://google.com/image-4x.png 4x"></body></html>' . PHP_EOL,
|
||||
$result
|
||||
);
|
||||
}
|
||||
|
||||
public function testReturnsImageIfNoSrcSet() {
|
||||
$asset_downloader = $this->createMock(AssetDownloader::class);
|
||||
|
||||
$dom = new DOMDocument('1.0', 'utf-8');
|
||||
|
||||
// create parent <head>
|
||||
|
@ -88,7 +93,21 @@ final class ImgSrcSetProcessorTest extends TestCase{
|
|||
$html->appendChild( $body );
|
||||
$dom->appendChild( $html );
|
||||
|
||||
$srcset_processor = new ImgSrcSetProcessor();
|
||||
$srcset_processor = new ImgSrcSetProcessor(
|
||||
'https://somedomain.com/', // site_url
|
||||
'somedomain.com', // site_url_host
|
||||
'https://somedomain.com/apage', // $page_url
|
||||
[
|
||||
'site_url_patterns' => [
|
||||
'https://somedomain.com',
|
||||
],
|
||||
'destination_url_patterns' => [
|
||||
'https://google.com',
|
||||
],
|
||||
], // $rewrite_rules,
|
||||
true, // $includeDiscoveredAssets,
|
||||
$asset_downloader
|
||||
);
|
||||
$srcset_processor->processImageSrcSet( $img );
|
||||
|
||||
$result = $dom->saveHtml();
|
||||
|
|
|
@ -5,17 +5,11 @@ namespace WP2Static;
|
|||
use PHPUnit\Framework\TestCase;
|
||||
use DOMDocument;
|
||||
|
||||
// mock AssetDownloader
|
||||
class AssetDownloader {
|
||||
public function downloadAsset( $url, $extension ) {
|
||||
//
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
final class URLRewriterTest extends TestCase {
|
||||
|
||||
public function testprocessElementURL() {
|
||||
// mock the AssetDownloader class which is passed to processor
|
||||
$asset_downloader = $this->createMock(AssetDownloader::class);
|
||||
|
||||
$dom = new DOMDocument('1.0', 'utf-8');
|
||||
|
||||
// create parent head
|
||||
|
@ -50,6 +44,7 @@ final class URLRewriterTest extends TestCase {
|
|||
],
|
||||
], // $rewrite_rules,
|
||||
true, // $includeDiscoveredAssets,
|
||||
$asset_downloader
|
||||
);
|
||||
|
||||
$url_rewriter->processElementURL( $anchor );
|
||||
|
|
3
tests/unit/bootstrap.php
Normal file
3
tests/unit/bootstrap.php
Normal file
|
@ -0,0 +1,3 @@
|
|||
<?php
|
||||
|
||||
WP_Mock::bootstrap();
|
Loading…
Add table
Add a link
Reference in a new issue