Skip to content
This repository was archived by the owner on Aug 20, 2018. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# EditorConfig is awesome: http://EditorConfig.org

# Top-most EditorConfig file
root = true

# Unix-style newlines with a newline ending every file
[*]
end_of_line = lf
insert_final_newline = true

# JS / PHP
[*.{js,php,phpt}]
charset = utf-8
indent_style = tab
indent_size = 4

# NEON
[*.neon]
charset = utf-8
indent_style = tab
indent_size = 4

# Composer, NPM, Travis, BitbucketPipelines
[{composer.json,package.json,.travis.yml,bitbucket-pipelines.yml}]
indent_style = space
indent_size = 2
12 changes: 8 additions & 4 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
.gitattributes export-ignore
.gitignore export-ignore
.travis.yml export-ignore
tests/ export-ignore
/.docs export-ignore
/tests export-ignore
/.editorconfig export-ignore
/.gitattributes export-ignore
/.gitignore export-ignore
/.travis.yml export-ignore
/LICENSE export-ignore
/phpstan.neon export-ignore
13 changes: 10 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
.idea/
vendor/
# IDE
/.idea

composer.lock
# Composer
/vendor
/composer.lock

# Tests
/tests/*.log
/tests/tmp
/tests/coverage.html
File renamed without changes.
42 changes: 32 additions & 10 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,18 @@
}
],
"require": {
"php": ">=5.6.0",
"kdyby/fake-session": "^2.0",
"nette/application": "^2.4 || ^v3.0",
"nette/bootstrap": "^2.4 || ^v3.0",
"nette/di": "^2.4 || ^v3.0",
"nette/safe-stream": "^2.3",
"nette/security": "^2.4 || ^v3.0",
"nette/tester": "^2.0",
"nette/utils": "^2.4 || ^v3.0"
"php": ">=7.1",
"kdyby/fake-session": "^2.0.0",
"nette/application": "^2.4.12 || ^3.0",
"nette/bootstrap": "^2.4.6 || ^3.0",
"nette/di": "^2.4.13 || ^3.0",
"nette/safe-stream": "^2.3.3",
"nette/security": "^2.4.3 || ^3.0",
"nette/tester": "^2.0.2",
"nette/utils": "^2.5.2 || ^v3.0"
},
"require-dev": {
"ninjify/qa": "^0.8.0",
"latte/latte": "^2.4",
"nette/forms": "^2.4 || ^v3.0",
"nette/robot-loader": "^2.4 || ^v3.0",
Expand All @@ -36,6 +37,8 @@
"nette/database": "^2.4 || ^v3.0",
"zenify/doctrine-migrations": "^2.3"
},
"prefer-stable": true,
"minimum-stability": "dev",
"autoload": {
"psr-4": {
"Testbench\\": [
Expand All @@ -53,12 +56,31 @@
]
},
"bin": [
"src/run-tests"
"bin/testbench"
],
"suggest": {
"kdyby/doctrine": "Allows enhanced database tests using Doctrine",
"kdyby/doctrine-dbal-batchimport": "Allows SQL scripts import for Doctrine (required with kdyby/doctrine)",
"nette/database": "Allows enhanced database tests using Nette\\Database",
"zenify/doctrine-migrations": "Migrate database to the fresh state"
},
"scripts": {
"qa": [
"linter src tests",
"codesniffer src tests"
],
"phpstan-install": [
"mkdir -p temp/phpstan",
"composer require -d temp/phpstan phpstan/phpstan:0.10",
"composer require -d temp/phpstan phpstan/phpstan-nette:0.10"
],
"phpstan": [
"temp/phpstan/vendor/bin/phpstan analyse -l max -c phpstan.neon src"
]
},
"extra": {
"branch-alias": {
"dev-develop": "3.0.x-dev"
}
}
}
23 changes: 23 additions & 0 deletions ruleset.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version="1.0"?>
<ruleset name="Contributte">
<!-- Contributte Coding Standard -->
<rule ref="./vendor/ninjify/coding-standard/contributte.xml">
<exclude name="SlevomatCodingStandard.Commenting.ForbiddenAnnotations.AnnotationForbidden" />
</rule>

<!-- Specific rules -->
<rule ref="SlevomatCodingStandard.Files.TypeNameMatchesFileName">
<properties>
<property name="rootNamespaces" type="array" value="
src=>Testbench,
"/>
</properties>
</rule>

<rule ref="SlevomatCodingStandard.Namespaces.ReferenceUsedNamesOnly.ReferenceViaFullyQualifiedName">
<exclude-pattern>/src/Response/CSVResponse.php</exclude-pattern>
</rule>

<!--Exclude folders -->
<exclude-pattern>/tests</exclude-pattern>
</ruleset>
11 changes: 7 additions & 4 deletions src/Bootstrap.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
<?php
<?php declare(strict_types = 1);

namespace Testbench;

use Tester\Environment;
use Tracy\Debugger;

class Bootstrap
{

Expand All @@ -10,7 +13,7 @@ class Bootstrap
/** @var callable */
public static $onBeforeContainerCreate;

public static function setup($tempDir, $callback = NULL)
public static function setup($tempDir, $callback = null): void
{
if (!class_exists('Tester\Assert')) {
echo "Install Nette Tester using `composer update --dev`\n";
Expand All @@ -21,12 +24,12 @@ public static function setup($tempDir, $callback = NULL)

umask(0);
if (!ob_get_level() > 0) { //\Tester\Environment::setup already called
\Tester\Environment::setup();
Environment::setup();
}
date_default_timezone_set('Europe/Prague');

if (class_exists('Tracy\Debugger')) {
\Tracy\Debugger::$logDirectory = self::$tempDir;
Debugger::$logDirectory = self::$tempDir;
}

$_ENV = $_GET = $_POST = $_FILES = [];
Expand Down
51 changes: 29 additions & 22 deletions src/ContainerFactory.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,23 @@
<?php
<?php declare(strict_types = 1);

namespace Testbench;

use Exception;
use Kdyby\Console\DI\ConsoleExtension;
use Kdyby\FakeSession\DI\FakeSessionExtension;
use Nette\Configurator;
use Nette\DI\Compiler;
use Nette\DI\Container;
use Nette\DI\Statement;
use Nette\SmartObject;

/**
* @internal
*/
class ContainerFactory
{
use \Nette\SmartObject;

use SmartObject;

private static $container;

Expand All @@ -16,29 +26,26 @@ private function __construct()
//Cannot be initialized
}

/**
* @return \Nette\DI\Container
*/
final public static function create($new = FALSE, $config = [])
final public static function create($new = false, $config = []): Container
{
if ($new || self::$container === NULL) {
$configurator = new \Nette\Configurator();
if ($new || self::$container === null) {
$configurator = new Configurator();
$configurator->addParameters($config);

$configurator->onCompile[] = function (\Nette\Configurator $configurator, \Nette\DI\Compiler $compiler) use ($config) {
$configurator->onCompile[] = function (Configurator $configurator, Compiler $compiler) use ($config): void {
$compiler->addConfig($config);
$compiler->addExtension('testbench', new \Testbench\TestbenchExtension);
self::registerAdditionalExtension($compiler, 'fakeSession', new \Kdyby\FakeSession\DI\FakeSessionExtension);
$compiler->addExtension('testbench', new TestbenchExtension());
self::registerAdditionalExtension($compiler, 'fakeSession', new FakeSessionExtension());
if (class_exists('Kdyby\Console\DI\ConsoleExtension')) {
self::registerAdditionalExtension($compiler, 'console', new \Kdyby\Console\DI\ConsoleExtension);
self::registerAdditionalExtension($compiler, 'console', new ConsoleExtension());
}
};

$configurator->setTempDirectory(\Testbench\Bootstrap::$tempDir); // shared container for performance purposes
$configurator->setDebugMode(FALSE);
$configurator->setTempDirectory(Bootstrap::$tempDir); // shared container for performance purposes
$configurator->setDebugMode(false);

if (is_callable(\Testbench\Bootstrap::$onBeforeContainerCreate)) {
call_user_func_array(\Testbench\Bootstrap::$onBeforeContainerCreate, [$configurator]);
if (is_callable(Bootstrap::$onBeforeContainerCreate)) {
call_user_func_array(Bootstrap::$onBeforeContainerCreate, [$configurator]);
}

self::$container = $configurator->createContainer();
Expand All @@ -49,14 +56,14 @@ final public static function create($new = FALSE, $config = [])
/**
* Register extension if not registered by user.
*/
private static function registerAdditionalExtension(\Nette\DI\Compiler $compiler, $name, $newExtension)
private static function registerAdditionalExtension(Compiler $compiler, $name, $newExtension): void
{
$extensions = [];
$config = $compiler->getConfig();
foreach (isset($config['extensions']) ? $config['extensions'] : [] as $extension) {
foreach ($config['extensions'] ?? [] as $extension) {
if (is_string($extension)) {
$extensions[] = $extension;
} elseif ($extension instanceof \Nette\DI\Statement) {
} elseif ($extension instanceof Statement) {
$extensions[] = $extension->getEntity();
}
}
Expand All @@ -67,12 +74,12 @@ private static function registerAdditionalExtension(\Nette\DI\Compiler $compiler

final public function __clone()
{
throw new \Exception('Clone is not allowed');
throw new Exception('Clone is not allowed');
}

final public function __wakeup()
final public function __wakeup(): void
{
throw new \Exception('Unserialization is not allowed');
throw new Exception('Unserialization is not allowed');
}

}
10 changes: 5 additions & 5 deletions src/DatabasesRegistry.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php
<?php declare(strict_types = 1);

namespace Testbench;

Expand All @@ -9,13 +9,13 @@ class DatabasesRegistry

public function __construct()
{
$this->dataFile = 'nette.safe://' . \Testbench\Bootstrap::$tempDir . '/../databases.testbench';
$this->dataFile = 'nette.safe://' . Bootstrap::$tempDir . '/../databases.testbench';
}

/**
* @return TRUE if registration successful or FALSE if database record already exists
*/
public function registerDatabase($databaseName)
public function registerDatabase(string $databaseName): bool
{
if (file_exists($this->dataFile)) {
$data = file_get_contents($this->dataFile);
Expand All @@ -28,9 +28,9 @@ public function registerDatabase($databaseName)
fwrite($handle, $databaseName . "\n");
fclose($handle);

return TRUE;
return true;
} else { //database already exists in log file
return FALSE;
return false;
}
}

Expand Down
21 changes: 18 additions & 3 deletions src/Mocks/ApplicationRequestMock.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,26 @@
<?php
<?php declare(strict_types = 1);

namespace Testbench\Mocks;

class ApplicationRequestMock extends \Nette\Application\Request
use Nette\Application\Request;

class ApplicationRequestMock extends Request
{

public function __construct($name = NULL, $method = NULL, array $params = [], array $post = [], array $files = [], array $flags = [])
/**
* @param mixed[] $params
* @param mixed[] $post
* @param mixed[] $files
* @param mixed[] $flags
*/
public function __construct(
?string $name = null,
?string $method = null,
array $params = [],
array $post = [],
array $files = [],
array $flags = []
)
{
$name = $name ?: 'Foo'; //It's going to be terminated anyway (see: \PresenterMock::afterRender)
parent::__construct($name, $method, $params, $post, $files, $flags);
Expand Down
8 changes: 5 additions & 3 deletions src/Mocks/ControlMock.php
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
<?php
<?php declare(strict_types = 1);

namespace Testbench\Mocks;

class ControlMock extends \Nette\Application\UI\Control
use Nette\Application\UI\Control;

class ControlMock extends Control
{

public function link($destination, $args = [])
{
if (!is_array($args)) {
$args = array_slice(func_get_args(), 1);
}
$params = urldecode(http_build_query($args, NULL, ', '));
$params = urldecode(http_build_query($args, null, ', '));
$params = $params ? "($params)" : '';
return "link|$destination$params";
}
Expand Down
Loading