Skip to content

docs: improve testing/feature.rst #8494

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 3 commits into from
Feb 2, 2024
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
13 changes: 10 additions & 3 deletions user_guide_src/source/testing/feature.rst
Original file line number Diff line number Diff line change
Expand Up @@ -29,25 +29,27 @@ Requesting a Page
Essentially, feature tests simply allows you to call an endpoint on your application and get the results back.
To do this, you use the ``call()`` method.

1. The first parameter is the HTTP method to use (most frequently either GET or POST).
1. The first parameter is the HTTP method to use (most frequently either ``GET`` or ``POST``).
2. The second parameter is the URI path on your site to test.
3. The third parameter ``$params`` accepts an array that is used to populate the
superglobal variables for the HTTP verb you are using. So, a method of **GET**
would have the **$_GET** variable populated, while a **POST** request would
have the **$_POST** array populated. The ``$params`` is also used in
would have the ``$_GET`` variable populated, while a **POST** request would
have the ``$_POST`` array populated. The ``$params`` is also used in
:ref:`feature-formatting-the-request`.

.. note:: The ``$params`` array does not make sense for every HTTP verb, but is
included for consistency.

.. literalinclude:: feature/002.php
:lines: 2-

Shorthand Methods
-----------------

Shorthand methods for each of the HTTP verbs exist to ease typing and make things clearer:

.. literalinclude:: feature/003.php
:lines: 2-

Setting Different Routes
------------------------
Expand All @@ -56,6 +58,7 @@ You can use a custom collection of routes by passing an array of "routes" into t
override any existing routes in the system:

.. literalinclude:: feature/004.php
:lines: 2-

Each of the "routes" is a 3 element array containing the HTTP verb (or "add" for all),
the URI to match, and the routing destination.
Expand All @@ -68,6 +71,7 @@ of key/value pairs that should exist within the ``$_SESSION`` variable when this
that the current values of ``$_SESSION`` should be used. This is handy for testing authentication and more.

.. literalinclude:: feature/005.php
:lines: 2-

Setting Headers
---------------
Expand All @@ -76,6 +80,7 @@ You can set header values with the ``withHeaders()`` method. This takes an array
passed as a header into the call:

.. literalinclude:: feature/006.php
:lines: 2-

Bypassing Events
----------------
Expand All @@ -84,6 +89,7 @@ Events are handy to use in your application, but can be problematic during testi
to send out emails. You can tell the system to skip any event handling with the ``skipEvents()`` method:

.. literalinclude:: feature/007.php
:lines: 2-

.. _feature-formatting-the-request:

Expand All @@ -100,6 +106,7 @@ body of the request in the given format.
This will also set the `Content-Type` header for your request accordingly.

.. literalinclude:: feature/008.php
:lines: 2-

.. _feature-setting-the-body:

Expand Down
5 changes: 3 additions & 2 deletions user_guide_src/source/testing/feature/001.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
<?php

namespace App;
namespace Tests\Feature;

use CodeIgniter\Test\CIUnitTestCase;
use CodeIgniter\Test\DatabaseTestTrait;
use CodeIgniter\Test\FeatureTestTrait;

class TestFoo extends CIUnitTestCase
class FooTest extends CIUnitTestCase
{
use DatabaseTestTrait;
use FeatureTestTrait;
Expand Down