Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[Testing] Add docs for new WebTestCase assertions #11271

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 1 commit into from
Apr 7, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
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
27 changes: 22 additions & 5 deletions 27 testing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -204,12 +204,29 @@ component, run:
$ composer require --dev symfony/css-selector

Now you can use CSS selectors with the crawler. To assert that the phrase
"Hello World" is on the page at least once, you can use this assertion::
"Hello World" is present in the page's main title, you can use this assertion::

$this->assertGreaterThan(
0,
$crawler->filter('html:contains("Hello World")')->count()
);
$this->assertSelectorTextContains('html h1.title', 'Hello World');

This assertion will internally call ``$crawler->filter('html h1.title')``, which allows
you to use CSS selectors to filter any HTML element in the page and check for
its existence, attributes, text, etc.

The ``assertSelectorTextContains`` method is not a native PHPUnit assertion and is
available thanks to the ``WebTestCase`` class.

.. versionadded:: 4.3

The ``WebTestCase`` assertions were introduced in Symfony 4.3

.. seealso::

Using native PHPUnit methods, the same assertion would look like this::

$this->assertGreaterThan(
0,
$crawler->filter('html h1.title:contains("Hello World")')->count()
);

The crawler can also be used to interact with the page. Click on a link by first
selecting it with the crawler using either an XPath expression or a CSS selector,
Expand Down
83 changes: 83 additions & 0 deletions 83 testing/functional_tests_assertions.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
.. index::
single: Tests; Assertions

Functional Test specific Assertions
===================================

Pierstoval marked this conversation as resolved.
Show resolved Hide resolved
.. versionadded:: 4.3

The shortcut methods for assertions using ``WebTestCase`` were introduced
in Symfony 4.3.

When doing functional tests, sometimes you need to make complex assertions in
order to check whether the ``Request``, the ``Response`` or the ``Crawler``
contain the expected information to make your test succeed.

Here is an example with plain PHPUnit::

$this->assertGreaterThan(
0,
$crawler->filter('html:contains("Hello World")')->count()
);

Now here is the example with the assertions specific to Symfony::

$this->assertSelectorTextContains('html', 'Hello World');

.. note::

These assertions only work if a request has been made with the ``Client``
in a test case extending the ``WebTestCase`` class.

Pierstoval marked this conversation as resolved.
Show resolved Hide resolved
Assertions Reference
---------------------

Response
~~~~~~~~

- ``assertResponseIsSuccessful()``
Copy link
Member

Choose a reason for hiding this comment

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

Maybe it is usefull to have the full signature here or transform them all to :method:`` links?

Copy link
Contributor

Choose a reason for hiding this comment

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

We decided to not do this because Symfony API isn’t available anymore.

Yesterday I talked to @Nyholm, @dmaicher and @xabbuh about fixing this by unsung github. MaBe you can talk to each other directly

Copy link
Member

Choose a reason for hiding this comment

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

We cannot link to a specific method when using GitHub, so that's a bit sad. For class, it will be a good solution though. We must discuss whether it's usefull to link functions, or just like to the class on github directly

Copy link
Contributor

Choose a reason for hiding this comment

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

in a PR github seems to have this method information. Otherwiese we could have a proxy wich calculates the needed line number on demand for the github url

- ``assertResponseStatusCodeSame()``
- ``assertResponseRedirects()``
- ``assertResponseHasHeader()``
- ``assertResponseNotHasHeader()``
- ``assertResponseHeaderSame()``
- ``assertResponseHeaderNotSame()``
- ``assertResponseHasCookie()``
- ``assertResponseNotHasCookie()``
- ``assertResponseCookieValueSame()``

Request
~~~~~~~

- ``assertRequestAttributeValueSame()``
- ``assertRouteSame()``

Browser
~~~~~~~

- ``assertBrowserHasCookie()``
- ``assertBrowserNotHasCookie()``
- ``assertBrowserCookieValueSame()``

Crawler
~~~~~~~

- ``assertSelectorExists()``
- ``assertSelectorNotExists()``
- ``assertSelectorTextContains()``
- ``assertSelectorTextSame()``
- ``assertSelectorTextNotContains()``
- ``assertPageTitleSame()``
- ``assertPageTitleContains()``
- ``assertInputValueSame()``
- ``assertInputValueNotSame()``

Troubleshooting
---------------

These assertions will not work with `symfony/panther`_ as they use the
``Request`` and ``Response`` objects from the ``HttpFoundation``
component, and the ``KernelBrowser`` from the ``FrameworkBundle``.
Panther only uses the ``BrowserKit`` component.

.. _`symfony/panther`: https://github.com/symfony/panther
Morty Proxy This is a proxified and sanitized view of the page, visit original site.