Skip to content

Commit

Permalink
Improve tests
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexMasterov committed Jan 7, 2017
1 parent 21e66c0 commit 026004c
Show file tree
Hide file tree
Showing 9 changed files with 164 additions and 136 deletions.
5 changes: 4 additions & 1 deletion composer.json
Expand Up @@ -19,14 +19,17 @@
"equip/framework": "3.0.0-alpha1",
"twig/twig": "^2.0"
},
"require-dev": {
"phpunit/phpunit": "^5.7"
},
"autoload": {
"psr-4": {
"AlexMasterov\\EquipTwig\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"AlexMasterov\\EquipTwigTests\\": "tests/"
"AlexMasterov\\EquipTwig\\Tests\\": "tests/"
}
},
"suggest": {
Expand Down
31 changes: 0 additions & 31 deletions tests/Asset/Template.php

This file was deleted.

6 changes: 4 additions & 2 deletions tests/Configuration/TwigConfigurationTest.php
@@ -1,6 +1,6 @@
<?php

namespace AlexMasterov\EquipTwigTests\Configuration;
namespace AlexMasterov\EquipTwig\Tests\Configuration;

use AlexMasterov\EquipTwig\Configuration\TwigConfiguration;
use Auryn\Injector;
Expand All @@ -11,8 +11,10 @@ class TwigConfigurationTest extends TestCase
{
public function testApply()
{
// Stab
$config = [
'TWIG_FILE_EXTENSIONS' => 'html.twig,twig'
'TWIG_FILE_EXTENSIONS' => 'html.twig,twig',
'TWIG_TEMPLATES' => __DIR__
];

$injector = new Injector;
Expand Down
16 changes: 10 additions & 6 deletions tests/Configuration/TwigExtensionSetTest.php
@@ -1,6 +1,6 @@
<?php

namespace AlexMasterov\EquipTwigTests\Configuration;
namespace AlexMasterov\EquipTwig\Tests\Configuration;

use AlexMasterov\EquipTwig\Configuration\TwigConfiguration;
use AlexMasterov\EquipTwig\Configuration\TwigExtensionSet;
Expand All @@ -14,8 +14,10 @@ class TwigExtensionSetTest extends TestCase
{
public function testThenDebugIsEnabled()
{
// Stab
$config = [
'TWIG_DEBUG' => true
'TWIG_DEBUG' => true,
'TWIG_TEMPLATES' => __DIR__
];

$injector = new Injector();
Expand All @@ -28,17 +30,19 @@ public function testThenDebugIsEnabled()
$configuration = $injector->make(TwigExtensionSet::class);
$configuration->apply($injector);

// Execute
$twig = $injector->make(Twig_Environment::class);

$this->assertTrue($twig->isDebug());
$this->assertArrayHasKey('debug', $twig->getExtensions());
// $this->assertArrayHasKey('Twig_Extension_Debug', $twig->getExtensions());
// Verify
self::assertTrue($twig->isDebug());
}

public function testThenExtensionIsInvalid()
{
$this->expectException(ExtensionException::class);
// Verify
self::expectException(ExtensionException::class);

// Execute
new TwigExtensionSet([new \stdClass]);
}
}
25 changes: 14 additions & 11 deletions tests/Exception/ExtensionExceptionTest.php
@@ -1,29 +1,32 @@
<?php

namespace AlexMasterov\EquipTwigTests\Exception;
namespace AlexMasterov\EquipTwig\Tests\Exception;

use AlexMasterov\EquipTwig\Exception\ExceptionInterface;
use AlexMasterov\EquipTwig\Exception\ExtensionException;
use InvalidArgumentException;
use PHPUnit_Framework_TestCase as TestCase;
use Twig_ExtensionInterface;
use stdClass;

class ExtensionExceptionTest extends TestCase
{
public function testInvalidExtension()
{
$extension = new stdClass;
$interface = Twig_ExtensionInterface::class;
// Stab
$extension = new \stdClass;
$message = sprintf(
'Twig extension `%s` must implement `%s`' ,
get_class($extension),
Twig_ExtensionInterface::class
);

// Execute
$exception = ExtensionException::invalidClass($extension);

$this->assertInstanceOf(ExtensionException::class, $exception);
$this->assertInstanceOf(InvalidArgumentException::class, $exception);
$this->assertInstanceOf(ExceptionInterface::class, $exception);
$this->assertSame(
'Twig extension `'. get_class($extension) .'` must implement `'. $interface .'`',
$exception->getMessage()
);
// Verify
self::assertInstanceOf(ExtensionException::class, $exception);
self::assertInstanceOf(InvalidArgumentException::class, $exception);
self::assertInstanceOf(ExceptionInterface::class, $exception);
self::assertSame($message, $exception->getMessage());
}
}
21 changes: 13 additions & 8 deletions tests/Exception/LoaderExceptionTest.php
@@ -1,6 +1,6 @@
<?php

namespace AlexMasterov\EquipTwigTests\Exception;
namespace AlexMasterov\EquipTwig\Tests\Exception;

use AlexMasterov\EquipTwig\Exception\ExceptionInterface;
use AlexMasterov\EquipTwig\Exception\LoaderException;
Expand All @@ -11,17 +11,22 @@ class LoaderExceptionTest extends TestCase
{
public function testLoaderException()
{
// Stab
$template = 'nowhere';
$where = __DIR__;
$message = sprintf(
'Unable to find template `%s` (looked into: %s).',
$template,
$where
);

// Execute
$exception = LoaderException::notFound($template, $where);

$this->assertInstanceOf(LoaderException::class, $exception);
$this->assertInstanceOf(Twig_Error_Loader::class, $exception);
$this->assertInstanceOf(ExceptionInterface::class, $exception);
$this->assertSame(
'Unable to find template `'. $template .'` (looked into: '. $where . ').',
$exception->getMessage()
);
// Verify
self::assertInstanceOf(LoaderException::class, $exception);
self::assertInstanceOf(Twig_Error_Loader::class, $exception);
self::assertInstanceOf(ExceptionInterface::class, $exception);
self::assertSame($message, $exception->getMessage());
}
}
116 changes: 53 additions & 63 deletions tests/Loader/FilesystemLoaderTest.php
@@ -1,105 +1,95 @@
<?php

namespace AlexMasterov\EquipTwigTests\Loader;
namespace AlexMasterov\EquipTwig\Tests\Loader;

use AlexMasterov\EquipTwigTests\Asset\Template;
use AlexMasterov\EquipTwig\Exception\LoaderException;
use AlexMasterov\EquipTwig\Loader\FilesystemLoader;
use PHPUnit_Framework_TestCase as TestCase;
use AlexMasterov\EquipTwig\Tests\TestCase;
use Twig_Source;

class FilesystemLoaderTest extends TestCase
{
public function testConstructor()
{
$fileExtensions = ['html.twig'];

$loader = new FilesystemLoader(
Template::path(),
$fileExtensions
);

$class = new \ReflectionClass($loader);

// path
$propPath = $class->getProperty('path');
$propPath->setAccessible(true);

$this->assertSame(Template::path(), $propPath->getValue($loader));

// fileExtensions
$propFileExtensions = $class->getProperty('fileExtensions');
$propFileExtensions->setAccessible(true);

$this->assertEquals($fileExtensions, $propFileExtensions->getValue($loader));
}

public function testGetSource()
{
$loader = new FilesystemLoader(Template::path());
$source = $loader->getSource(Template::name());

$this->assertSame(Template::code(), $source);
}

public function testGetSourceContext()
{
$loader = new FilesystemLoader(Template::path());
$source = $loader->getSourceContext(Template::name());
// Stab
$template = $this->template('test.html.twig');

$this->assertInstanceOf(Twig_Source::class, $source);
$this->assertSame(Template::name(), $source->getName());
$this->assertSame(Template::code(), $source->getCode());
// Execute
$loader = new FilesystemLoader($template->path());
$source = $loader->getSourceContext($template->name());

// Verify
self::assertInstanceOf(Twig_Source::class, $source);
self::assertSame($template->name(), $source->getName());
self::assertSame($template->code(), $source->getCode());
}

public function testGetCacheKey()
{
$loader = new FilesystemLoader(Template::path());
$loader->getSourceContext(Template::name());
// Stab
$template = $this->template('test.html.twig');

// Execute
$loader = new FilesystemLoader($template->path());
$loader->getSourceContext($template->name());

$cacheKey = $loader->getCacheKey(Template::name());
$cacheKey = $loader->getCacheKey($template->name());

$this->assertSame(Template::templatePath(), $cacheKey);
// Verify
self::assertSame(realpath($template->templatePath()), $cacheKey);
}

public function testIsFresh()
{
$loader = new FilesystemLoader(Template::path());
// Stab
$template = $this->template('test.html.twig');

$this->assertTrue(
$loader->isFresh(Template::name(), time())
// Execute
$loader = new FilesystemLoader($template->path());

// Verify
self::assertTrue(
$loader->isFresh($template->name(), time())
);
}

public function testExists()
{
$loader = new FilesystemLoader(Template::path(), ['html.twig']);
// Stab
$template = $this->template('test.html.twig');
$ext = 'html.twig';

$this->assertTrue(
$loader->exists(Template::name())
);
// Execute
$loader = new FilesystemLoader($template->path(), [$ext]);

// cached
$loader->getSourceContext(Template::name());
// Verify
self::assertTrue($loader->exists($template->name()));

$this->assertTrue(
$loader->exists(Template::name())
);
// Execute (with cached)
$loader->getSourceContext($template->name());

// without .ext
$name = strstr(Template::name(), '.', true);
$loader->getSourceContext($name);
// Verify
self::assertTrue($loader->exists($template->name()));

$this->assertTrue(
$loader->exists($name)
// Execute (without .ext)
$loader->getSourceContext(
basename($template->name(), ".{$ext}")
);

// Verify
self::assertTrue($loader->exists($template->name()));
}

public function testThenTemplateNotFound()
{
$this->expectException(LoaderException::class);
// Verify
self::expectException(LoaderException::class);

// Stab
$template = $this->template('test.html.twig');

$loader = new FilesystemLoader(Template::path());
// Execute
$loader = new FilesystemLoader($template->path());
$loader->getSourceContext('nowhere.html.twig');
}
}

0 comments on commit 026004c

Please sign in to comment.