Skip to content

Commit

Permalink
Tweak tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mbardelmeijer committed May 4, 2020
1 parent bdc37ac commit 1677895
Show file tree
Hide file tree
Showing 10 changed files with 79 additions and 47 deletions.
7 changes: 4 additions & 3 deletions src/Spritesheet.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

namespace Enflow\Svg;

use Illuminate\Contracts\Support\Htmlable;
use Illuminate\Support\Collection;
use Illuminate\Support\HtmlString;
use Illuminate\Contracts\Support\Htmlable;

class Spritesheet extends Collection implements Htmlable
{
Expand All @@ -14,9 +15,9 @@ public function queue(Svg $svg): void
$this->put($svg->id(), $svg);
}

public function toHtml()
public function toHtml(): HtmlString
{
// Regex ported from https://github.com/Hedronium/SpacelessBlade/blob/master/src/SpacelessBladeProvider.php
return preg_replace('/>\\s+</', '><', view('svg::spritesheet', ['spritesheet' => $this])->render());
return new HtmlString(preg_replace('/>\\s+</', '><', view('svg::spritesheet', ['spritesheet' => $this])->render()));
}
}
9 changes: 3 additions & 6 deletions tests/PackTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,9 @@

class PackTest extends TestCase
{
protected function getEnvironmentSetUp($app)
{
$app['config']->set('svg.packs', [
'custom' => __DIR__ . '/fixtures/custom',
]);
}
protected array $packs = [
'custom' => __DIR__ . '/fixtures/custom',
];

public function test_get_all_packs()
{
Expand Down
41 changes: 41 additions & 0 deletions tests/SpritesheetTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace Enflow\Svg\Test;

use Enflow\Svg\Exceptions\SvgMustBeRendered;
use Enflow\Svg\Exceptions\SvgNotFoundException;
use Enflow\Svg\Spritesheet;
use Enflow\Svg\Svg;

class SpritesheetTest extends TestCase
{
use Snapshots;

public function test_spritesheet()
{
$this->assertCount(0, app(Spritesheet::class));
$this->assertMatchesTextSnapshot(app(Spritesheet::class));

$this->assertMatchesXmlSnapshot(svg('clock')->render());

$this->assertCount(1, app(Spritesheet::class));

// We don't use 'assertMatchesHtmlSnapshot' as this adds a <body> etc. around the test-subject.
// This is fine locally, but fails in the GitHub actions matrix.
$this->assertMatchesTextSnapshot(app(Spritesheet::class));

svg('clock')->render();
$this->assertCount(1, app(Spritesheet::class));
}

public function test_that_svg_is_only_once_in_spritesheet()
{
svg('clock')->render();
svg('clock')->render();
svg('house')->render();

$spritesheet = app(Spritesheet::class);
$this->assertCount(2, $spritesheet);
$this->assertEquals(2, substr_count($spritesheet->toHtml(), '<symbol'));
}
}
45 changes: 12 additions & 33 deletions tests/SvgTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,35 +11,25 @@ class SvgTest extends TestCase
{
use Snapshots;

protected function getEnvironmentSetUp($app)
{
$app['config']->set('svg.packs', [
'custom' => __DIR__ . '/fixtures/custom',
'auto-discovery-disabled' => [
'path' => __DIR__ . '/fixtures/icons',
'auto_size_on_viewbox' => true,
'auto_discovery' => false,
],
'icons' => [
'path' => __DIR__ . '/fixtures/icons',
'auto_size_on_viewbox' => true,
],
]);
}
protected array $packs = [
'custom' => __DIR__ . '/fixtures/custom',
'auto-discovery-disabled' => [
'path' => __DIR__ . '/fixtures/icons',
'auto_size_on_viewbox' => true,
'auto_discovery' => false,
],
'icons' => [
'path' => __DIR__ . '/fixtures/icons',
'auto_size_on_viewbox' => true,
],
];

public function test_svg_rendering()
{
$svg = svg('clock'); // Get from "custom", the first of the array

$this->assertMatchesXmlSnapshot($svg->render());
$this->assertEquals($svg->pack->name, 'custom');

$spritesheet = app(Spritesheet::class);
$this->assertCount(1, $spritesheet);

// We don't use 'assertMatchesHtmlSnapshot' as this adds a <body> etc. around the test-subject.
// This is fine locally, but fails in the GitHub actions matrix.
$this->assertMatchesTextSnapshot($spritesheet->toHtml());
}

public function test_auto_size_for_svg_rendering()
Expand Down Expand Up @@ -93,17 +83,6 @@ public function test_that_every_render_is_the_same()
$this->assertEquals($svgOne->render(), $svgTwo->render());
}

public function test_that_svg_is_only_once_in_spritesheet()
{
svg('clock')->render();
svg('clock')->render();
svg('house')->render();

$spritesheet = app(Spritesheet::class);
$this->assertCount(2, $spritesheet);
$this->assertEquals(2, substr_count($spritesheet->toHtml(), '<symbol'));
}

public function test_that_custom_svg_takes_over_width_and_height_from_source()
{
$this->assertMatchesXmlSnapshot(svg('fixed-width-height')->render());
Expand Down
13 changes: 13 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,23 @@

abstract class TestCase extends TestbenchTestCase
{
protected array $packs = [
'custom' => __DIR__ . '/fixtures/custom',
'icons' => [
'path' => __DIR__ . '/fixtures/icons',
'auto_size_on_viewbox' => true,
],
];

protected function getPackageProviders($app)
{
return [
\Enflow\Svg\SvgServiceProvider::class,
];
}

protected function getEnvironmentSetUp($app)
{
$app['config']->set('svg.packs', $this->packs);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[]
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0"?>
<svg aria-hidden="true" focusable="false" role="img">
<use xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#custom-clock"/>
</svg>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"custom-clock":{"name":"clock","pack":{"name":"custom","paths":["\/home\/michel\/code\/enflow\/laravel-svg\/tests\/fixtures\/custom"],"autoSizeOnViewBox":false,"autoDiscovery":true},"contents":"<svg aria-hidden=\"true\" focusable=\"false\" data-prefix=\"fas\" data-icon=\"clock\" class=\"svg-inline--fa fa-clock fa-w-16\" role=\"img\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" viewBox=\"0 0 512 512\"><path fill=\"currentColor\" d=\"M256,8C119,8,8,119,8,256S119,504,256,504,504,393,504,256,393,8,256,8Zm92.49,313h0l-20,25a16,16,0,0,1-22.49,2.5h0l-67-49.72a40,40,0,0,1-15-31.23V112a16,16,0,0,1,16-16h32a16,16,0,0,1,16,16V256l58,42.5A16,16,0,0,1,348.49,321Z\"><\/path><\/svg>\n"}}
4 changes: 0 additions & 4 deletions tests/__snapshots__/SvgTest--test_svg_rendering--68--2.html

This file was deleted.

1 change: 0 additions & 1 deletion tests/__snapshots__/SvgTest--test_svg_rendering--68--2.txt

This file was deleted.

0 comments on commit 1677895

Please sign in to comment.