1+ <?php
2+
3+ use Imponeer \Decorators \LogDataOutput \OutputDecorator ;
4+ use PHPUnit \Framework \TestCase ;
5+ use Symfony \Component \Console \Formatter \OutputFormatter ;
6+ use Symfony \Component \Console \Output \BufferedOutput ;
7+
8+ class OutputDecoratorTest extends TestCase
9+ {
10+ /**
11+ * @var OutputDecorator
12+ */
13+ protected $ decorator ;
14+ /**
15+ * @var BufferedOutput
16+ */
17+ protected $ output ;
18+
19+ protected function setUp (): void
20+ {
21+ $ this ->output = new BufferedOutput ();
22+ $ this ->decorator = new OutputDecorator ($ this ->output );
23+
24+ parent ::setUp ();
25+ }
26+
27+ public static function getTestData (): array {
28+ return [
29+ 'writeln (simple) ' => [
30+ 'writeln ' ,
31+ 'test1 ' ,
32+ ],
33+ 'success (simple) ' => [
34+ 'success ' ,
35+ 'test1 ' ,
36+ ],
37+ 'error (simple) ' => [
38+ 'error ' ,
39+ 'test1 ' ,
40+ ],
41+ 'info (simple) ' => [
42+ 'info ' ,
43+ 'test1 ' ,
44+ ],
45+ 'msg (simple) ' => [
46+ 'msg ' ,
47+ 'test1 ' ,
48+ ],
49+ 'fatal (simple) ' => [
50+ 'fatal ' ,
51+ 'test1 ' ,
52+ ],
53+ 'success (with args) ' => [
54+ 'success ' ,
55+ 'test1 %s ' ,
56+ [
57+ 'test2 ' ,
58+ ],
59+ 'test1 test2 ' ,
60+ ],
61+ 'error (with args) ' => [
62+ 'error ' ,
63+ 'test1 %s ' ,
64+ [
65+ 'test2 ' ,
66+ ],
67+ 'test1 test2 ' ,
68+ ],
69+ 'info (with args) ' => [
70+ 'info ' ,
71+ 'test1 %s ' ,
72+ [
73+ 'test2 ' ,
74+ ],
75+ 'test1 test2 ' ,
76+ ],
77+ 'msg (with args) ' => [
78+ 'msg ' ,
79+ 'test1 %s ' ,
80+ [
81+ 'test2 ' ,
82+ ],
83+ 'test1 test2 ' ,
84+ ],
85+ 'fatal (with args) ' => [
86+ 'fatal ' ,
87+ 'test1 %s ' ,
88+ [
89+ 'test2 ' ,
90+ ],
91+ 'test1 test2 ' ,
92+ ],
93+ ];
94+ }
95+
96+ protected function useDecoratorMethod (string $ method , string $ text , array $ params ): string {
97+ if (empty ($ params )) {
98+ $ this ->decorator ->$ method ($ text );
99+ } else {
100+ $ args = $ params ;
101+ array_unshift ($ args , $ text );
102+ call_user_func_array ([$ this ->decorator , $ method ], $ args );
103+ }
104+
105+ return $ this ->output ->fetch ();
106+ }
107+
108+ /**
109+ * @dataProvider getTestData
110+ */
111+ public function testIncrIndent (string $ method , string $ text , array $ args = [], $ shouldReturn = null ): void {
112+ if ($ shouldReturn === null ) {
113+ $ shouldReturn = $ text ;
114+ }
115+
116+ $ buffer = $ this ->useDecoratorMethod ($ method , $ text , $ args );
117+ $ this ->assertSame ($ shouldReturn . PHP_EOL , $ buffer );
118+
119+ $ this ->decorator ->incrIndent ();
120+ $ buffer = $ this ->useDecoratorMethod ($ method , $ text , $ args );
121+ $ this ->assertNotSame ($ shouldReturn . PHP_EOL , $ buffer );
122+ $ this ->assertSame ($ this ->decorator ->renderIndentString () . $ shouldReturn . PHP_EOL , $ buffer );
123+
124+ $ this ->decorator ->incrIndent ();
125+ $ buffer = $ this ->useDecoratorMethod ($ method , $ text , $ args );
126+ $ this ->assertNotSame ($ shouldReturn . PHP_EOL , $ buffer );
127+ $ this ->assertSame ($ this ->decorator ->renderIndentString () . $ shouldReturn . PHP_EOL , $ buffer );
128+ }
129+
130+ /**
131+ * @dataProvider getTestData
132+ */
133+ public function testDecrIndent (string $ method , string $ text , array $ args = [], $ shouldReturn = null ): void
134+ {
135+ if ($ shouldReturn === null ) {
136+ $ shouldReturn = $ text ;
137+ }
138+
139+ $ buffer = $ this ->useDecoratorMethod ($ method , $ text , $ args );
140+ $ this ->assertSame ($ shouldReturn . PHP_EOL , $ buffer );
141+
142+ $ this ->decorator ->incrIndent ();
143+ $ this ->decorator ->decrIndent ();
144+
145+ $ buffer = $ this ->useDecoratorMethod ($ method , $ text , $ args );
146+ $ this ->assertSame ($ shouldReturn . PHP_EOL , $ buffer );
147+ }
148+
149+ /**
150+ * @dataProvider getTestData
151+ */
152+ public function testResetIncr (string $ method , string $ text , array $ args = [], $ shouldReturn = null ): void {
153+ if ($ shouldReturn === null ) {
154+ $ shouldReturn = $ text ;
155+ }
156+
157+ $ buffer = $ this ->useDecoratorMethod ($ method , $ text , $ args );
158+ $ this ->assertSame ($ shouldReturn . PHP_EOL , $ buffer );
159+
160+ $ this ->decorator ->incrIndent ();
161+ $ this ->decorator ->incrIndent ();
162+ $ this ->decorator ->incrIndent ();
163+ $ this ->decorator ->resetIndent ();
164+
165+ $ buffer = $ this ->useDecoratorMethod ($ method , $ text , $ args );
166+ $ this ->assertSame ($ shouldReturn . PHP_EOL , $ buffer );
167+ }
168+
169+ }
0 commit comments