Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
maryo committed Sep 23, 2016
1 parent 8960a63 commit 7003762
Show file tree
Hide file tree
Showing 21 changed files with 4,537 additions and 2 deletions.
13 changes: 13 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/vendor/
/var/
/composer.phar

.DS_Store
*.swp
*.swo
.local.vimrc
.vimrc.local
*~
/.idea/
/nbproject/
/local/
20 changes: 20 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
language: php

cache:
directories:
$HOME/.composer/cache/files

php:
- 7.0

install:
- php -n ~/.phpenv/versions/$(phpenv version-name)/bin/composer self-update
- php -n ~/.phpenv/versions/$(phpenv version-name)/bin/composer require satooshi/php-coveralls
- php -n ~/.phpenv/versions/$(phpenv version-name)/bin/composer update

script:
- php -n ~/.phpenv/versions/$(phpenv version-name)/bin/composer lint
- phpunit --coverage-clover build/logs/clover.xml

after_success:
- travis_retry php -n vendor/bin/coveralls
5 changes: 5 additions & 0 deletions AppKernel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php
use Vanio\TestingBundle\HttpKernel\TestKernel;

class AppKernel extends TestKernel
{}
50 changes: 50 additions & 0 deletions DependencyInjection/TestContainerBuilder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php
namespace Vanio\TestingBundle\DependencyInjection;

use Symfony\Component\Config\Definition\Processor;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Extension\Extension;
use Symfony\Component\DependencyInjection\ParameterBag\FrozenParameterBag;

/**
* @method Extension getExtension(string $name)
*/
class TestContainerBuilder extends ContainerBuilder
{
/**
* Compiles the container without unsetting configuration of bundle extensions for testing purposes.
*/
public function compile()
{
$compiler = $this->getCompiler();

if ($this->isTrackingResources()) {
foreach ($compiler->getPassConfig()->getPasses() as $pass) {
$this->addObjectResource($pass);
}
}

$compiler->compile($this);

if ($this->isTrackingResources()) {
foreach ($this->getDefinitions() as $definition) {
$class = $definition->getClass();

if ($class && $definition->isLazy() && class_exists($class)) {
$this->addClassResource(new \ReflectionClass($class));
}
}
}

$this->parameterBag->resolve();
$this->parameterBag = new FrozenParameterBag($this->parameterBag->all());
}

public function processExtensionConfig(string $name): array
{
return (new Processor)->processConfiguration(
$this->getExtension($name)->getConfiguration([], $this),
$this->getExtensionConfig($name)
);
}
}
57 changes: 57 additions & 0 deletions HttpKernel/TestKernel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php
namespace Vanio\TestingBundle\HttpKernel;

use Symfony\Component\Config\Loader\LoaderInterface;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
use Symfony\Component\HttpKernel\Bundle\BundleInterface;
use Symfony\Component\HttpKernel\Kernel;
use Vanio\TestingBundle\DependencyInjection\TestContainerBuilder;

abstract class TestKernel extends Kernel
{
/** @var TestContainerBuilder */
protected $container;

public function __construct(string $environment = 'default', bool $debug = true)
{
parent::__construct($environment, $debug);
}

/**
* @return BundleInterface[]
*/
public function registerBundles(): array
{
return require sprintf('%s/Tests/Functional/app/%s/bundles.php', $this->rootDir, $this->environment);
}

public function registerContainerConfiguration(LoaderInterface $loader)
{
$loader->load(sprintf('%s/Tests/Functional/app/%s/config.yml', $this->rootDir, $this->environment));
}

public function getCacheDir(): string
{
return sprintf('%s/var/cache/%s', $this->rootDir, $this->environment);
}

public function getLogDir(): string
{
return sprintf('%s/var/logs/%s', $this->rootDir, $this->environment);
}

/**
* Initializes the container while keeping the container builder instance for testing purposes.
*/
protected function initializeContainer()
{
$this->container = $this->buildContainer();
$this->container->compile();
$this->container->set('kernel', $this);
}

protected function getContainerBuilder(): TestContainerBuilder
{
return new TestContainerBuilder(new ParameterBag($this->getKernelParameters()));
}
}
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2016 Vanio Solutions

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
17 changes: 17 additions & 0 deletions PhpUnit/KernelTestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php
namespace Vanio\TestingBundle\PhpUnit;

use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase as BaseKernelTestCase;
use Vanio\TestingBundle\DependencyInjection\TestContainerBuilder;

abstract class KernelTestCase extends BaseKernelTestCase
{
/** @var TestContainerBuilder */
protected static $container;

protected static function boot(array $options = ['environment' => 'default'])
{
self::bootKernel($options);
self::$container = self::$kernel->getContainer();
}
}
84 changes: 84 additions & 0 deletions PhpUnit/WebTestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?php
namespace Vanio\TestingBundle\PhpUnit;

use Html2Text\Html2Text;
use Symfony\Bundle\FrameworkBundle\Client;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase as BaseWebTestCase;
use Symfony\Component\DomCrawler\Crawler;
use Symfony\Component\HttpFoundation\Response;
use Vanio\TestingBundle\DependencyInjection\TestContainerBuilder;

class WebTestCase extends BaseWebTestCase
{
/** @var Client|null */
protected static $client;

/** @var TestContainerBuilder|null */
protected static $container;

/** @var Crawler|null */
protected $crawler;

protected static function boot(array $options = ['environment' => 'default'])
{
self::$client = self::createClient($options);
self::bootKernel($options);
self::$container = self::$kernel->getContainer();
}

protected function request(
string $method,
string $uri,
array $parameters = [],
array $files = [],
array $server = [],
string $content = null,
bool $changeHistory = true
): Response {
$this->crawler = self::$client->request($method, $uri, $parameters, $files, $server, $content, $changeHistory);

return self::$client->getResponse();
}

/**
* @param string|string[] $text
* @param string $html
*/
protected function assertHtmlContainsText($text, string $html)
{
$haystack = $this->convertHtmlToText($html);

foreach ((array) $text as $needle) {
$this->assertContains($this->convertHtmlToText($needle), $haystack);
}
}

/**
* @param string|string[] $text
* @param string $html
*/
protected function assertHtmlNotContainsText($text, string $html)
{
$haystack = $this->convertHtmlToText($html);

foreach ((array) $text as $needle) {
$this->assertNotContains($this->convertHtmlToText($needle), $haystack);
}
}

/**
* @param string $html
* @return string
*/
private function convertHtmlToText(string $html): string
{
$text = (new Html2Text($html))->getText();

return $this->normalizeWhitespace($text);
}

private function normalizeWhitespace(string $text): string
{
return preg_replace('~\s+~', ' ', $text);
}
}
Loading

0 comments on commit 7003762

Please sign in to comment.