StopwatchFileFetcherTest   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 93
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 9

Importance

Changes 0
Metric Value
wmc 5
lcom 2
cbo 9
dl 0
loc 93
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A testReturnsSameValueAsInnerFetcher() 0 14 1
A testRecordsFetching() 0 18 1
A testRecordsFetchingWhenAnExceptionIsThrown() 0 14 1
A testDurationIsRecorded() 0 19 1
A testCustomCategoryIsUsed() 0 18 1
1
<?php
2
3
declare( strict_types = 1 );
4
5
namespace FileFetcher\Stopwatch\Tests\Unit\PackagePrivate;
6
7
use FileFetcher\CallbackFileFetcher;
8
use FileFetcher\FileFetchingException;
9
use FileFetcher\InMemoryFileFetcher;
10
use FileFetcher\Stopwatch\PackagePrivate\StopwatchFileFetcher;
11
use FileFetcher\ThrowingFileFetcher;
12
use PHPUnit\Framework\TestCase;
13
use Symfony\Component\Stopwatch\Stopwatch;
14
15
/**
16
 * @covers \FileFetcher\Stopwatch\PackagePrivate\StopwatchFileFetcher
17
 *
18
 * @licence BSD-3-Clause
19
 * @author Jeroen De Dauw < [email protected] >
20
 */
21
class StopwatchFileFetcherTest extends TestCase {
22
23
	private const STOPWATCH_CATEGORY = 'file_fetcher';
24
25
	public function testReturnsSameValueAsInnerFetcher() {
26
		$fetcher = new StopwatchFileFetcher(
27
			new InMemoryFileFetcher( [
28
				'testFile' => 'MJAU'
29
			] ),
30
			new Stopwatch(),
31
			'category'
32
		);
33
34
		$this->assertSame(
35
			'MJAU',
36
			$fetcher->fetchFile( 'testFile' )
37
		);
38
	}
39
40
	public function testRecordsFetching() {
41
		$stopwatch = new Stopwatch();
42
43
		$fetcher = new StopwatchFileFetcher(
44
			new InMemoryFileFetcher( [
45
				'testFile' => 'MJAU'
46
			] ),
47
			$stopwatch,
48
			self::STOPWATCH_CATEGORY
49
		);
50
51
		$fetcher->fetchFile( 'testFile' );
52
53
		$this->assertSame(
54
			self::STOPWATCH_CATEGORY,
55
			$stopwatch->getEvent( 'testFile' )->getCategory()
56
		);
57
	}
58
59
	public function testRecordsFetchingWhenAnExceptionIsThrown() {
60
		$stopwatch = $this->createMock( Stopwatch::class );
61
		$stopwatch->expects( $this->once() )->method( 'start' );
62
		$stopwatch->expects( $this->once() )->method( 'stop' );
63
64
		$fetcher = new StopwatchFileFetcher(
65
			new ThrowingFileFetcher(),
66
			$stopwatch,
0 ignored issues
show
Documentation introduced by
$stopwatch is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Symfony\Component\Stopwatch\Stopwatch>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
67
			'category'
68
		);
69
70
		$this->expectException( FileFetchingException::class );
71
		$fetcher->fetchFile( 'testFile' );
72
	}
73
74
	public function testDurationIsRecorded() {
75
		$stopwatch = new Stopwatch();
76
77
		$fetcher = new StopwatchFileFetcher(
78
			new CallbackFileFetcher( function() {
79
				usleep( 1000 );
80
				return '';
81
			} ),
82
			$stopwatch,
83
			'category'
84
		);
85
86
		$fetcher->fetchFile( 'testFile' );
87
88
		$this->assertGreaterThanOrEqual(
89
			1,
90
			$stopwatch->getEvent( 'testFile' )->getDuration()
91
		);
92
	}
93
94
	public function testCustomCategoryIsUsed() {
95
		$stopwatch = new Stopwatch();
96
97
		$fetcher = new StopwatchFileFetcher(
98
			new InMemoryFileFetcher( [
99
				'testFile' => 'MJAU'
100
			] ),
101
			$stopwatch,
102
			'customCategory'
103
		);
104
105
		$fetcher->fetchFile( 'testFile' );
106
107
		$this->assertSame(
108
			'customCategory',
109
			$stopwatch->getEvent( 'testFile' )->getCategory()
110
		);
111
	}
112
113
}
114