summaryrefslogtreecommitdiffstats
path: root/build/integration/features/bootstrap/FeatureContext.php
blob: e1185debbc5675315757885a7d8ad8cbe2d2bc31 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
<?php

use Behat\Behat\Context\BehatContext;
use GuzzleHttp\Client;
use GuzzleHttp\Message\ResponseInterface;

//
// Require 3rd-party libraries here:
//
//   require_once 'PHPUnit/Autoload.php';
//   require_once 'PHPUnit/Framework/Assert/Functions.php';
//

/**
 * Features context.
 */
class FeatureContext extends BehatContext {

    /** @var string */
    private $baseUrl = '';

    /** @var ResponseInterface */
    private $response = null;

	/** @var string */
	private $currentUser = '';

	/** @var int */
	private $apiVersion = 1;

    /**
     * Initializes context.
     * Every scenario gets it's own context object.
     *
     * @param array $parameters context parameters (set them up through behat.yml)
     */
    public function __construct(array $parameters) {

        // Initialize your context here
		$this->baseUrl = $parameters['baseUrl'];
		$this->adminUser = $parameters['admin'];
    }

    /**
     * @When /^sending "([^"]*)" to "([^"]*)"$/
     */
    public function sendingTo($verb, $url) {
        $fullUrl = $this->baseUrl . "v{$this->apiVersion}.php" . $url;
        $client = new Client();
        // TODO: get admin user from config
        $options = [];
		if ($this->currentUser === 'admin') {
			$options['auth'] = $this->adminUser;
		}

		try {
			$this->response = $client->send($client->createRequest($verb, $fullUrl, $options));
		} catch (\GuzzleHttp\Exception\ClientException $ex) {
			$this->response = $ex->getResponse();
		}
    }

    /**
     * @Then /^the status code should be "([^"]*)"$/
     */
    public function theStatusCodeShouldBe($statusCode) {
        PHPUnit_Framework_Assert::assertEquals($statusCode, $this->response->getStatusCode());
    }

    /**
     * @Given /^As an "([^"]*)"$/
     */
    public function asAn($user) {
		$this->currentUser = $user;
    }

	/**
	 * @Given /^using api version "([^"]*)"$/
	 */
	public function usingApiVersion($version) {
		$this->apiVersion = $version;
	}

	/**
	 * @Given /^user "([^"]*)" exists$/
	 */
	public function userExists($user) {
		throw new \Behat\Behat\Exception\PendingException();
	}

}