Skip to content
This repository has been archived by the owner on Aug 20, 2018. It is now read-only.

Commit

Permalink
Add fake $_SERVER['HTTP_HOST'] and $_SERVER['SERVER_NAME']
Browse files Browse the repository at this point in the history
To make legacy code testing easier.
  • Loading branch information
Valicek1 authored and mrtnzlml committed Aug 5, 2016
1 parent 5134803 commit e2fd316
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 11 deletions.
1 change: 1 addition & 0 deletions src/Bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public static function setup($tempDir, $callback = NULL)

$_SERVER['HTTP_USER_AGENT'] = 'Awesome Browser';
$_SERVER['REMOTE_ADDR'] = '11.22.33.44';
$_SERVER['HTTP_HOST'] = $_SERVER['SERVER_NAME'] = 'test.bench';
}

}
28 changes: 17 additions & 11 deletions src/Scaffold/TestsGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,8 @@ public function generateTests($outputFolder)
break;
case 'action':
if ($extra instanceof \Nette\Application\Responses\RedirectResponse) {
$path = preg_replace('~^https?://([/a-z0-9]+).*~i', '$1', $extra->getUrl());
$generatedMethod->addBody('$this->checkRedirect(?, ?);', [$destination, $path]);
$url = new \Nette\Http\Url($extra->getUrl());
$generatedMethod->addBody('$this->checkRedirect(?, ?);', [$destination, $url->getPath()]);
} elseif ($extra instanceof \Nette\Application\Responses\JsonResponse) {
$generatedMethod->addBody('$this->checkJson(?);', [$destination]);
} else {
Expand Down Expand Up @@ -127,24 +127,30 @@ public function generateTests($outputFolder)
$controls = '';
/** @var \Nette\Application\UI\Form $form */
foreach ($form->getControls() as $control) {
if ($control->getName() === '_token_') {
if ($control->getName() === '_token_' || $control instanceof \Nette\Forms\Controls\SubmitButton) {
continue;
}
$controls .= "\t'" . $control->getName() . "' => '###', //FIXME: replace with value\n";
$value = "'###', //FIXME: replace with value";
if ($control instanceof \Nette\Forms\Controls\Checkbox) {
$value = 'FALSE';
}
$controls .= "\t'" . $control->getName() . "' => $value\n";
}
try {
$form->onSuccess($form, $form->getValues());
$testClass->addMethod('test' . ucfirst($testMethod))->addBody(
"\$this->checkForm(?, ?, [\n" .
$controls .
"], ?);", [$destination . ':', $action, FALSE]);
"\$this->checkForm(?, ?, [\n" . $controls . '], ?);',
[$destination . ':', $action, FALSE]
);
} catch (\Nette\Application\AbortException $exc) {
$extra = $this->getResponse($service);
$path = $extra ? preg_replace('~^https?://([/a-z0-9]+).*~i', '$1', $extra->getUrl()) : '/';
$path = $extra ? (new \Nette\Http\Url($extra->getUrl()))->getPath() : '/';
$testClass->addMethod('test' . ucfirst($testMethod))->addBody(
"\$this->checkForm(?, ?, [\n" .
$controls .
"], ?);", [$destination . ':', $action, $path]);
"\$this->checkForm(?, ?, [\n" . $controls . '], ?);',
[$destination . ':', $action, $path]
);
} catch (\Exception $exc) {
//This sucks but we have to move on - failure is not an option
}
}

Expand Down
2 changes: 2 additions & 0 deletions tests/Runner/Bootstrap.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ class Bootstrap extends \Tester\TestCase
{
Assert::same('Awesome Browser', $_SERVER['HTTP_USER_AGENT']);
Assert::same('11.22.33.44', $_SERVER['REMOTE_ADDR']);
Assert::same('test.bench', $_SERVER['HTTP_HOST']);
Assert::same('test.bench', $_SERVER['SERVER_NAME']);
}

}
Expand Down

0 comments on commit e2fd316

Please sign in to comment.