Skip to content

Commit

Permalink
Rework test project structure
Browse files Browse the repository at this point in the history
  • Loading branch information
brendt committed Dec 3, 2017
1 parent 7173615 commit 212af8f
Show file tree
Hide file tree
Showing 37 changed files with 180 additions and 74 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -3,3 +3,4 @@
.idea
/tests/.cache
/vendor
/test_project
3 changes: 2 additions & 1 deletion composer.json
Expand Up @@ -13,7 +13,8 @@
"illuminate/support": "^5.5",
"symfony/routing": "^3.3",
"symfony/dependency-injection": "^3.3",
"symfony/config": "^3.3"
"symfony/config": "^3.3",
"leafo/scssphp": "^0.7.1"
},
"require-dev" : {
"larapack/dd" : "^1.0",
Expand Down
54 changes: 53 additions & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions src/Pageon/Html/Image/ImageFactory.php
Expand Up @@ -80,6 +80,7 @@ private function createScaledFileName(Image $image, int $width, int $height): st
private function copySourceImageToDestination(string $srcPath): void
{
$fs = new Filesystem();

$fs->copy("{$this->sourceDirectory}/{$srcPath}", "{$this->publicDirectory}/{$srcPath}");
}
}
5 changes: 2 additions & 3 deletions src/Stitcher/Page/Adapter/CollectionAdapter.php
Expand Up @@ -48,12 +48,11 @@ public function isValid($subject): bool
return is_array($subject) && isset($subject['variable']) && isset($subject['parameter']);
}

protected function getEntries($pageConfiguration): array
protected function getEntries($pageConfiguration): ?array
{
$variable = $pageConfiguration['variables'][$this->variable] ?? null;

$entries = $this->variableParser->parse($variable)
?? $variable;
$entries = $this->variableParser->parse($variable) ?? $variable;

return $entries;
}
Expand Down
2 changes: 2 additions & 0 deletions src/Stitcher/Renderer.php
Expand Up @@ -5,4 +5,6 @@
interface Renderer
{
public function renderTemplate(string $template, array $variables): string;

public function customExtension(string $name, callable $function): void;
}
7 changes: 7 additions & 0 deletions src/Stitcher/Renderer/Extension.php
@@ -0,0 +1,7 @@
<?php

namespace Stitcher\Renderer;

interface Extension
{
}
21 changes: 21 additions & 0 deletions src/Stitcher/Renderer/Extension/Css.php
@@ -0,0 +1,21 @@
<?php

namespace Stitcher\Renderer\Extension;

use Leafo\ScssPhp\Compiler as Sass;
use Stitcher\Renderer\Extension;

class Css implements Extension
{
private $sass;

public function __construct(Sass $sass)
{
$this->sass = $sass;
}

public function handle(string $src)
{
// dd($src);
}
}
6 changes: 6 additions & 0 deletions src/Stitcher/Renderer/TwigRenderer.php
Expand Up @@ -5,6 +5,7 @@
use Stitcher\Renderer;
use Symfony\Component\Filesystem\Filesystem;
use Twig_Environment;
use Twig_Function;

class TwigRenderer extends Twig_Environment implements Renderer
{
Expand All @@ -29,4 +30,9 @@ public function renderTemplate(string $path, array $variables): string
{
return $this->render($path, $variables);
}

public function customExtension(string $name, callable $function): void
{
$this->addFunction(new Twig_Function($name, $function));
}
}
23 changes: 1 addition & 22 deletions tests/CreateStitcherFiles.php
Expand Up @@ -4,53 +4,32 @@

use Stitcher\Command\Parse;
use Stitcher\File;
use Symfony\Component\Filesystem\Filesystem;

trait CreateStitcherFiles
{
protected function createIndexTemplate(): void
{
$fs = new Filesystem();

$fs->copy(__DIR__ . '/resources/twig/index.twig', File::path('template/index.twig'));
}

protected function createAllTemplates(): void
{
$fs = new Filesystem();

$fs->copy(__DIR__ . '/resources/twig/_partials/main.twig', File::path('template/_partials/main.twig'));
$fs->copy(__DIR__ . '/resources/twig/overview.twig', File::path('template/overview.twig'));
$fs->copy(__DIR__ . '/resources/twig/detail.twig', File::path('template/detail.twig'));
$this->createIndexTemplate();
}

protected function createDataFile()
{
$fs = new Filesystem();

$fs->copy(__DIR__ . '/resources/data/entries.yaml', File::path('data/entries.yaml'));
}

protected function createImageFiles()
{
$fs = new Filesystem();

$fs->copy(__DIR__ . '/resources/green_large.jpg', File::path('images/green_large.jpg'));
$fs->copy(__DIR__ . '/resources/green.jpg', File::path('images/green.jpg'));
}

protected function createSiteConfiguration(string $configurationPath = null): void
{
$fs = new Filesystem();
$configurationPath = $configurationPath ?? File::path('config/site.yaml');

$fs->copy(__DIR__ . '/resources/config/site.yaml', $configurationPath);
}

protected function parseAll(): void
{
$configurationFile = File::path('config/site.yaml');
$configurationFile = File::path('src/site.yaml');

$this->createAllTemplates();
$this->createSiteConfiguration($configurationFile);
Expand Down
4 changes: 2 additions & 2 deletions tests/CreateStitcherObjects.php
Expand Up @@ -20,7 +20,7 @@ trait CreateStitcherObjects
protected function createPageRenderer() : PageRenderer
{
return PageRenderer::make(
TwigRenderer::make(File::path('/template'))
TwigRenderer::make(File::path('/resources/view'))
);
}

Expand Down Expand Up @@ -56,7 +56,7 @@ protected function createAdapterFactory(VariableParser $variableParser) : Adapte

protected function createImageFactory($sourceDirectory = null): ImageFactory
{
$sourceDirectory = $sourceDirectory ?? __DIR__ . '/';
$sourceDirectory = $sourceDirectory ?? File::path();
$publicPath = File::path('public');

return ImageFactory::make($sourceDirectory, $publicPath, FixedWidthScaler::make([
Expand Down
20 changes: 10 additions & 10 deletions tests/Pageon/Html/Image/ImageFactoryTest.php
Expand Up @@ -14,31 +14,31 @@ public function it_creates_multiple_variations_of_one_source()
{
$public = File::path('public');

$factory = ImageFactory::make(__DIR__ . '/../../../', $public, FixedWidthScaler::make([
$factory = ImageFactory::make(File::path(), $public, FixedWidthScaler::make([
300, 500,
]));

$factory->create('resources/green_large.jpg');
$factory->create('resources/images/green_large.jpg');

$this->assertNotNull(File::read('public/resources/green_large.jpg'));
$this->assertNotNull(File::read('public/resources/green_large-500x500.jpg'));
$this->assertNotNull(File::read('public/resources/green_large-300x300.jpg'));
$this->assertNotNull(File::read('public/resources/images/green_large.jpg'));
$this->assertNotNull(File::read('public/resources/images/green_large-500x500.jpg'));
$this->assertNotNull(File::read('public/resources/images/green_large-300x300.jpg'));
}

/** @test */
public function it_adds_the_srcset()
{
$public = File::path('public');

$factory = ImageFactory::make(__DIR__ . '/../../../', $public, FixedWidthScaler::make([
$factory = ImageFactory::make(File::path(), $public, FixedWidthScaler::make([
300, 500,
]));

$image = $factory->create('resources/green_large.jpg');
$image = $factory->create('resources/images/green_large.jpg');
$srcset = $image->srcset();

$this->assertContains('/resources/green_large.jpg 2500w', $srcset);
$this->assertContains('/resources/green_large-500x500.jpg 500w', $srcset);
$this->assertContains('/resources/green_large-300x300.jpg 300w', $srcset);
$this->assertContains('/resources/images/green_large.jpg 2500w', $srcset);
$this->assertContains('/resources/images/green_large-500x500.jpg 500w', $srcset);
$this->assertContains('/resources/images/green_large-300x300.jpg 300w', $srcset);
}
}
13 changes: 3 additions & 10 deletions tests/Pageon/Integration/FullSiteParseTest.php
Expand Up @@ -5,23 +5,16 @@
use Stitcher\Command\Parse;
use Stitcher\File;
use Stitcher\Test\CreateStitcherObjects;
use Stitcher\Test\CreateStitcherFiles;
use Stitcher\Test\StitcherTest;

class FullSiteParseTest extends StitcherTest
{
use CreateStitcherFiles;
use CreateStitcherObjects;

/** @test */
public function parse_test()
{
$configurationFile = File::path('config/site.yaml');

$this->createAllTemplates();
$this->createSiteConfiguration($configurationFile);
$this->createDataFile();
$this->createImageFiles();
$configurationFile = File::path('src/site.yaml');

$command = Parse::make(
File::path('public'),
Expand Down Expand Up @@ -91,8 +84,8 @@ private function assertImageParsed()
{
$detail = File::read('public/entries/a.html');

$this->assertContains('<img src="/images/green.jpg"', $detail);
$this->assertContains('srcset="/images/green.jpg 250w', $detail);
$this->assertContains('<img src="/resources/images/green.jpg"', $detail);
$this->assertContains('srcset="/resources/images/green.jpg 250w', $detail);
$this->assertContains('alt="test"', $detail);
}
}
2 changes: 0 additions & 2 deletions tests/Pageon/Integration/ServerTest.php
Expand Up @@ -2,8 +2,6 @@

namespace Pageon\Integration;

use Stitcher\Command\Parse;
use Stitcher\File;
use Stitcher\Test\CreateStitcherFiles;
use Stitcher\Test\CreateStitcherObjects;
use Stitcher\Test\StitcherTest;
Expand Down
23 changes: 23 additions & 0 deletions tests/SetUp.php
@@ -0,0 +1,23 @@
<?php

namespace Stitcher\Test;

use Stitcher\File;
use Symfony\Component\Filesystem\Filesystem;

final class SetUp
{
const TEST_PROJECT = 'test_project';

public static function run()
{
$fs = new Filesystem();

$fs->mirror(
__DIR__. '/' . self::TEST_PROJECT,
__DIR__ . '/../' . self::TEST_PROJECT
);

File::base(__DIR__ . '/../' . self::TEST_PROJECT);
}
}
2 changes: 1 addition & 1 deletion tests/Stitcher/Application/DevelopmentServerTest.php
Expand Up @@ -20,7 +20,7 @@ protected function setUp()
{
parent::setUp();

$configurationFile = File::path('config/site.yaml');
$configurationFile = File::path('src/site.yaml');

$this->createAllTemplates();
$this->createSiteConfiguration($configurationFile);
Expand Down
2 changes: 1 addition & 1 deletion tests/Stitcher/Command/PartialParseTest.php
Expand Up @@ -19,7 +19,7 @@ protected function setUp()
{
parent::setUp();

$configurationFile = File::path('config/site.yaml');
$configurationFile = File::path('src/site.yaml');

$this->createAllTemplates();
$this->createSiteConfiguration($configurationFile);
Expand Down
22 changes: 22 additions & 0 deletions tests/Stitcher/Renderer/Extension/CssTest.php
@@ -0,0 +1,22 @@
<?php

namespace Stitcher\Renderer\Extension;

use Leafo\ScssPhp\Compiler as Sass;
use Stitcher\Test\StitcherTest;

class CssTest extends StitcherTest
{
/** @test */
public function it_moves_a_css_file()
{
$css = $this->createExtension();

$css->handle('/css/normal.css');
}

private function createExtension(): Css
{
return new Css(new Sass());
}
}
2 changes: 1 addition & 1 deletion tests/Stitcher/Template/TwigRendererTest.php
Expand Up @@ -13,7 +13,7 @@ class TwigRendererTest extends StitcherTest
/** @test */
public function it_can_render_a_template()
{
$renderer = TwigRenderer::make(File::path('template'));
$renderer = TwigRenderer::make(File::path('resources/view'));
$this->createAllTemplates();

$html = $renderer->renderTemplate('index.twig', [
Expand Down

0 comments on commit 212af8f

Please sign in to comment.