Skip to content

Commit 16ba85c

Browse files
committed
Log all tests by default except console, which limits to 5
1 parent ef2f42f commit 16ba85c

File tree

6 files changed

+65
-11
lines changed

6 files changed

+65
-11
lines changed

README.md

+46-4
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ Write the tests in a CSV file ready for import.
5454

5555
Default options are:
5656

57-
- rows: `5`
57+
- rows: `null` (all the tests)
5858
- file: `phpunit_results.csv`
5959

6060
## Json
@@ -67,7 +67,7 @@ Write the tests in a JSON file ready for import.
6767

6868
Default options are:
6969

70-
- rows: `5`
70+
- rows: `null` (all the tests)
7171
- file: `phpunit_results.json`
7272

7373
### MySQL
@@ -80,7 +80,7 @@ Store the test name and the time into a MySQL database. It will override existin
8080

8181
Default credentials are (as array):
8282

83-
- rows: `5`
83+
- rows: `null` (all the tests)
8484
- database: `phpunit_results`
8585
- table: `default`
8686
- username: `root`
@@ -97,10 +97,52 @@ Store the test name and the time into a SQLite database. It will override existi
9797

9898
Default credentials are (as array):
9999

100-
- rows: `5`
100+
- rows: `null` (all the tests)
101101
- database: `phpunit_results.db`
102102
- table: `default`
103103

104+
## Arguments
105+
106+
To override the default configuration per extension, you need to use `<arguments>`in your `phpunit.xml` file
107+
108+
```xml
109+
<extension class="Lloople\PHPUnitExtensions\Runners\SlowestTests\Json">
110+
<arguments>
111+
<integer>10</integer>
112+
<string>phpunit_results_as_json.json</string>
113+
</arguments>
114+
</extension>
115+
```
116+
117+
In the case of the MySQL and SQLite, which needs a database connection, configuration goes as array
118+
119+
<extension class="Lloople\PHPUnitExtensions\Runners\SlowestTests\MySQL">
120+
<arguments>
121+
<null/> <!-- This allows you to log all the tests -->
122+
<array>
123+
<element key="database">
124+
<string>my_phpunit_results</string>
125+
</element>
126+
<element key="table">
127+
<string>project1_test_results</string>
128+
</element>
129+
<element key="username">
130+
<string>homestead</string>
131+
</element>
132+
<element key="password">
133+
<string>secret</string>
134+
</element>
135+
<element key="host">
136+
<string>192.168.12.14</string>
137+
</element>
138+
</array>
139+
</arguments>
140+
</extension>
141+
```
142+
143+
You don't need to override those credentials that already fit to your
144+
usecase, since the class will merge your configuration with the default one
145+
104146
### Changelog
105147
106148
Please see [CHANGELOG](CHANGELOG.md) for more information what has changed recently.

src/Runners/SlowestTests/Channel.php

+5-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ abstract class Channel implements AfterTestHook, AfterLastTestHook
1111

1212
protected $rows;
1313

14-
public function __construct(int $rows = 5)
14+
public function __construct(?int $rows = null)
1515
{
1616
$this->rows = $rows;
1717
}
@@ -42,6 +42,10 @@ abstract protected function printResults(): void;
4242

4343
protected function testsToPrint(): array
4444
{
45+
if ($this->rows === null) {
46+
return $this->tests;
47+
}
48+
4549
return array_slice($this->tests, 0, $this->rows, true);
4650
}
4751

src/Runners/SlowestTests/Console.php

+6
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@
44

55
class Console extends Channel
66
{
7+
8+
public function __construct(?int $rows = 5)
9+
{
10+
parent::__construct($rows);
11+
}
12+
713
protected function printResults(): void
814
{
915
echo PHP_EOL . "Showing the top {$this->rows} slowest tests:" . PHP_EOL;

src/Runners/SlowestTests/Csv.php

+3-2
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@ class Csv extends Channel
66
{
77
protected $file;
88

9-
public function __construct(int $rows = 5, string $file = 'phpunit_results.csv')
9+
public function __construct(?int $rows = null, string $file = 'phpunit_results.csv')
1010
{
11-
$this->rows = $rows;
11+
parent::__construct($rows);
12+
1213
$this->file = $file;
1314
}
1415

src/Runners/SlowestTests/Json.php

+3-2
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@ class Json extends Channel
66
{
77
protected $file;
88

9-
public function __construct(int $rows = 5, string $file = 'phpunit_results.json')
9+
public function __construct(?int $rows = null, string $file = 'phpunit_results.json')
1010
{
11-
$this->rows = $rows;
11+
parent::__construct($rows);
12+
1213
$this->file = $file;
1314
}
1415

src/Runners/SlowestTests/MySQL.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class MySQL extends Channel
1717
'host' => '127.0.0.1'
1818
];
1919

20-
public function __construct(int $rows = 5, array $credentials = [])
20+
public function __construct(?int $rows = null, array $credentials = [])
2121
{
2222
parent::__construct($rows);
2323

@@ -45,7 +45,7 @@ protected function connect(): void
4545
protected function createTableIfNotExists(): void
4646
{
4747
$this->connection->prepare(
48-
"CREATE TABLE IF NOT EXISTS {$this->credentials['table']} (
48+
"CREATE TABLE IF NOT EXISTS `{$this->credentials['table']}` (
4949
`time` float DEFAULT NULL,
5050
`name` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
5151
`method` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,

0 commit comments

Comments
 (0)