Merge pull request #1065 from jim-parry/testing/commands

Testing/commands
This commit is contained in:
Lonnie Ezell 2018-06-06 08:25:39 -05:00 committed by GitHub
commit 4174a4c97d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 215 additions and 12 deletions

4
.gitignore vendored
View File

@ -63,6 +63,8 @@ writable/uploads/*
writable/debugbar/*
application/Database/Migrations/2*
php_errors.log
#-------------------------
@ -124,4 +126,4 @@ nb-configuration.xml
.vscode/
/results/
/phpunit.xml
/phpunit.xml

View File

@ -1,25 +1,68 @@
<?php namespace CodeIgniter\Commands\Server;
/**
* CodeIgniter
*
* An open source application development framework for PHP
*
* This content is released under the MIT License (MIT)
*
* Copyright (c) 2014-2018 British Columbia Institute of Technology
*
* 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.
*
* @package CodeIgniter
* @author CodeIgniter Dev Team
* @copyright 2014-2018 British Columbia Institute of Technology (https://bcit.ca/)
* @license https://opensource.org/licenses/MIT MIT License
* @link https://codeigniter.com
* @since Version 3.0.0
* @filesource
*/
use CodeIgniter\CLI\BaseCommand;
use CodeIgniter\CLI\CLI;
/**
* Launch the PHP development server
*
* Not testable, as it throws phpunit for a loop :-/
* @codeCoverageIgnore
*/
class Serve extends BaseCommand
{
protected $group = 'CodeIgniter';
protected $name = 'serve';
protected $group = 'CodeIgniter';
protected $name = 'serve';
protected $description = 'Launchs the CodeIgniter PHP-Development Server.';
protected $usage = 'serve';
protected $arguments = [];
protected $options = [
'-php' => 'The PHP Binary [default: "PHP_BINARY"]',
'-host' => 'The HTTP Host [default: "localhost"]',
'-port' => 'The HTTP Host Port [default: "8080"]',
protected $usage = 'serve';
protected $arguments = [];
protected $options = [
'-php' => 'The PHP Binary [default: "PHP_BINARY"]',
'-host' => 'The HTTP Host [default: "localhost"]',
'-port' => 'The HTTP Host Port [default: "8080"]',
];
public function run(array $params)
{
// Collect any user-supplied options and apply them
$php = CLI::getOption('php') ?? PHP_BINARY;
$php = CLI::getOption('php') ?? PHP_BINARY;
$host = CLI::getOption('host') ?? 'localhost';
$port = CLI::getOption('port') ?? '8080';
@ -38,4 +81,5 @@ class Serve extends BaseCommand
// to ensure our environment is set and it simulates basic mod_rewrite.
passthru("{$php} -S {$host}:{$port} -t {$docroot} {$rewrite}");
}
}

View File

@ -6,8 +6,9 @@
* development server based around PHP's built-in development
* server. This file simply tries to mimic Apache's mod_rewrite
* functionality so the site will operate as normal.
*
*/
// @codeCoverageIgnoreStart
// Avoid this file run when listing commands
if (php_sapi_name() === 'cli')
{
@ -36,3 +37,4 @@ if ($uri !== '/' && (is_file($path) || is_dir($path)))
// Otherwise, we'll load the index file and let
// the framework handle the request from here.
require_once $fcpath . 'index.php';
// @codeCoverageIgnoreEnd

View File

@ -39,6 +39,12 @@ use CodeIgniter\CLI\BaseCommand;
use CodeIgniter\CLI\CLI;
use Config\App;
/**
* Creates a migration file for database sessions.
*
* @package CodeIgniter\Commands
*/
class CreateMigration extends BaseCommand
{

View File

@ -0,0 +1,17 @@
<?php namespace CodeIgniter\Commands;
class CommandsTestStreamFilter extends \php_user_filter
{
public static $buffer = '';
public function filter($in, $out, &$consumed, $closing)
{
while ($bucket = stream_bucket_make_writeable($in)) {
self::$buffer .= $bucket->data;
$consumed += $bucket->datalen;
}
return PSFS_PASS_ON;
}
}
stream_filter_register('CommandsTestStreamFilter', 'CodeIgniter\Commands\CommandsTestStreamFilter');

View File

@ -1,4 +1,4 @@
<?php
<?php namespace Tests\Support\Database\Seeds;
class CITestSeeder extends \CodeIgniter\Database\Seeder
{

View File

@ -0,0 +1,62 @@
<?php namespace CodeIgniter\Commands;
use Config\MockAppConfig;
use CodeIgniter\HTTP\UserAgent;
use CodeIgniter\CLI\CLI;
use CodeIgniter\CLI\CommandRunner;
class CommandsTest extends \CIUnitTestCase
{
private $stream_filter;
public function setUp()
{
CommandsTestStreamFilter::$buffer = '';
$this->stream_filter = stream_filter_append(STDOUT, 'CommandsTestStreamFilter');
$this->env = new \CodeIgniter\Config\DotEnv(ROOTPATH);
$this->env->load();
// Set environment values that would otherwise stop the framework from functioning during tests.
if ( ! isset($_SERVER['app.baseURL']))
{
$_SERVER['app.baseURL'] = 'http://example.com';
}
$_SERVER['argv'] = ['spark', 'list'];
$_SERVER['argc'] = 2;
CLI::init();
$this->config = new MockAppConfig();
$this->request = new \CodeIgniter\HTTP\IncomingRequest($this->config, new \CodeIgniter\HTTP\URI('https://somwhere.com'), null, new UserAgent());
$this->response = new \CodeIgniter\HTTP\Response($this->config);
$this->runner = new CommandRunner($this->request, $this->response);
}
public function tearDown()
{
stream_filter_remove($this->stream_filter);
}
public function testHelpCommand()
{
$this->runner->index(['help']);
$result = CommandsTestStreamFilter::$buffer;
// make sure the result looks like a command list
$this->assertContains('Displays basic usage information.', $result);
$this->assertContains('command_name', $result);
}
public function testListCommands()
{
$this->runner->index(['list']);
$result = CommandsTestStreamFilter::$buffer;
// make sure the result looks like a command list
$this->assertContains('Lists the available commands.', $result);
$this->assertContains('Displays basic usage information.', $result);
}
}

View File

@ -0,0 +1,70 @@
<?php namespace CodeIgniter\Commands;
use Config\MockAppConfig;
use CodeIgniter\HTTP\UserAgent;
use CodeIgniter\CLI\CLI;
use CodeIgniter\CLI\CommandRunner;
class SessionsCommandsTest extends \CIUnitTestCase
{
private $stream_filter;
public function setUp()
{
CommandsTestStreamFilter::$buffer = '';
$this->stream_filter = stream_filter_append(STDOUT, 'CommandsTestStreamFilter');
$this->env = new \CodeIgniter\Config\DotEnv(ROOTPATH);
$this->env->load();
// Set environment values that would otherwise stop the framework from functioning during tests.
if ( ! isset($_SERVER['app.baseURL']))
{
$_SERVER['app.baseURL'] = 'http://example.com';
}
$_SERVER['argv'] = ['spark', 'list'];
$_SERVER['argc'] = 2;
CLI::init();
$this->config = new MockAppConfig();
$this->request = new \CodeIgniter\HTTP\IncomingRequest($this->config, new \CodeIgniter\HTTP\URI('https://somwhere.com'), null, new UserAgent());
$this->response = new \CodeIgniter\HTTP\Response($this->config);
$this->runner = new CommandRunner($this->request, $this->response);
}
public function tearDown()
{
stream_filter_remove($this->stream_filter);
}
public function testCreateMigrationCommand()
{
$this->runner->index(['session:migration']);
$result = CommandsTestStreamFilter::$buffer;
// make sure we end up with a migration class in the right place
// or at least that we claim to have done so
// separate assertions avoid console color codes
$this->assertContains('Created file:', $result);
$this->assertContains('APPPATH/Database/Migrations/', $result);
$this->assertContains('_create_ci_sessions_table.php', $result);
}
public function testOverriddenCreateMigrationCommand()
{
$_SERVER['argv'] = ['spark','session:migration', '-t', 'mygoodies'];
$_SERVER['argc'] = 4;
CLI::init();
$this->runner->index(['session:migration']);
$result = CommandsTestStreamFilter::$buffer;
// make sure we end up with a migration class in the right place
$this->assertContains('Created file:', $result);
$this->assertContains('APPPATH/Database/Migrations/', $result);
$this->assertContains('_create_mygoodies_table.php', $result);
}
}