8000 Add browserkit component documentation by javiereguiluz · Pull Request #6072 · symfony/symfony-docs · GitHub
[go: up one dir, main page]

Skip to content

Add browserkit component documentation #6072

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 21 commits into from
Feb 9, 2016
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Adding documentation for cookies
  • Loading branch information
robert Parker authored and javiereguiluz committed Dec 23, 2015
commit ea3fd71234a5fe3aae1f380ccb4c4ee7e47a091a
92 changes: 90 additions & 2 deletions components/browser_kit/introduction.rst
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,96 @@ You can get the form object by using the crawler to select the button and runnin
Cookies
-------

Retreiving Cookies
~~~~~~~~~~~~~~~~~~

The Crawler has a cookieJar which is a container for storing and recieving cookies.

.. code-block:: php

use ACME\Client;

// Make a request
$client = new Client();
$crawler = $client->request('GET', 'http://symfony.com');

// Get the cookie Jar
$cookieJar = $crawler->getCookieJar();

// Get a cookie by name
$flavor = $cookieJar->get('flavor');

// Get cookie data
$name = $flavor->getName();
$value = $flavor->getValue();
$raw = $flavor->getRawValue();
$secure = $flavor->isSecure();
$isHttpOnly = $flavor->isHttpOnly();
$isExpired = $flavor->isExpired();
$expires = $flavor->getExpiresTime();
$path = $flavor->getPath();
$domain = $flavor->getDomain();

Looping Through Cookies
~~~~~~~~~~~~~~~~~~~~~~~

.. code-block:: php
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No text, devs will probably like this 😉 . I'm not sure about it though


use ACME\Client;

// Make a request
$client = new Client();
$crawler = $client->request('GET', 'http://symfony.com');

// Get the cookie Jar
$cookieJar = $crawler->getCookieJar();

// Get array with all cookies
$cookies = $cookieJar->all();
foreach($cookies as $cookie)
{
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Symfony CS: put { on the same line as foreach (...)

// ...
}

// Get all values
$values = $cookieJar->allValues('http://symfony.com');
foreach($values as $value)
{
// ...
}

// Get all raw values
$rawValues = $cookieJar->allRawValues('http://symfony.com');
foreach($rawValues as $rawValue)
{
// ...
}

.. note::
These cookie jar methods only return cookies that have not expired.

Setting Cookies
~~~~~~~~~~~~~~~

You can define create cookies and add them to a cookie jar that can be injected it into the client constructor.

.. code-block:: php

use ACME\Client;

// create cookies and add to cookie jar
$expires = new \DateTime();
$expires->add(new \DateInterval('P1D'));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not strtotime('+1 day'), which will be much more readable imo? (I mean replacing the complete Date* logic here with this single function call on line 191)

$cookie = new Cookie(
'flavor',
'chocolate chip',
$now->getTimestamp()
);

// create a client and set the cookies
$client = new Client(array(), array(), $cookieJar);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

undefined variable $cookieJar

// ...

History
-------

Expand Down Expand Up @@ -138,8 +228,6 @@ You can restart the clients history with the restart method. This will also clea
// restart history
$client->restart();

Insulated Request
-----------------

.. _Packagist: https://packagist.org/packages/symfony/browser-kit
.. _Goutte: https://github.com/fabpot/Goutte
0