Skip to content

Commit 3e2f780

Browse files
committed
docs: add user guide
1 parent 33d61c2 commit 3e2f780

File tree

4 files changed

+56
-4
lines changed

4 files changed

+56
-4
lines changed

user_guide_src/source/changelogs/v4.3.0.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ Others
3333
Enhancements
3434
************
3535

36-
- Added the ``StreamFilterTrait`` to make it easier to work with capturing data from STDOUT and STDERR streams. See :ref:`testing-overview-stream-filters`.
36+
- Added the ``StreamFilterTrait`` to make it easier to work with capturing data from STDOUT and STDERR streams. See :ref:`testing-cli-output`.
37+
- Added the ``PhpStreamWrapper`` to make it easier to work with setting data to ``php://stdin``. See :ref:`testing-cli-input`.
3738
- Added before and after events to ``BaseModel::insertBatch()`` and ``BaseModel::updateBatch()`` methods. See :ref:`model-events-callbacks`.
3839
- Added ``Model::allowEmptyInserts()`` method to insert empty data. See :ref:`Using CodeIgniter's Model <model-allow-empty-inserts>`
3940
- Added ``$routes->useSupportedLocalesOnly(true)`` so that the Router returns 404 Not Found if the locale in the URL is not supported in ``Config\App::$supportedLocales``. See :ref:`Localization <localization-in-routes>`

user_guide_src/source/cli/cli_library.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,12 @@ Sometimes you need to ask the user for more information. They might not have pro
3131
arguments, or the script may have encountered an existing file and needs confirmation before overwriting. This is
3232
handled with the ``prompt()`` or ``promptByKey()`` method.
3333

34+
.. note:: Since v4.3.0, you can write tests for these methods with ``PhpStreamWrapper``.
35+
See :ref:`testing-cli-input`.
36+
37+
prompt()
38+
========
39+
3440
You can provide a question by passing it in as the first parameter:
3541

3642
.. literalinclude:: cli_library/002.php

user_guide_src/source/testing/overview.rst

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -252,10 +252,10 @@ component name:
252252

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

255-
.. _testing-overview-stream-filters:
255+
.. _testing-cli-output:
256256

257-
Stream Filters
258-
==============
257+
Testing CLI Output
258+
==================
259259

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

@@ -276,3 +276,23 @@ See :ref:`Testing Traits <testing-overview-traits>`.
276276

277277
If you override the ``setUp()`` or ``tearDown()`` methods in your test, then you must call the ``parent::setUp()`` and
278278
``parent::tearDown()`` methods respectively to configure the ``StreamFilterTrait``.
279+
280+
.. _testing-cli-input:
281+
282+
Testing CLI Input
283+
=================
284+
285+
**PhpStreamWrapper** provides a way to write tests for methods that require user input,
286+
such as ``CLI::prompt()``, ``CLI::wait()``, and ``CLI::input()``.
287+
288+
**Overview of methods**
289+
290+
- ``PhpStreamWrapper::register()`` Register the ``PhpStreamWrapper`` to php protocol.
291+
- ``PhpStreamWrapper::restore()`` Restore the php protocol wrapper back to the PHP built-in wrapper.
292+
- ``PhpStreamWrapper::setContent()`` Set the input data.
293+
294+
An example demonstrating this inside one of your test cases:
295+
296+
.. literalinclude:: overview/019.php
297+
298+
.. note:: It is strongly recommended that ``PhpStreamWrapper`` be registered/unregistered only when needed. Otherwise, it will interfere with other built-in php streams while registered.
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
use CodeIgniter\CLI\CLI;
4+
use CodeIgniter\Test\CIUnitTestCase;
5+
use CodeIgniter\Test\PhpStreamWrapper;
6+
7+
final class SomeTest extends CIUnitTestCase
8+
{
9+
public function testPrompt(): void
10+
{
11+
// Register the PhpStreamWrapper.
12+
PhpStreamWrapper::register();
13+
14+
// Set the user input to 'red'. It will be provided as `php://stdin` output.
15+
$expected = 'red';
16+
PhpStreamWrapper::setContent($expected);
17+
18+
$output = CLI::prompt('What is your favorite color?');
19+
20+
$this->assertSame($expected, $output);
21+
22+
// Restore php protocol wrapper.
23+
PhpStreamWrapper::restore();
24+
}
25+
}

0 commit comments

Comments
 (0)