|
| 1 | +<?php |
| 2 | + |
| 3 | +use Behat\Behat\Event\SuiteEvent, |
| 4 | + Behat\Behat\Event\FeatureEvent; |
| 5 | +use Behat\Behat\Context\Step\Given, |
| 6 | + Behat\Behat\Context\Step\When, |
| 7 | +Behat\Behat\Context\Step\Then; |
| 8 | +use Behat\Behat\Context\ClosuredContextInterface, |
| 9 | + Behat\Behat\Context\TranslatedContextInterface, |
| 10 | + Behat\Behat\Context\BehatContext, |
| 11 | + Behat\Behat\Exception\PendingException; |
| 12 | +use Behat\Gherkin\Node\PyStringNode, |
| 13 | + Behat\Gherkin\Node\TableNode; |
| 14 | + |
| 15 | +use Github\Client; |
| 16 | + |
| 17 | +require_once 'PHPUnit/Autoload.php'; |
| 18 | +require_once 'PHPUnit/Framework/Assert/Functions.php'; |
| 19 | + |
| 20 | +class FeatureContext extends BehatContext |
| 21 | +{ |
| 22 | + private $client; |
| 23 | + private $lastResponse; |
| 24 | + |
| 25 | + public function __construct() |
| 26 | + { |
| 27 | + $this->client = new Client(); |
| 28 | + } |
| 29 | + |
| 30 | + /** |
| 31 | + * @AfterScenario |
| 32 | + */ |
| 33 | + public function clearLastResponse() |
| 34 | + { |
| 35 | + $this->lastResponse = null; |
| 36 | + } |
| 37 | + |
| 38 | + /** |
| 39 | + * @When /^(?:|I )send a request for rate limit information$/ |
| 40 | + */ |
| 41 | + public function iSendRequestForRateLimitInformation() |
| 42 | + { |
| 43 | + $this->lastResponse = $this->client->getRateLimit(); |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * @Then /^(?:|I )should not reach my rate limit$/ |
| 48 | + */ |
| 49 | + public function iShouldNotReachMyRateLimit() |
| 50 | + { |
| 51 | + $remainingCalls = $this->client->getHttpClient()->remainingCalls; |
| 52 | + |
| 53 | + assertNotNull($remainingCalls); |
| 54 | + assertTrue(5000 > $remainingCalls); |
| 55 | + assertTrue(0 < $remainingCalls); |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * @When /^(?:|I )send a markdown data:$/ |
| 60 | + */ |
| 61 | + public function iSendMarkdownData(PyStringNode $jsonString) |
| 62 | + { |
| 63 | + $data = json_decode($jsonString->getRaw(), true); |
| 64 | + |
| 65 | + $this->lastResponse = $this->client->getMarkdownApi()->render( |
| 66 | + $data['text'], |
| 67 | + isset($data['mode']) ? $data['mode'] : null, |
| 68 | + isset($data['context']) ? $data['context'] : null |
| 69 | + ); |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * @Then /^(?:the )?response should equal to html:$/ |
| 74 | + */ |
| 75 | + public function theResponseShouldEqualToHtml(PyStringNode $htmlString) |
| 76 | + { |
| 77 | + $expected = $htmlString->getRaw(); |
| 78 | + $actual = $this->lastResponse; |
| 79 | + |
| 80 | + assertEquals($expected, $actual, "Failed asserting that equals to \n".print_r($actual, true)); |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * @Then /^(?:the )?response should equal to json:$/ |
| 85 | + */ |
| 86 | + public function theResponseShouldEqualToJson(PyStringNode $jsonString) |
| 87 | + { |
| 88 | + $expected = json_decode($jsonString->getRaw(), true); |
| 89 | + $actual = $this->lastResponse; |
| 90 | + |
| 91 | + if (null === $expected) { |
| 92 | + throw new \RuntimeException( |
| 93 | + "Can not convert etalon to json:\n".$jsonString->getRaw() |
| 94 | + ); |
| 95 | + } |
| 96 | + |
| 97 | + assertEquals($expected, $actual, "Failed asserting that equals to \n".print_r($actual, true)); |
| 98 | + } |
| 99 | +} |
0 commit comments