/api/vendor/peej/tonic/features/bootstrap/FeatureContext.php
PHP | 519 lines | 330 code | 54 blank | 135 comment | 52 complexity | 8b7f42a23b1052cc464a1f85a5883dab MD5 | raw file
- <?php
- use Behat\Behat\Context\ClosuredContextInterface,
- Behat\Behat\Context\TranslatedContextInterface,
- Behat\Behat\Context\BehatContext,
- Behat\Behat\Exception\PendingException;
- use Behat\Gherkin\Node\PyStringNode,
- Behat\Gherkin\Node\TableNode;
- use Tonic\Application,
- Tonic\Request,
- Tonic\Resource,
- Tonic\Response;
- /**
- * Features context.
- */
- class FeatureContext extends BehatContext
- {
- private $app, $request, $resource, $response, $exception, $error;
- private $createMethod = array();
- private $data;
- private $options = array();
- /**
- * @BeforeFeature
- */
- public static function setupFeature()
- {
- unset($_SERVER);
- unset($_GET);
- unset($_POST);
- }
- /**
- * @Given /^the request URI of "([^"]*)"$/
- */
- public function theRequestUriOf($uri)
- {
- $parsedUri = parse_url($uri);
- $_SERVER['REDIRECT_URL'] = $parsedUri['path'];
- if (isset($parsedUri['query'])) {
- $query = explode('&', $parsedUri['query']);
- foreach ($query as $item) {
- list($name, $value) = explode('=', $item);
- $_GET[$name] = $value;
- }
- }
- $_SERVER['SCRIPT_NAME'] = '';
- }
- /**
- * @Given /^the request method of "([^"]*)"$/
- */
- public function theRequestMethodOf($method)
- {
- $_SERVER['REQUEST_METHOD'] = $method;
- }
- /**
- * @When /^I create an application object$/
- */
- public function iCreateAnApplicationObject()
- {
- $this->app = new Application($this->options);
- }
- /**
- * @When /^I create a request object$/
- */
- public function iCreateARequestObject()
- {
- if ($this->data) {
- $this->options['data'] = $this->data;
- $this->options['contentType'] = $_SERVER['CONTENT_TYPE'];
- }
- $this->request = new Request($this->options);
- }
- /**
- * @Then /^I should see a request URI of "([^"]*)"$/
- */
- public function iShouldSeeARequestUriOf($uri)
- {
- if ($this->request->uri != $uri) throw new Exception;
- }
- /**
- * @Then /^I should see a request method of "([^"]*)"$/
- */
- public function iShouldSeeARequestMethodOf($method)
- {
- if ($this->request->method != $method) throw new Exception;
- }
- /**
- * @Given /^an? "([^"]*)" header of ['"]([^']*)['"]$/
- */
- public function aHeaderOf($header, $value)
- {
- $headerMapping = array(
- 'accept' => 'HTTP_ACCEPT',
- 'accept language' => 'HTTP_ACCEPT_LANGUAGE',
- 'if-none-match' => 'HTTP_IF_NONE_MATCH',
- 'if-match' => 'HTTP_IF_MATCH',
- 'content-type' => 'CONTENT_TYPE',
- 'auth user' => 'PHP_AUTH_USER',
- 'auth password' => 'PHP_AUTH_PW',
- 'x-authentication' => 'HTTP_X_AUTHENTICATION'
- );
- $_SERVER[$headerMapping[$header]] = $value;
- }
- /**
- * @Given /^I should see an? "([^"]*)" string of "([^"]*)"$/
- */
- public function iShouldSeeAStringOf($header, $string)
- {
- $propertyMapping = array(
- 'accept' => 'accept',
- 'accept language' => 'acceptLanguage',
- 'if-none-match' => 'ifNoneMatch',
- 'if-match' => 'ifMatch',
- 'content-type' => 'contentType'
- );
- if (is_array($this->request->$propertyMapping[$header])) {
- $value = join(',', $this->request->$propertyMapping[$header]);
- } else {
- $value = $this->request->$propertyMapping[$header];
- }
- if ($value != $string)
- throw new Exception($value);
- }
- /**
- * @Given /^body data of \'([^\']*)\'$/
- */
- public function bodyDataOf($data)
- {
- $this->data = $data;
- }
- /**
- * @Given /^I should see body data of "([^"]*)"$/
- */
- public function iShouldSeeBodyDataOf($data)
- {
- if ($this->request->data != $data)
- throw new Exception();
- }
- /**
- * @Given /^a resource definition "([^"]*)" with URI "([^"]*)" and priority of (\d+)$/
- */
- public function aResourceDefinitionWithUriAndPriorityOf($className, $uri, $priority)
- {
- $this->aResourceDefinition($className, $uri, $priority);
- }
- /**
- * @Given /^a resource definition "([^"]*)" with URI "([^"]*)" and namespace of "([^"]*)"$/
- */
- public function aResourceDefinitionWithUriAndNamespaceOf($className, $uri, $namespace)
- {
- $this->aResourceDefinition($className, $uri, 1, $namespace);
- }
- /**
- * @Given /^a resource definition "([^"]*)" with URI "([^"]*)" and namespace annotation of "([^"]*)"$/
- */
- public function aResourceDefinitionWithUriAndNamespaceAnnotationOf($className, $uri, $namespace)
- {
- $this->aResourceDefinition($className, $uri, 1, NULL, $namespace);
- }
- /**
- * @Given /^a resource definition "([^"]*)" with URI "([^"]*)" and windows style line endings$/
- */
- public function aResourceDefinitionWithUriAndWindowsStyleLineEndings($className, $uri)
- {
- $this->aResourceDefinition($className, $uri, 1, NULL, NULL, "\r\n");
- }
- private function aResourceDefinition($className, $uri, $priority = 1, $namespace = NULL, $annotationNamespace = NULL, $lineEnding = "\n")
- {
- $classDefinition = '';
- if ($namespace) $classDefinition .= 'namespace '.$namespace.';'.$lineEnding;
- $classDefinition .= '/**'.$lineEnding.
- ' * @uri '.$uri.$lineEnding.
- ' * @priority '.$priority.$lineEnding;
- if ($annotationNamespace) $classDefinition .= ' * @namespace '.$annotationNamespace.$lineEnding;
- $classDefinition .= ' */'.$lineEnding.
- 'class '.$className.' extends \Tonic\Resource {'.$lineEnding;
- foreach ($this->createMethod as $methodData) {
- $classDefinition .= ' /**'.$lineEnding;
- $classDefinition .= ' * @method '.(isset($methodData['method']) ? $methodData['method'] : 'GET').$lineEnding;
- if (isset($methodData['lang'])) $classDefinition .= ' * @lang '.$methodData['lang'].$lineEnding;
- if (isset($methodData['accepts'])) $classDefinition .= ' * @accepts '.$methodData['accepts'].$lineEnding;
- if (isset($methodData['provides'])) $classDefinition .= ' * @provides '.$methodData['provides'].$lineEnding;
- $classDefinition .= $lineEnding.' */'.$lineEnding.
- ' function '.$methodData['name'].'() {'.$lineEnding.
- ' return "'.$methodData['name'].'";'.$lineEnding.
- ' }'.$lineEnding;
- }
- $classDefinition .= '}'.$lineEnding;
- eval($classDefinition);
- }
- /**
- * @Given /^load the resource$/
- */
- public function loadTheResource()
- {
- try {
- $this->resource = $this->app->getResource($this->request);
- } catch (Tonic\Exception $e) {
- $this->exception = $e;
- }
- }
- /**
- * @Then /^the loaded resource should have a class of "([^"]*)"$/
- */
- public function theLoadedResourceShouldHaveAClassOf($className)
- {
- $loadedClassName = get_class($this->resource);
- if ($loadedClassName != $className) throw new Exception($loadedClassName.' != '.$className);
- }
- /**
- * @Given /^the loaded resource should have a param "([^"]*)" with the value "([^"]*)"$/
- */
- public function theLoadedResourceShouldHaveAParamWithTheValue($name, $value)
- {
- if (!isset($this->resource->params[$name])) throw new Exception('Param '.$name.' not found');
- if ($this->resource->params[$name] != $value) throw new Exception('Param '.$name.' does not equal '.$value);
- }
- /**
- * @Given /^execute the resource$/
- */
- public function executeTheResource()
- {
- set_error_handler(function ($level, $message, $file, $line) {
-