|
| 1 | +.. index:: |
| 2 | + single: Tests; Assertions |
| 3 | + |
| 4 | +Functional Test specific Assertions |
| 5 | +=================================== |
| 6 | + |
| 7 | +.. versionadded:: 4.3 |
| 8 | + |
| 9 | + The shortcut methods for assertions using ``WebTestCase`` were introduced |
| 10 | + in Symfony 4.3. |
| 11 | + |
| 12 | +When doing functional tests, sometimes you need to make complex assertions in |
| 13 | +order to check whether the ``Request``, the ``Response`` or the ``Crawler`` |
| 14 | +contain the expected information to make your test succeed. |
| 15 | + |
| 16 | +Here is an example with plain PHPUnit:: |
| 17 | + |
| 18 | + $this->assertGreaterThan( |
| 19 | + 0, |
| 20 | + $crawler->filter('html:contains("Hello World")')->count() |
| 21 | + ); |
| 22 | + |
| 23 | +Now here is the example with the assertions specific to Symfony:: |
| 24 | + |
| 25 | + $this->assertSelectorTextContains('html', 'Hello World'); |
| 26 | + |
| 27 | +.. note:: |
| 28 | + |
| 29 | + These assertions only work if a request has been made with the ``Client`` |
| 30 | + in a test case extending the ``WebTestCase`` class. |
| 31 | + |
| 32 | +Assertions Reference |
| 33 | +--------------------- |
| 34 | + |
| 35 | +Response |
| 36 | +~~~~~~~~ |
| 37 | + |
| 38 | +- ``assertResponseIsSuccessful()`` |
| 39 | +- ``assertResponseStatusCodeSame()`` |
| 40 | +- ``assertResponseRedirects()`` |
| 41 | +- ``assertResponseHasHeader()`` |
| 42 | +- ``assertResponseNotHasHeader()`` |
| 43 | +- ``assertResponseHeaderSame()`` |
| 44 | +- ``assertResponseHeaderNotSame()`` |
| 45 | +- ``assertResponseHasCookie()`` |
| 46 | +- ``assertResponseNotHasCookie()`` |
| 47 | +- ``assertResponseCookieValueSame()`` |
| 48 | + |
| 49 | +Request |
| 50 | +~~~~~~~ |
| 51 | + |
| 52 | +- ``assertRequestAttributeValueSame()`` |
| 53 | +- ``assertRouteSame()`` |
| 54 | + |
| 55 | +Browser |
| 56 | +~~~~~~~ |
| 57 | + |
| 58 | +- ``assertBrowserHasCookie()`` |
| 59 | +- ``assertBrowserNotHasCookie()`` |
| 60 | +- ``assertBrowserCookieValueSame()`` |
| 61 | + |
| 62 | +Crawler |
| 63 | +~~~~~~~ |
| 64 | + |
| 65 | +- ``assertSelectorExists()`` |
| 66 | +- ``assertSelectorNotExists()`` |
| 67 | +- ``assertSelectorTextContains()`` |
| 68 | +- ``assertSelectorTextSame()`` |
| 69 | +- ``assertSelectorTextNotContains()`` |
| 70 | +- ``assertPageTitleSame()`` |
| 71 | +- ``assertPageTitleContains()`` |
| 72 | +- ``assertInputValueSame()`` |
| 73 | +- ``assertInputValueNotSame()`` |
| 74 | + |
| 75 | +Troubleshooting |
| 76 | +--------------- |
| 77 | + |
| 78 | +These assertions will not work with `symfony/panther`_ as they use the |
| 79 | +``Request`` and ``Response`` objects from the ``HttpFoundation`` |
| 80 | +component, and the ``KernelBrowser`` from the ``FrameworkBundle``. |
| 81 | +Panther only uses the ``BrowserKit`` component. |
| 82 | + |
| 83 | +.. _`symfony/panther`: https://github.com/symfony/panther |
0 commit comments