Skip to content
Merged
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
11 changes: 8 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ composer require pavlakis/slim-cli

### Pass the parameters in this order
`route / method / query string`
```
```php
php public/index.php /status GET event=true
```

Expand All @@ -22,7 +22,7 @@ $app->add(new \pavlakis\cli\CliRequest());

### Pass a route to test it with

```
```php

$app->get('/status', 'PHPMinds\Action\EventStatusAction:dispatch')
->setName('status');
Expand All @@ -31,7 +31,7 @@ $app->get('/status', 'PHPMinds\Action\EventStatusAction:dispatch')

### Check you're only using a CLI call

```
```php
final class EventStatusAction
{
...
Expand All @@ -54,3 +54,8 @@ final class EventStatusAction

}
```


### Credits

Based on Bobby DeVeaux's ([@bobbyjason](https://twitter.com/bobbyjason)) [Gulp Skeleton](https://github.com/dvomedia/gulp-skeleton/blob/master/web/index.php)
51 changes: 46 additions & 5 deletions src/CliRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
* A Slim 3 middleware enabling a mock GET request to be made through the CLI.
* Use in the form: php public/index.php /status GET event=true
*
* @link https://github.com/pavlakis/slim-cli
* @copyright Copyright © 2015-2016 Antonis Pavlakis
* @license https://github.com/pavlakis/slim-cli/blob/master/LICENSE (BSD 3-Clause License)
* @link https://github.com/pavlakis/slim-cli
* @copyright Copyright © 2015-2016 Antonis Pavlakis
* @author Bobby DeVeaux (@bobbyjason) Based on Bobby's code from: https://github.com/dvomedia/gulp-skeleton/blob/master/web/index.php
* @license https://github.com/pavlakis/slim-cli/blob/master/LICENSE (BSD 3-Clause License)
*/
namespace pavlakis\cli;

Expand All @@ -30,6 +31,44 @@ public function getRequest()
return $this->request;
}

/**
* Get a value from an array if exists otherwise return a default value
*
* @param array $argv
* @param integer $key
* @param mixed $default
* @return string
*/
private function get($argv, $key, $default = '')
{
if (!array_key_exists($key, $argv)) {
return $default;
}

return $argv[$key];
}

/**
* Construct the URI if path and params are being passed
*
* @param string $path
* @param string $params
* @return string
*/
private function getUri($path, $params)
{
$uri = '';
if (strlen($path) === 0) {
$path = '/';
}

if (strlen($params) !== 0) {
$uri = $path . '?' . $params;
}

return $uri;
}

/**
* Invoke middleware
*
Expand All @@ -47,12 +86,14 @@ public function __invoke(ServerRequestInterface $request, ResponseInterface $res

if (isset($argv)) {

list($call, $path, $method, $params) = $argv;
$path = $this->get($argv, 1);
$method = $this->get($argv, 2);
$params = $this->get($argv, 3);

if (strtoupper($method) === 'GET') {
$this->request = \Slim\Http\Request::createFromEnvironment(\Slim\Http\Environment::mock([
'REQUEST_METHOD' => 'GET',
'REQUEST_URI' => $path . '?' . $params,
'REQUEST_URI' => $this->getUri($path, $params),
'QUERY_STRING' => $params
]));
}
Expand Down
19 changes: 19 additions & 0 deletions tests/phpunit/CliRequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,25 @@ public function testCorrectRequestParametersArePassed()
$this->assertEquals('event=true', $cliRequest->getRequest()->getUri()->getQuery());
}

public function testMinimalCorrectRequestParametersArePassed()
{
$req = $this->requestFactory();
$res = new Response();
$next = function (Request $req, Response $res) {
return $res;
};

unset($GLOBALS['argv'][3]);

/** @var CliRequest $cliRequest */
$cliRequest = new CliRequest();

/** @var ResponseInterface $res */
$res = $cliRequest($req, $res, $next);

$this->assertEquals('', $cliRequest->getRequest()->getUri()->getQuery());
}

public function testRequestPathHasBeenUpdated()
{
$req = $this->requestFactory();
Expand Down