Skip to content

Commit c4745f2

Browse files
authored
Merge pull request #17 from imponeer/add-phpunit-tests-testing-with-php-8.2
PHP 8.2 support + tests
2 parents b67ce38 + c7facb5 commit c4745f2

File tree

6 files changed

+216
-18
lines changed

6 files changed

+216
-18
lines changed

.github/workflows/on_pull_request.yml

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,9 @@ jobs:
1313
max-parallel: 3
1414
matrix:
1515
php:
16-
- 7.2
17-
- 7.3
18-
- 7.4
1916
- 8.0
2017
- 8.1
18+
- 8.2
2119
composer:
2220
- 2
2321
name: Test - php:${{ matrix.php }}; composer:${{ matrix.composer }}
@@ -28,14 +26,15 @@ jobs:
2826
uses: shivammathur/setup-php@master
2927
with:
3028
php-version: ${{ matrix.php }}
31-
extensions: curl, gd, pdo_mysql, json, mbstring, pcre, session
32-
ini-values: post_max_size=256M
29+
extensions: mbstring, pcre
3330
coverage: none
3431
tools: composer:v${{ matrix.composer }}
3532
- name: Composer validate
3633
run: composer validate --strict --no-check-lock
3734
- name: Install Composer dependencies (with dev)
3835
run: composer install --no-progress --no-suggest --prefer-dist --optimize-autoloader
36+
- name: PHPUnit
37+
run: composer test
3938
- name: Install Composer dependencies (without dev)
4039
run: composer install --no-progress --no-dev --no-suggest --prefer-dist --optimize-autoloader
4140

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
/vendor/
22
/composer.lock
33
/.idea/
4+
/.phpunit.result.cache
5+
/.phpunit.cache/

composer.json

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
"description": "Small decorator that extends Symfony OutputInterface delivered class with few options for easier to log data",
44
"type": "library",
55
"require": {
6-
"symfony/console": "^5.0",
7-
"php": ">=7.2"
6+
"symfony/console": "^6",
7+
"php": ">=8.0.2"
88
},
99
"license": "MIT",
1010
"authors": [
@@ -23,5 +23,11 @@
2323
"console",
2424
"output",
2525
"decorator"
26-
]
26+
],
27+
"require-dev": {
28+
"phpunit/phpunit": "^9|^10"
29+
},
30+
"scripts": {
31+
"test": "phpunit --testdox"
32+
}
2733
}

phpunit.xml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?xml version="1.0"?>
2+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.0/phpunit.xsd" backupGlobals="true" bootstrap="vendor/autoload.php" colors="false" processIsolation="false" stopOnError="false" stopOnFailure="false" stopOnIncomplete="false" stopOnSkipped="false" stopOnRisky="false" timeoutForSmallTests="1" timeoutForMediumTests="10" timeoutForLargeTests="60">
3+
<coverage includeUncoveredFiles="true">
4+
<include>
5+
<directory>src</directory>
6+
</include>
7+
</coverage>
8+
<testsuites>
9+
<testsuite name="All">
10+
<directory suffix="Test.php">tests/</directory>
11+
</testsuite>
12+
</testsuites>
13+
</phpunit>

src/OutputDecorator.php

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,15 @@ public function fatal(string $message, ...$params): void
6969
$this->write('<error>' . vsprintf($message, $params) . '</error>', true);
7070
}
7171

72+
/**
73+
* Renders current indent string
74+
*
75+
* @return string
76+
*/
77+
public function renderIndentString(): string {
78+
return str_repeat(' ', $this->indent * 2);
79+
}
80+
7281
/**
7382
* @inheritDoc
7483
*/
@@ -77,7 +86,7 @@ public function write($messages, $newline = false, $options = self::OUTPUT_NORMA
7786
if ($this->indent > 0) {
7887
$tmpMsg = '';
7988
foreach (explode(PHP_EOL, $messages) as $line) {
80-
$tmpMsg .= str_repeat(' ', $this->indent * 2) . $line . PHP_EOL;
89+
$tmpMsg .= $this->renderIndentString() . $line . PHP_EOL;
8190
}
8291
$messages = rtrim($tmpMsg);
8392
}
@@ -150,55 +159,55 @@ public function setVerbosity($level)
150159
/**
151160
* @inheritDoc
152161
*/
153-
public function getVerbosity()
162+
public function getVerbosity(): int
154163
{
155164
return $this->output->getVerbosity();
156165
}
157166

158167
/**
159168
* @inheritDoc
160169
*/
161-
public function isQuiet()
170+
public function isQuiet(): bool
162171
{
163172
return $this->output->isQuiet();
164173
}
165174

166175
/**
167176
* @inheritDoc
168177
*/
169-
public function isVerbose()
178+
public function isVerbose(): bool
170179
{
171180
return $this->output->isVerbose();
172181
}
173182

174183
/**
175184
* @inheritDoc
176185
*/
177-
public function isVeryVerbose()
186+
public function isVeryVerbose(): bool
178187
{
179188
return $this->output->isVeryVerbose();
180189
}
181190

182191
/**
183192
* @inheritDoc
184193
*/
185-
public function isDebug()
194+
public function isDebug(): bool
186195
{
187196
return $this->output->isDebug();
188197
}
189198

190199
/**
191200
* @inheritDoc
192201
*/
193-
public function setDecorated($decorated)
202+
public function setDecorated($decorated): void
194203
{
195204
$this->output->setDecorated($decorated);
196205
}
197206

198207
/**
199208
* @inheritDoc
200209
*/
201-
public function isDecorated()
210+
public function isDecorated(): bool
202211
{
203212
return $this->output->isDecorated();
204213
}
@@ -208,15 +217,15 @@ public function isDecorated()
208217
*
209218
* @param OutputFormatterInterface $formatter Output formatter
210219
*/
211-
public function setFormatter(OutputFormatterInterface $formatter)
220+
public function setFormatter(OutputFormatterInterface $formatter): void
212221
{
213222
$this->output->setFormatter($formatter);
214223
}
215224

216225
/**
217226
* @inheritDoc
218227
*/
219-
public function getFormatter()
228+
public function getFormatter(): OutputFormatterInterface
220229
{
221230
return $this->output->getFormatter();
222231
}

tests/OutputDecoratorTest.php

Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
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

Comments
 (0)