Skip to content

Commit

Permalink
#77 - Create Cookies trait (#79)
Browse files Browse the repository at this point in the history
* Create Cookies trait

* csf

* Apply suggestions from code review

Co-authored-by: Jakub Kermes <[email protected]>

* Suggestion from code review to ensure that exostomg request is also used for accesing the cookies

* @when cookie :name with value :value is set

* deleteCookie() changes

---------

Co-authored-by: Jakub Kermes <[email protected]>
  • Loading branch information
PiotrFedak and JakubKermes authored Jul 9, 2024
1 parent 53e2fb7 commit c4ef6d5
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 21 deletions.
13 changes: 13 additions & 0 deletions src/Features/Cookies.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

namespace Blumilk\BLT\Features;

use Behat\Behat\Context\Context;
use Blumilk\BLT\Features\Traits\Cookies as CookiesTrait;

class Cookies implements Context
{
use CookiesTrait;
}
2 changes: 2 additions & 0 deletions src/Features/Toolbox.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Blumilk\BLT\Features\Traits\Application;
use Blumilk\BLT\Features\Traits\Authentication;
use Blumilk\BLT\Features\Traits\Console;
use Blumilk\BLT\Features\Traits\Cookies;
use Blumilk\BLT\Features\Traits\Database;
use Blumilk\BLT\Features\Traits\Dispatcher;
use Blumilk\BLT\Features\Traits\Eloquent;
Expand Down Expand Up @@ -36,4 +37,5 @@ class Toolbox implements Context
use Translations;
use View;
use Notification;
use Cookies;
}
70 changes: 70 additions & 0 deletions src/Features/Traits/Cookies.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php

declare(strict_types=1);

namespace Blumilk\BLT\Features\Traits;

use Behat\Gherkin\Node\TableNode;
use Illuminate\Contracts\Cookie\QueueingFactory as CookieFactory;
use PHPUnit\Framework\Assert;

trait Cookies
{
use Application;
use HttpRequest;

/**
* @When cookie :name with value :value is set
* @When cookie named :name with value :value is created
* @When cookie :name with value :value is added
*/
public function setCookie(string $name, string $value): void
{
$cookieFactory = $this->getContainer()->make(CookieFactory::class);
$cookieFactory->queue($name, $value);
}

/**
* @Then cookie :name should have value :value
* @Then the cookie :name should contain value :value
* @Then the cookie named :name should have the value :value
*/
public function assertCookieValue(string $name, string $value): void
{
$cookieValue = $this->request->cookies->get($name);
Assert::assertEquals($value, $cookieValue, "Cookie $name does not have the expected value $value.");
}

/**
* @When cookie :name is deleted
* @When cookie :name is removed
* @When cookie :name is cleared
*/
public function deleteCookie(string $name): void
{
$cookieFactory = $this->getContainer()->make(CookieFactory::class);
$cookieFactory->queue($cookieFactory->forget($name));
}

/**
* @Then the cookie :name should be missing
* @Then the cookie named :name should not be present
*/
public function assertCookieNotExists(string $name): void
{
$cookieValue = $this->request->cookies->get($name);
Assert::assertNull($cookieValue, "Cookie $name should not exist.");
}

/**
* @Then the following cookies should be present:
* @Then the cookies should be set to:
*/
public function assertCookiesExist(TableNode $table): void
{
foreach ($table as $row) {
$cookieValue = $this->request->cookies->get($row["name"]);
Assert::assertEquals($row["value"], $cookieValue, "Cookie {$row["name"]} does not have the expected value {$row["value"]}.");
}
}
}
10 changes: 0 additions & 10 deletions src/Features/Traits/HttpRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,16 +76,6 @@ public function requestQueryParamsContains(TableNode $table): void
}
}

/**
* @Given request cookies contains:
*/
public function requestCookiesContains(TableNode $table): void
{
foreach ($table as $row) {
$this->request->cookies->set($row["key"], $row["value"]);
}
}

/**
* @Given request server params contains:
*/
Expand Down
11 changes: 0 additions & 11 deletions src/Features/Traits/HttpResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,17 +79,6 @@ public function aResponseShouldContainJsonFragment(TableNode $table): void
}
}

/**
* @Then the response should have cookie :name
*/
public function aResponseShouldHaveCookie(string $name): void
{
Assert::assertTrue($this->response->headers->has("Set-Cookie"));
$cookies = $this->response->headers->getCookies();
$cookieNames = array_map(fn($cookie) => $cookie->getName(), $cookies);
Assert::assertContains($name, $cookieNames);
}

/**
* @Then the response should have header :header
*/
Expand Down

0 comments on commit c4ef6d5

Please sign in to comment.