Skip to content

Commit

Permalink
update tests, coverage, cleanup, rename test constant placeholders
Browse files Browse the repository at this point in the history
  • Loading branch information
joemaller committed Feb 27, 2024
1 parent 1ddc085 commit 5b1ebf6
Show file tree
Hide file tree
Showing 7 changed files with 103 additions and 134 deletions.
7 changes: 5 additions & 2 deletions src/Deprecated/DumpSymbols.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,15 @@ public function dumpSymbols()
);
}, $this->inUse);
$symbols = implode("\n", $symbols);
printf("<svg xmlns='http://www.w3.org/2000/svg' style='display: none;'>\n%s\n</svg>\n", $symbols);
printf(
"<svg xmlns='http://www.w3.org/2000/svg' style='display: none;'>\n%s\n</svg>\n",
$symbols
);
} else {
if (is_user_logged_in()) {
echo "<!-- NO SVGs IN USE -->\n";

if ($this->is_debug) {
if ($this->WP_DEBUG) {
$trace = array_map(fn($i) => $i['file'] . ':' . $i['line'], debug_backtrace());
$trace = implode("\n\t", $trace);
printf("<!-- SVG::dumpSymbols call stack:\n\t%s\n -->\n", $trace);
Expand Down
46 changes: 23 additions & 23 deletions src/SVG.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,33 +23,29 @@
class SVG
{
use Deprecated\Directory;
use Deprecated\Get;
use Deprecated\DumpSymbols;
// use Deprecated\LibFill;
use Deprecated\Get;

public $lib = [];

/**
* A placeholder for WP_DEBUG which can be mocked
* Placeholders for mocking
*/
public $is_debug = false;
/**
* A placeholder for the ABSPATH constant which can be mocked
*/
public $abspath;
public $ABSPATH;
public $WP_DEBUG = false;

public $inUse;
public $libDir;
public $transient;
public $rest_base;
public $rest_namespace;
public $rest_route;
public $rest_base;
public $inUse;
public $shortcode;
public $transient;

public function __construct($libDir = null)
{
$this->is_debug = defined('WP_DEBUG') && WP_DEBUG;
$this->abspath = defined('ABSPATH') ? ABSPATH : false;
$this->ABSPATH = defined('ABSPATH') ? ABSPATH : '/';
$this->WP_DEBUG = defined('WP_DEBUG') && WP_DEBUG;

$this->libDir = $libDir ?? get_template_directory() . '/dist/images/svg';

Expand Down Expand Up @@ -98,14 +94,13 @@ public function init()
/**
* Disable transients when WP_DEBUG is true
*/
if ($this->is_debug === true) {
if ($this->WP_DEBUG === true) {
$this->lib = false;
}

if ($this->lib === false) {
$this->lib = [];
$this->loadFromDirectory($this->libDir);
// $this->libfill();
$this->lib['_from_transient'] = false;

set_transient($this->transient, $this->lib, 12 * HOUR_IN_SECONDS);
Expand Down Expand Up @@ -147,7 +142,7 @@ public function loadFromDirectory($dir)

$svg = $this->normalizeSvg(file_get_contents($file->getRealPath()));

$rootRelPath = str_replace($this->abspath, '', $file->getRealPath());
$rootRelPath = str_replace($this->ABSPATH, '', $file->getRealPath());
$srcUrl = site_url($rootRelPath);

/**
Expand Down Expand Up @@ -177,8 +172,6 @@ public function loadFromDirectory($dir)
*/
public function rewrapSvg($svg, $attributes = [])
{
// ~d($attributes);
// d($svg);
$esc_atts = array_map('urlencode', $attributes);
$svg->_links->self = add_query_arg($esc_atts, $svg->_links->self);
$svg->_links->svg = add_query_arg($esc_atts, $svg->_links->svg);
Expand Down Expand Up @@ -278,7 +271,9 @@ public function normalizeSvg($rawSVGString)

$width = array_key_exists('width', $xml_attributes) ? $xml_attributes['width'] : null;
$height = array_key_exists('height', $xml_attributes) ? $xml_attributes['height'] : null;
$viewBox = array_key_exists('viewbox', $xml_attributes) ? explode(' ', $xml_attributes['viewbox']) : [];
$viewBox = array_key_exists('viewbox', $xml_attributes)
? explode(' ', $xml_attributes['viewbox'])
: [];

$attributes = [];

Expand Down Expand Up @@ -440,7 +435,9 @@ public function embed($key, $attributes = [])
{
$svg = $this->fetch($key, $attributes);
if (is_WP_Error($svg)) {
$template = $this->is_debug ? '<text y="20" fill="red">Error: %s</text>' : '"\n<!-- Error: %s -->\n"';
$template = $this->WP_DEBUG
? '<text y="20" fill="red">Error: %s</text>'
: '"\n<!-- Error: %s -->\n"';
$err = sprintf($template, $svg->get_error_message());
return $this->wrapSvg($err);
}
Expand All @@ -459,7 +456,10 @@ public function use($key)

if ($this->hasSVG($name)) {
array_push($this->inUse, $name);
return sprintf('<svg class="%1$s"><use xlink:href="#%1$s" href="#%1$s" /></svg>', $name);
return sprintf(
'<svg class="%1$s"><use xlink:href="#%1$s" href="#%1$s" /></svg>',
$name
);
}
}

Expand Down Expand Up @@ -529,13 +529,13 @@ public function registerRestRoutes()
}

/**
* Check for $this->is_debug and remove private underscore-prefixed keys when false
* Check for $this->WP_DEBUG and remove private underscore-prefixed keys when false
* @param object $svg
* @return object
*/
public function removePrivateKeys($svg)
{
if ($this->is_debug) {
if ($this->WP_DEBUG) {
return $svg;
}
$clean = (object) [];
Expand Down
4 changes: 1 addition & 3 deletions tests/CaseNormalizationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public function setUp(): void
{
$this->SVG = new SVG(__DIR__ . '/fixtures/svg');
$this->SVG->init();
$this->SVG->is_debug = true;
$this->SVG->WP_DEBUG = true;
}

public function beforeEach()
Expand Down Expand Up @@ -127,8 +127,6 @@ public function testSpaces()

$actual = $this->SVG->normalizeKey('omg spaces');
$this->assertEquals($expected, $actual);


}

public function testsubdir()
Expand Down
4 changes: 2 additions & 2 deletions tests/DeprecatedTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public function test_dumpSymbolsNoSVGs()
global $is_user_logged_in;

$is_user_logged_in = true;
$this->SVG->is_debug = false;
$this->SVG->WP_DEBUG = false;

$this->expectOutputRegex('/<!-- NO SVGs IN USE/');
$this->SVG->dumpSymbols();
Expand All @@ -63,7 +63,7 @@ public function test_dumpSymbolsNoSVGsDebug()
global $is_user_logged_in;

$is_user_logged_in = true;
$this->SVG->is_debug = true;
$this->SVG->WP_DEBUG = true;

$this->expectOutputRegex('/<!-- NO SVGs IN USE/');
$this->SVG->dumpSymbols();
Expand Down
9 changes: 9 additions & 0 deletions tests/NormalizeSvgTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,15 @@ public function testBadViewBox()
$this->assertEquals($expected, $actual->attributes['viewBox']);
}

public function testNoViewBox()
{
$w = 50;
$h = 40;
$actual = $this->SVG->normalizeSvg("<svg height='$h' width='$w'></svg>");
$expected = "0 0 $w $h";
$this->assertEquals($expected, $actual->attributes['viewBox']);
}

public function testBadSVG()
{
$actual = $this->SVG->normalizeSvg('not xml');
Expand Down
14 changes: 4 additions & 10 deletions tests/RestApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,7 @@ public function testReturnsSVGFile(): void
->onlyMethods(['returnSvgFile'])
->getMock();

$mockSvg
->expects($this->exactly(2))
->method('returnSvgFile')
->willReturnArgument(0);
$mockSvg->expects($this->exactly(2))->method('returnSvgFile')->willReturnArgument(0);

/**
* $args['svg'] is null
Expand Down Expand Up @@ -139,10 +136,7 @@ public function testReturnSvgFile(): void
->onlyMethods(['exit'])
->getMock();

$mockSvg
->expects($this->once())
->method('exit')
->willReturnArgument(0);
$mockSvg->expects($this->once())->method('exit')->willReturnArgument(0);

// $req = new WP_REST_Request(['name' => 'arrow']);
$mockSvg->returnSvgFile('arrow');
Expand Down Expand Up @@ -194,7 +188,7 @@ public function testResponseHasAttributes(): void
public function testResponseHas_srcPath(): void
{
$req = new WP_REST_Request(['name' => 'arrow']);
$this->SVG->is_debug = true;
$this->SVG->WP_DEBUG = true;
$actual = $this->SVG->restResponse($req);
$this->assertObjectHasProperty('__srcPath', $actual);
}
Expand All @@ -205,7 +199,7 @@ public function testResponseHas_srcPath(): void
public function testResponseHasNo_srcPath(): void
{
$req = new WP_REST_Request(['name' => 'arrow']);
$this->SVG->is_debug = false;
$this->SVG->WP_DEBUG = false;
$actual = $this->SVG->restResponse($req);
$this->assertObjectNotHasProperty('__srcPath', $actual);
}
Expand Down
Loading

0 comments on commit 5b1ebf6

Please sign in to comment.