Skip to content

Commit

Permalink
Initial Commit
Browse files Browse the repository at this point in the history
Using the base Dusk test files from Laravel. Also uses a customised
artisan:serve (artisan orchestra:serve) to provide a locally driven
server to provide what is effectively the site during testing.

The server uses the current Test Class to derive the application state
to use when responding to the request.
  • Loading branch information
Keoghan committed Oct 20, 2017
0 parents commit f361899
Show file tree
Hide file tree
Showing 18 changed files with 835 additions and 0 deletions.
1 change: 1 addition & 0 deletions .coveralls.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
service_name: travis-ci
16 changes: 16 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
* text=auto

# Ignore following folder/file.
/build export-ignore
/docs export-ignore
/tests export-ignore
/.coveralls.yml export-ignore
/.gitattributes export-ignore
/.gitignore export-ignore
/.php_cs export-ignore
/.scrutinizer.yml export-ignore
/.travis.yml export-ignore
/phpunit.xml export-ignore
/CHANGELOG.md export-ignore
/LICENSE export-ignore
/README.md export-ignore
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Composer
composer.lock
vendor/*
tests/Browser/console
tests/Browser/screenshots
tmp/*
19 changes: 19 additions & 0 deletions .php_cs.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php
$finder = PhpCsFixer\Finder::create()
->in(__DIR__.'/src')
->in(__DIR__.'/tests');

return PhpCsFixer\Config::create()
->setRiskyAllowed(false)
->setRules([
'@Symfony' => true,
'binary_operator_spaces' => ['align_double_arrow' => false, 'align_equals' => false],
'no_empty_comment' => false,
'no_extra_consecutive_blank_lines' => false,
'not_operator_with_successor_space' => true,
'ordered_imports' => ['sortAlgorithm' => 'length'],
'phpdoc_align' => false,
'phpdoc_no_empty_return' => false,
'yoda_style' => false,
])
->setFinder($finder);
39 changes: 39 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
language: php

sudo: false

php:
- 7.0
- 7.1
- 7.2

env:
global:
- setup=basic
- coverage=no

before_script:
- composer config discard-changes true
- if [[ $setup = 'basic' ]]; then travis_retry composer install --prefer-dist --no-interaction; fi
- if [[ $setup = 'stable' ]]; then travis_retry composer update --prefer-dist --no-interaction --prefer-stable; fi
- if [[ $setup = 'lowest' ]]; then travis_retry composer update --prefer-dist --no-interaction --prefer-lowest --prefer-stable; fi
- if [[ $setup = 'coveralls' ]]; then travis_retry composer require "satooshi/php-coveralls=~0.7" --prefer-dist --no-interaction --dev; fi

script:
- if [[ $coverage = 'yes' ]]; then ./vendor/bin/phpunit -c phpunit.xml --coverage-clover build/logs/clover.xml; fi
- if [[ $coverage = 'no' ]]; then ./vendor/bin/phpunit -c phpunit.xml; fi

after_script:
- if [[ $setup = 'coveralls' ]]; then php vendor/bin/coveralls -v; fi

matrix:
include:
- php: 7.0
env: setup=lowest
- php: 7.0
env: setup=stable
- php: 7.0
env: setup=coveralls coverage=yes
allow_failures:
- env: setup=coveralls coverage=yes
fast_finish: true
20 changes: 20 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
The MIT License (MIT)

Copyright (C) 2017 Konsulting Ltd <http://klever.co.uk>

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.
58 changes: 58 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
Laravel Dusk Kit Testing Helper for Packages Development
==============

Testbench Component is a simple package that is supposed to help you write tests for your Laravel package, especially when there is routing involved.

This is a package developed by Konsulting Ltd to (we hope) be transferred to the original Orchestra namespace.

* [Version Compatibility](#version-compatibility)
* [Installation](#installation)
* [Usage](#usage)
* [Changelog](https://github.com/orchestral/testbench-browser-kit/releases)

## Version Compatibility

Laravel | Testbench Browser Kit
:---------|:----------
5.5.x | 3.5.x

## Installation

To install through composer, simply put the following in your `composer.json` file:

```json
{
"require-dev": {
"konsnulting/testbench-dusk": "~3.5"
}
}
```

And then run `composer install` from the terminal.

### Quick Installation

Above installation can also be simplify by using the following command:

composer require --dev "orchestra/testbench-dusk=~3.5"

## Usage

Use the `Orchestra\Testbench\Dusk\TestCase` as the parent class for your test. Optionally, extend that class to provide a custom base class for your project.

You can also separate your tests in your `phpunit.xml` file by providing different testsuites. For example:
```xml
<testsuites>
<testsuite name="Browser">
<directory suffix="Test.php">./tests/Browser</directory>
</testsuite>
<testsuite name="Feature">
<directory suffix="Test.php">./tests/Feature</directory>
</testsuite>
<testsuite name="Unit">
<directory suffix="Test.php">./tests/Unit</directory>
</testsuite>
</testsuites>
```

You can optionally set the default testsuite with the option `defaultTestSuite="Unit"`
49 changes: 49 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
{
"name": "konsulting/testbench-dusk",
"description": "Laravel Dusk Testing Helper for Packages Development",
"type": "library",
"license": "MIT",
"authors": [
{
"name": "Keoghan Litchfield",
"email": "[email protected]",
"homepage": "https://klever.co.uk"
},
{
"name": "Mior Muhammad Zaki",
"email": "[email protected]",
"homepage": "https://github.com/crynobone"
}
],
"autoload": {
"psr-4": {
"Orchestra\\Testbench\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"Orchestra\\Testbench\\Tests\\": "tests/"
}
},
"require": {
"php": ">=7.0",
"laravel/dusk": "~2.0",
"orchestra/testbench": "~3.5.0"
},
"require-dev": {
"mockery/mockery": "~1.0",
"orchestra/database": "~3.5.0",
"phpunit/phpunit": "~6.0"
},
"suggest": {
"orchestra/database": "Allow to use --realpath migration for testing (~3.5)."
},
"extra": {
"branch-alias": {
"dev-master": "3.5-dev"
}
},
"config": {
"sort-packages": true
}
}
27 changes: 27 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
backupStaticAttributes="false"
bootstrap="vendor/autoload.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
syntaxCheck="false">

<testsuites>
<testsuite name="Orchestra\Testbench Test Suite">
<directory suffix="Test.php">./tests/</directory>
</testsuite>
</testsuites>
<php>
<env name="APP_ENV" value="testing"/>
</php>

<filter>
<whitelist addUncoveredFilesFromWhitelist="false">
<directory suffix=".php">src/</directory>
</whitelist>
</filter>
</phpunit>
30 changes: 30 additions & 0 deletions src/Concerns/CanServeSite.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace Orchestra\Testbench\Concerns;

use Orchestra\Testbench\ForkedServer;

trait CanServeSite
{
protected $forkedServers = [];

public function serve($port = 8000)
{
$this->forkedServers[$port] = new ForkedServer($port);
$this->forkedServers[$port]->stash(static::class);
$this->forkedServers[$port]->start();
}

public function stopServing($port = 8000)
{
if (isset($this->forkedServers[$port])) {
$this->forkedServers[$port]->stop();
return;
}

foreach (array_keys($this->forkedServers) as $p) {
$this->stopServing($p);
}
throw new Exception("No server on port {$port}. We stopped them all.");
}
}
39 changes: 39 additions & 0 deletions src/Console/ServeCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace Orchestra\Testbench\Console;

use Illuminate\Foundation\Console\ServeCommand as BaseServeCommand;
use Symfony\Component\Process\PhpExecutableFinder;
use Symfony\Component\Process\ProcessUtils;

class ServeCommand extends BaseServeCommand
{
/**
* The console command name.
*
* @var string
*/
protected $name = 'orchestra:serve';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Serve the application on the PHP development server';

/**
* Get the full server command.
*
* @return string
*/
protected function serverCommand()
{
return sprintf('%s -S %s:%s %s/server.php',
ProcessUtils::escapeArgument((new PhpExecutableFinder)->find(false)),
$this->host(),
$this->port(),
ProcessUtils::escapeArgument(dirname(__DIR__))
);
}
}
65 changes: 65 additions & 0 deletions src/ConsoleServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php

namespace Orchestra\Testbench;

use Illuminate\Support\ServiceProvider;
use Illuminate\Contracts\Foundation\Application;
use Orchestra\Testbench\Console\ServeCommand;

class ConsoleServiceProvider extends ServiceProvider
{
/**
* Indicates if loading of the provider is deferred.
*
* @var bool
*/
protected $defer = true;

/**
* Register all of the migration commands.
*
* @return void
*/
public function register()
{
$commands = ['Serve'];

// We'll simply spin through the list of commands that are migration related
// and register each one of them with an application container. They will
// be resolved in the Artisan start file and registered on the console.
foreach ($commands as $command) {
$this->{'register'.$command.'Command'}();
}

// Once the commands are registered in the application IoC container we will
// register them with the Artisan start event so that these are available
// when the Artisan application actually starts up and is getting used.
$this->commands(
'command.serve'
);
}

/**
* Register the command.
*
* @return void
*/
protected function registerServeCommand()
{
$this->app->singleton('command.serve', function () {
return new ServeCommand();
});
}

/**
* Get the services provided by the provider.
*
* @return array
*/
public function provides()
{
return [
'command.serve',
];
}
}
Loading

0 comments on commit f361899

Please sign in to comment.