2
2
Testing CLI Commands
3
3
####################
4
4
5
+ .. contents ::
6
+ :local:
7
+ :depth: 3
8
+
9
+ .. _using-mock-input-output :
10
+
11
+ *********************
12
+ Using MockInputOutput
13
+ *********************
14
+
15
+ .. versionadded :: 4.5.0
16
+
17
+ MockInputOutput
18
+ ===============
19
+
20
+ **MockInputOutput ** provides a esay way to write tests for commands that require
21
+ user input, such as ``CLI::prompt() ``, ``CLI::wait() ``, and ``CLI::input() ``.
22
+
23
+ You can replace the ``InputOutput `` class with ``MockInputOutput `` during test
24
+ execution to capture inputs and outputs.
25
+
26
+ .. note :: When you use ``MockInputOutput``, you don't need to use
27
+ :ref: `stream-filter-trait `, :ref: `ci-test-stream-filter `, and
28
+ :ref: `php-stream-wrapper `.
29
+
30
+ Helper Methods
31
+ ---------------
32
+
33
+ getOutput(?int $index = null): string
34
+ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
35
+
36
+ Gets the output.
37
+
38
+ - If you call it like ``$io->getOutput() ``, it returns the whole output string.
39
+ - If you specify ``0 `` or a positive number, it returns the output array item.
40
+ Each item has the output of a ``CLI::fwrite() `` call.
41
+ - If you specify a negative number ``-n ``, it returns the last ``n ``-th item of
42
+ the output array.
43
+
44
+ getOutputs(): array
45
+ ^^^^^^^^^^^^^^^^^^^
46
+
47
+ Returns the output array. Each item has the output of a ``CLI::fwrite() `` call.
48
+
49
+ How to Use
50
+ ==========
51
+
52
+ - ``CLI::setInputOutput() `` can set the ``MockInputOutput `` instance to the ``CLI `` class.
53
+ - ``CLI::resetInputOutput() `` resets the ``InputOutput `` instance in the ``CLI `` class.
54
+ - ``MockInputOutput::setInputs() `` sets the user input array.
55
+ - ``MockInputOutput::getOutput() `` gets the command output.
56
+
57
+ The following test code is to test the command ``spark db:table ``:
58
+
59
+ .. literalinclude :: cli/001.php
60
+
61
+ ***********************
62
+ Without MockInputOutput
63
+ ***********************
64
+
5
65
.. _testing-cli-output :
6
66
7
67
Testing CLI Output
8
68
==================
9
69
70
+ .. _stream-filter-trait :
71
+
10
72
StreamFilterTrait
11
73
-----------------
12
74
@@ -17,10 +79,11 @@ StreamFilterTrait
17
79
You may need to test things that are difficult to test. Sometimes, capturing a stream, like PHP's own STDOUT, or STDERR,
18
80
might be helpful. The ``StreamFilterTrait `` helps you capture the output from the stream of your choice.
19
81
20
- **Overview of methods **
82
+ How to Use
83
+ ^^^^^^^^^^
21
84
22
- - ``StreamFilterTrait::getStreamFilterBuffer() `` Get the captured data from the buffer.
23
- - ``StreamFilterTrait::resetStreamFilterBuffer() `` Reset captured data.
85
+ - ``StreamFilterTrait::getStreamFilterBuffer() `` gets the captured data from the buffer.
86
+ - ``StreamFilterTrait::resetStreamFilterBuffer() `` resets captured data.
24
87
25
88
An example demonstrating this inside one of your test cases:
26
89
@@ -32,6 +95,8 @@ See :ref:`Testing Traits <testing-overview-traits>`.
32
95
If you override the ``setUp() `` or ``tearDown() `` methods in your test, then you must call the ``parent::setUp() `` and
33
96
``parent::tearDown() `` methods respectively to configure the ``StreamFilterTrait ``.
34
97
98
+ .. _ci-test-stream-filter :
99
+
35
100
CITestStreamFilter
36
101
------------------
37
102
@@ -40,7 +105,8 @@ CITestStreamFilter
40
105
If you need to capture streams in only one test, then instead of using the StreamFilterTrait trait, you can manually
41
106
add a filter to streams.
42
107
43
- **Overview of methods **
108
+ How to Use
109
+ ^^^^^^^^^^
44
110
45
111
- ``CITestStreamFilter::registration() `` Filter registration.
46
112
- ``CITestStreamFilter::addOutputFilter() `` Adding a filter to the output stream.
@@ -55,6 +121,8 @@ add a filter to streams.
55
121
Testing CLI Input
56
122
=================
57
123
124
+ .. _php-stream-wrapper :
125
+
58
126
PhpStreamWrapper
59
127
----------------
60
128
@@ -68,7 +136,8 @@ such as ``CLI::prompt()``, ``CLI::wait()``, and ``CLI::input()``.
68
136
see `The streamWrapper class <https://www.php.net/manual/en/class.streamwrapper.php >`_
69
137
in the PHP maual.
70
138
71
- **Overview of methods **
139
+ How to Use
140
+ ^^^^^^^^^^
72
141
73
142
- ``PhpStreamWrapper::register() `` Register the ``PhpStreamWrapper `` to the ``php `` protocol.
74
143
- ``PhpStreamWrapper::restore() `` Restore the php protocol wrapper back to the PHP built-in wrapper.
0 commit comments