1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare( strict_types = 1 ); |
4
|
|
|
|
5
|
|
|
namespace FileFetcher\Tests\Unit; |
6
|
|
|
|
7
|
|
|
use FileFetcher\CallbackFileFetcher; |
8
|
|
|
use FileFetcher\FileFetchingException; |
9
|
|
|
use FileFetcher\InMemoryFileFetcher; |
10
|
|
|
use FileFetcher\StopwatchFileFetcher; |
11
|
|
|
use FileFetcher\ThrowingFileFetcher; |
12
|
|
|
use PHPUnit\Framework\TestCase; |
13
|
|
|
use Symfony\Component\Stopwatch\Stopwatch; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @covers \FileFetcher\StopwatchFileFetcher |
17
|
|
|
*/ |
18
|
|
|
class StopwatchFileFetcherTest extends TestCase { |
19
|
|
|
|
20
|
|
|
private const STOPWATCH_CATEGORY = 'file_fetcher'; |
21
|
|
|
|
22
|
|
|
public function testReturnsSameValueAsInnerFetcher() { |
23
|
|
|
$fetcher = new StopwatchFileFetcher( |
24
|
|
|
new InMemoryFileFetcher( [ |
25
|
|
|
'testFile' => 'MJAU' |
26
|
|
|
] ), |
27
|
|
|
new Stopwatch() |
28
|
|
|
); |
29
|
|
|
|
30
|
|
|
$this->assertSame( |
31
|
|
|
'MJAU', |
32
|
|
|
$fetcher->fetchFile( 'testFile' ) |
33
|
|
|
); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public function testRecordsFetching() { |
37
|
|
|
$stopwatch = new Stopwatch(); |
38
|
|
|
|
39
|
|
|
$fetcher = new StopwatchFileFetcher( |
40
|
|
|
new InMemoryFileFetcher( [ |
41
|
|
|
'testFile' => 'MJAU' |
42
|
|
|
] ), |
43
|
|
|
$stopwatch |
44
|
|
|
); |
45
|
|
|
|
46
|
|
|
$fetcher->fetchFile( 'testFile' ); |
47
|
|
|
|
48
|
|
|
$this->assertSame( |
49
|
|
|
self::STOPWATCH_CATEGORY, |
50
|
|
|
$stopwatch->getEvent( 'testFile' )->getCategory() |
51
|
|
|
); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function testRecordsFetchingWhenAnExceptionIsThrown() { |
55
|
|
|
$stopwatch = $this->createMock( Stopwatch::class ); |
56
|
|
|
$stopwatch->expects( $this->once() )->method( 'start' ); |
57
|
|
|
$stopwatch->expects( $this->once() )->method( 'stop' ); |
58
|
|
|
|
59
|
|
|
$fetcher = new StopwatchFileFetcher( |
60
|
|
|
new ThrowingFileFetcher(), |
61
|
|
|
$stopwatch |
62
|
|
|
); |
63
|
|
|
|
64
|
|
|
$this->expectException( FileFetchingException::class ); |
65
|
|
|
$fetcher->fetchFile( 'testFile' ); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public function testDurationIsRecorded() { |
69
|
|
|
$stopwatch = new Stopwatch(); |
70
|
|
|
|
71
|
|
|
$fetcher = new StopwatchFileFetcher( |
72
|
|
|
new CallbackFileFetcher( function() { |
73
|
|
|
usleep( 1000 ); |
74
|
|
|
return ''; |
75
|
|
|
} ), |
76
|
|
|
$stopwatch |
77
|
|
|
); |
78
|
|
|
|
79
|
|
|
$fetcher->fetchFile( 'testFile' ); |
80
|
|
|
|
81
|
|
|
$this->assertGreaterThanOrEqual( |
82
|
|
|
1, |
83
|
|
|
$stopwatch->getEvent( 'testFile' )->getDuration() |
84
|
|
|
); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
public function testCustomCategoryIsUsed() { |
88
|
|
|
$stopwatch = new Stopwatch(); |
89
|
|
|
|
90
|
|
|
$fetcher = new StopwatchFileFetcher( |
91
|
|
|
new InMemoryFileFetcher( [ |
92
|
|
|
'testFile' => 'MJAU' |
93
|
|
|
] ), |
94
|
|
|
$stopwatch, |
95
|
|
|
'customCategory' |
96
|
|
|
); |
97
|
|
|
|
98
|
|
|
$fetcher->fetchFile( 'testFile' ); |
99
|
|
|
|
100
|
|
|
$this->assertSame( |
101
|
|
|
'customCategory', |
102
|
|
|
$stopwatch->getEvent( 'testFile' )->getCategory() |
103
|
|
|
); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
} |
107
|
|
|
|