Skip to content

Commit

Permalink
allow to configure script path for fake url
Browse files Browse the repository at this point in the history
[Fixes #67]
  • Loading branch information
fprochazka committed May 13, 2017
1 parent 91748f0 commit b22980d
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 2 deletions.
3 changes: 2 additions & 1 deletion src/Kdyby/Console/DI/ConsoleExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class ConsoleExtension extends Nette\DI\CompilerExtension
'version' => 'unknown',
'commands' => [],
'url' => NULL,
'urlScriptPath' => NULL,
'disabled' => TRUE,
'application' => TRUE,
'fakeHttp' => TRUE,
Expand Down Expand Up @@ -219,7 +220,7 @@ protected function beforeCompileFakeHttp(array $config)
Nette\Utils\Validators::assert($config['url'], 'url', 'console.url');
$builder->getDefinition($builder->getByType('Nette\Http\RequestFactory') ?: 'nette.httpRequestFactory')
->setFactory('Kdyby\Console\HttpRequestFactory')
->addSetup('setFakeRequestUrl', [$config['url']]);
->addSetup('setFakeRequestUrl', [$config['url'], $config['urlScriptPath']]);
}
}

Expand Down
6 changes: 5 additions & 1 deletion src/Kdyby/Console/HttpRequestFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,14 @@ class HttpRequestFactory extends Nette\Http\RequestFactory

/**
* @param string|Nette\Http\UrlScript $url
* @param string|null $scriptPath
*/
public function setFakeRequestUrl($url)
public function setFakeRequestUrl($url, $scriptPath = null)
{
$this->fakeUrl = $url ? new Nette\Http\UrlScript($url) : NULL;
if ($scriptPath !== null) {
$this->fakeUrl->setScriptPath($scriptPath);
}
}


Expand Down
55 changes: 55 additions & 0 deletions tests/KdybyTests/Console/HttpRequestFactory.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

/**
* Test: Kdyby\Console\HttpRequestFactory.
*
* @testCase Kdyby\Console\HttpRequestFactoryTest
* @author Filip Procházka <filip@prochazka.su>
* @package Kdyby\Console
*/

namespace KdybyTests\Console;

use Kdyby;
use Nette\Application\LinkGenerator;
use Nette\Application\PresenterFactory;
use Nette\Application\Routers\Route;
use Tester;
use Tester\Assert;

require_once __DIR__ . '/../bootstrap.php';



/**
* @author Filip Procházka <filip@prochazka.su>
*/
class HttpRequestFactoryTest extends Tester\TestCase
{

public function testScriptPath()
{
$requestFactory = new Kdyby\Console\HttpRequestFactory();
$requestFactory->setFakeRequestUrl(
'http://domain.tld/path/',
'/path/'
);

$httpRequest = $requestFactory->createHttpRequest();
Assert::same('cli', $httpRequest->getMethod());

$presenterFactory = new PresenterFactory();
$presenterFactory->setMapping(['Kdyby' => 'KdybyModule\*\*Presenter']);
$linkGenerator = new LinkGenerator(
new Route('ABCDEF', ['presenter' => 'Kdyby:Cli', 'action' => 'default']),
$httpRequest->getUrl(),
$presenterFactory
);

$url = $linkGenerator->link('Kdyby:Cli:default', ['code' => 'brown-alert']);
Assert::same('http://domain.tld/path/ABCDEF?code=brown-alert', $url);
}

}

\run(new HttpRequestFactoryTest());

0 comments on commit b22980d

Please sign in to comment.