Skip to content

Commit 7802f9f

Browse files
committed
refactor DefaultLogWriterTest
1 parent 7f5842f commit 7802f9f

File tree

4 files changed

+117
-112
lines changed

4 files changed

+117
-112
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@ docs
44
vendor
55
tests/temp
66
.php-cs-fixer.cache
7-
7+
.idea
8+
.phpunit.result.cache

composer.json

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
},
2222
"require-dev": {
2323
"orchestra/testbench": "~3.8.0|^4.0|^5.0|^6.0|^7.0",
24+
"pestphp/pest": "^1.22",
2425
"phpunit/phpunit": "^8.0|^9.0"
2526
},
2627
"autoload": {
@@ -34,10 +35,13 @@
3435
}
3536
},
3637
"scripts": {
37-
"test": "vendor/bin/phpunit"
38+
"test": "vendor/bin/pest"
3839
},
3940
"config": {
40-
"sort-packages": true
41+
"sort-packages": true,
42+
"allow-plugins": {
43+
"pestphp/pest-plugin": true
44+
}
4145
},
4246
"extra": {
4347
"laravel": {

tests/DefaultLogWriterTest.php

Lines changed: 86 additions & 109 deletions
Original file line numberDiff line numberDiff line change
@@ -4,149 +4,126 @@
44

55
use Illuminate\Http\UploadedFile;
66
use Spatie\HttpLogger\DefaultLogWriter;
7+
use function PHPUnit\Framework\assertStringContainsString;
8+
use function PHPUnit\Framework\assertStringNotContainsString;
79

8-
class DefaultLogWriterTest extends TestCase
9-
{
10-
/** @var \Spatie\HttpLogger\DefaultLogWriter */
11-
protected $logger;
10+
beforeEach(function () {
11+
$this->logger = new DefaultLogWriter();
12+
});
1213

13-
public function setUp(): void
14-
{
15-
parent::setup();
14+
test('it logs request method and uri', function () {
15+
foreach (['post', 'put', 'patch', 'delete'] as $method) {
16+
$request = $this->makeRequest($method, $this->uri);
1617

17-
$this->logger = new DefaultLogWriter();
18+
$this->logger->logRequest($request);
1819
}
1920

20-
/** @test */
21-
public function it_logs_request_method_and_uri()
22-
{
23-
foreach (['post', 'put', 'patch', 'delete'] as $method) {
24-
$request = $this->makeRequest($method, $this->uri);
21+
$log = $this->readLogFile();
2522

26-
$this->logger->logRequest($request);
27-
}
23+
assertStringContainsString("POST {$this->uri}", $log);
24+
assertStringContainsString("PUT {$this->uri}", $log);
25+
assertStringContainsString("PATCH {$this->uri}", $log);
26+
assertStringContainsString("DELETE {$this->uri}", $log);
27+
});
2828

29-
$log = $this->readLogFile();
29+
test('it will log the body', function () {
30+
$request = $this->makeRequest('post', $this->uri, [
31+
'name' => 'Name',
32+
]);
3033

31-
$this->assertStringContainsString("POST {$this->uri}", $log);
32-
$this->assertStringContainsString("PUT {$this->uri}", $log);
33-
$this->assertStringContainsString("PATCH {$this->uri}", $log);
34-
$this->assertStringContainsString("DELETE {$this->uri}", $log);
35-
}
34+
$this->logger->logRequest($request);
3635

37-
/** @test */
38-
public function it_will_log_the_body()
39-
{
40-
$request = $this->makeRequest('post', $this->uri, [
41-
'name' => 'Name',
42-
]);
36+
$log = $this->readLogFile();
4337

44-
$this->logger->logRequest($request);
38+
assertStringContainsString('"name":"Name', $log);
39+
});
4540

46-
$log = $this->readLogFile();
41+
test('it will not log excluded fields', function () {
42+
$request = $this->makeRequest('post', $this->uri, [
43+
'name' => 'Name',
44+
'password' => 'none',
45+
'password_confirmation' => 'none',
46+
]);
4747

48-
$this->assertStringContainsString('"name":"Name', $log);
49-
}
48+
$this->logger->logRequest($request);
5049

51-
/** @test */
52-
public function it_will_not_log_excluded_fields()
53-
{
54-
$request = $this->makeRequest('post', $this->uri, [
55-
'name' => 'Name',
56-
'password' => 'none',
57-
'password_confirmation' => 'none',
58-
]);
50+
$log = $this->readLogFile();
5951

60-
$this->logger->logRequest($request);
52+
assertStringNotContainsString('password', $log);
53+
assertStringNotContainsString('password_confirmation', $log);
54+
});
6155

62-
$log = $this->readLogFile();
56+
test('it logs files', function () {
57+
$file = $this->getTempFile();
6358

64-
$this->assertStringNotContainsString('password', $log);
65-
$this->assertStringNotContainsString('password_confirmation', $log);
66-
}
67-
68-
/** @test */
69-
public function it_logs_files()
70-
{
71-
$file = $this->getTempFile();
59+
$request = $this->makeRequest('post', $this->uri, [], [], [
60+
'file' => new UploadedFile($file, 'test.md'),
61+
]);
7262

73-
$request = $this->makeRequest('post', $this->uri, [], [], [
74-
'file' => new UploadedFile($file, 'test.md'),
75-
]);
76-
77-
$this->logger->logRequest($request);
63+
$this->logger->logRequest($request);
7864

79-
$log = $this->readLogFile();
65+
$log = $this->readLogFile();
8066

81-
$this->assertStringContainsString('test.md', $log);
82-
}
67+
assertStringContainsString('test.md', $log);
68+
});
8369

84-
/** @test */
85-
public function it_logs_one_file_in_an_array()
86-
{
87-
$file = $this->getTempFile();
70+
test('it logs one file in an array', function () {
71+
$file = $this->getTempFile();
8872

89-
$request = $this->makeRequest('post', $this->uri, [], [], [
90-
'files' => [
91-
new UploadedFile($file, 'test.md'),
92-
],
93-
]);
73+
$request = $this->makeRequest('post', $this->uri, [], [], [
74+
'files' => [
75+
new UploadedFile($file, 'test.md'),
76+
],
77+
]);
9478

95-
$this->logger->logRequest($request);
79+
$this->logger->logRequest($request);
9680

97-
$log = $this->readLogFile();
81+
$log = $this->readLogFile();
9882

99-
$this->assertStringContainsString('test.md', $log);
100-
}
83+
assertStringContainsString('test.md', $log);
84+
});
10185

102-
/** @test */
103-
public function it_logs_multiple_files_in_an_array()
104-
{
105-
$file = $this->getTempFile();
86+
test('it logs multiple files in an array', function () {
87+
$file = $this->getTempFile();
10688

107-
$request = $this->makeRequest('post', $this->uri, [], [], [
108-
'files' => [
109-
new UploadedFile($file, 'first.doc'),
110-
new UploadedFile($file, 'second.doc'),
111-
],
112-
]);
89+
$request = $this->makeRequest('post', $this->uri, [], [], [
90+
'files' => [
91+
new UploadedFile($file, 'first.doc'),
92+
new UploadedFile($file, 'second.doc'),
93+
],
94+
]);
11395

114-
$this->logger->logRequest($request);
96+
$this->logger->logRequest($request);
11597

116-
$log = $this->readLogFile();
98+
$log = $this->readLogFile();
11799

118-
$this->assertStringContainsString('first.doc', $log);
119-
$this->assertStringContainsString('second.doc', $log);
120-
}
100+
assertStringContainsString('first.doc', $log);
101+
assertStringContainsString('second.doc', $log);
102+
});
121103

122-
/** @test */
123-
public function it_logs_using_the_default_log_level()
124-
{
125-
$request = $this->makeRequest('post', $this->uri, [
126-
'name' => 'Name',
127-
]);
104+
test('it logs using the default log level', function () {
105+
$request = $this->makeRequest('post', $this->uri, [
106+
'name' => 'Name',
107+
]);
128108

129-
$this->logger->logRequest($request);
109+
$this->logger->logRequest($request);
130110

131-
$log = $this->readLogFile();
111+
$log = $this->readLogFile();
132112

133-
$this->assertStringContainsString('testing.INFO', $log);
134-
$this->assertStringContainsString('"name":"Name', $log);
135-
}
113+
assertStringContainsString('testing.INFO', $log);
114+
assertStringContainsString('"name":"Name', $log);
115+
});
136116

137-
/** @test */
138-
public function it_logs_using_the_configured_log_level()
139-
{
140-
config(['http-logger.log_level' => 'debug']);
141-
$request = $this->makeRequest('post', $this->uri, [
142-
'name' => 'Name',
143-
]);
117+
test('it logs using the configured log level', function () {
118+
config(['http-logger.log_level' => 'debug']);
119+
$request = $this->makeRequest('post', $this->uri, [
120+
'name' => 'Name',
121+
]);
144122

145-
$this->logger->logRequest($request);
123+
$this->logger->logRequest($request);
146124

147-
$log = $this->readLogFile();
125+
$log = $this->readLogFile();
148126

149-
$this->assertStringContainsString('testing.DEBUG', $log);
150-
$this->assertStringContainsString('"name":"Name', $log);
151-
}
152-
}
127+
assertStringContainsString('testing.DEBUG', $log);
128+
assertStringContainsString('"name":"Name', $log);
129+
});

tests/Pest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
/*
4+
|--------------------------------------------------------------------------
5+
| Test Case
6+
|--------------------------------------------------------------------------
7+
*/
8+
9+
use Spatie\HttpLogger\Test\TestCase;
10+
11+
uses(TestCase::class)->in('.');
12+
13+
/*
14+
|--------------------------------------------------------------------------
15+
| Expectations
16+
|--------------------------------------------------------------------------
17+
*/
18+
19+
/*
20+
|--------------------------------------------------------------------------
21+
| Functions
22+
|--------------------------------------------------------------------------
23+
*/

0 commit comments

Comments
 (0)