Skip to content

Commit

Permalink
docs: move Testing CLI to a new page
Browse files Browse the repository at this point in the history
  • Loading branch information
kenjis committed Oct 13, 2023
1 parent 657fb08 commit 89ea637
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 83 deletions.
86 changes: 86 additions & 0 deletions user_guide_src/source/testing/cli.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
####################
Testing CLI Commands
####################

.. _testing-cli-output:

Testing CLI Output
==================

StreamFilterTrait
-----------------

.. versionadded:: 4.3.0

**StreamFilterTrait** provides an alternate to these helper methods.

You may need to test things that are difficult to test. Sometimes, capturing a stream, like PHP's own STDOUT, or STDERR,
might be helpful. The ``StreamFilterTrait`` helps you capture the output from the stream of your choice.

**Overview of methods**

- ``StreamFilterTrait::getStreamFilterBuffer()`` Get the captured data from the buffer.
- ``StreamFilterTrait::resetStreamFilterBuffer()`` Reset captured data.

An example demonstrating this inside one of your test cases:

.. literalinclude:: overview/018.php

The ``StreamFilterTrait`` has a configurator that is called automatically.
See :ref:`Testing Traits <testing-overview-traits>`.

If you override the ``setUp()`` or ``tearDown()`` methods in your test, then you must call the ``parent::setUp()`` and
``parent::tearDown()`` methods respectively to configure the ``StreamFilterTrait``.

CITestStreamFilter
------------------

**CITestStreamFilter** for manual/single use.

If you need to capture streams in only one test, then instead of using the StreamFilterTrait trait, you can manually
add a filter to streams.

**Overview of methods**

- ``CITestStreamFilter::registration()`` Filter registration.
- ``CITestStreamFilter::addOutputFilter()`` Adding a filter to the output stream.
- ``CITestStreamFilter::addErrorFilter()`` Adding a filter to the error stream.
- ``CITestStreamFilter::removeOutputFilter()`` Removing a filter from the output stream.
- ``CITestStreamFilter::removeErrorFilter()`` Removing a filter from the error stream.

.. literalinclude:: overview/020.php

.. _testing-cli-input:

Testing CLI Input
=================

PhpStreamWrapper
----------------

.. versionadded:: 4.3.0

**PhpStreamWrapper** provides a way to write tests for methods that require user input,
such as ``CLI::prompt()``, ``CLI::wait()``, and ``CLI::input()``.

.. note:: The PhpStreamWrapper is a stream wrapper class.
If you don't know PHP's stream wrapper,
see `The streamWrapper class <https://www.php.net/manual/en/class.streamwrapper.php>`_
in the PHP maual.

**Overview of methods**

- ``PhpStreamWrapper::register()`` Register the ``PhpStreamWrapper`` to the ``php`` protocol.
- ``PhpStreamWrapper::restore()`` Restore the php protocol wrapper back to the PHP built-in wrapper.
- ``PhpStreamWrapper::setContent()`` Set the input data.

.. important:: The PhpStreamWrapper is intended for only testing ``php://stdin``.
But when you register it, it handles all the `php protocol <https://www.php.net/manual/en/wrappers.php.php>`_ streams,
such as ``php://stdout``, ``php://stderr``, ``php://memory``.
So it is strongly recommended that ``PhpStreamWrapper`` be registered/unregistered
only when needed. Otherwise, it will interfere with other built-in php streams
while registered.

An example demonstrating this inside one of your test cases:

.. literalinclude:: overview/019.php
1 change: 1 addition & 0 deletions user_guide_src/source/testing/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ The following sections should get you quickly testing your applications.
Controller Testing <controllers>
HTTP Testing <feature>
response
cli
benchmark
debugging
Mocking <mocking>
83 changes: 0 additions & 83 deletions user_guide_src/source/testing/overview.rst
Original file line number Diff line number Diff line change
Expand Up @@ -262,86 +262,3 @@ component name:
.. literalinclude:: overview/017.php

.. note:: All component Factories are reset by default between each test. Modify your test case's ``$setUpMethods`` if you need instances to persist.

.. _testing-cli-output:

Testing CLI Output
==================

StreamFilterTrait
-----------------

.. versionadded:: 4.3.0

**StreamFilterTrait** provides an alternate to these helper methods.

You may need to test things that are difficult to test. Sometimes, capturing a stream, like PHP's own STDOUT, or STDERR,
might be helpful. The ``StreamFilterTrait`` helps you capture the output from the stream of your choice.

**Overview of methods**

- ``StreamFilterTrait::getStreamFilterBuffer()`` Get the captured data from the buffer.
- ``StreamFilterTrait::resetStreamFilterBuffer()`` Reset captured data.

An example demonstrating this inside one of your test cases:

.. literalinclude:: overview/018.php

The ``StreamFilterTrait`` has a configurator that is called automatically.
See :ref:`Testing Traits <testing-overview-traits>`.

If you override the ``setUp()`` or ``tearDown()`` methods in your test, then you must call the ``parent::setUp()`` and
``parent::tearDown()`` methods respectively to configure the ``StreamFilterTrait``.

CITestStreamFilter
------------------

**CITestStreamFilter** for manual/single use.

If you need to capture streams in only one test, then instead of using the StreamFilterTrait trait, you can manually
add a filter to streams.

**Overview of methods**

- ``CITestStreamFilter::registration()`` Filter registration.
- ``CITestStreamFilter::addOutputFilter()`` Adding a filter to the output stream.
- ``CITestStreamFilter::addErrorFilter()`` Adding a filter to the error stream.
- ``CITestStreamFilter::removeOutputFilter()`` Removing a filter from the output stream.
- ``CITestStreamFilter::removeErrorFilter()`` Removing a filter from the error stream.

.. literalinclude:: overview/020.php

.. _testing-cli-input:

Testing CLI Input
=================

PhpStreamWrapper
----------------

.. versionadded:: 4.3.0

**PhpStreamWrapper** provides a way to write tests for methods that require user input,
such as ``CLI::prompt()``, ``CLI::wait()``, and ``CLI::input()``.

.. note:: The PhpStreamWrapper is a stream wrapper class.
If you don't know PHP's stream wrapper,
see `The streamWrapper class <https://www.php.net/manual/en/class.streamwrapper.php>`_
in the PHP maual.

**Overview of methods**

- ``PhpStreamWrapper::register()`` Register the ``PhpStreamWrapper`` to the ``php`` protocol.
- ``PhpStreamWrapper::restore()`` Restore the php protocol wrapper back to the PHP built-in wrapper.
- ``PhpStreamWrapper::setContent()`` Set the input data.

.. important:: The PhpStreamWrapper is intended for only testing ``php://stdin``.
But when you register it, it handles all the `php protocol <https://www.php.net/manual/en/wrappers.php.php>`_ streams,
such as ``php://stdout``, ``php://stderr``, ``php://memory``.
So it is strongly recommended that ``PhpStreamWrapper`` be registered/unregistered
only when needed. Otherwise, it will interfere with other built-in php streams
while registered.

An example demonstrating this inside one of your test cases:

.. literalinclude:: overview/019.php

0 comments on commit 89ea637

Please sign in to comment.