Completed
Pull Request — master (#4)
by mw
18:43 queued 17:12
created

LoggableContainerBuilderTest::setUp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 9
nc 1
nop 0
1
<?php
2
3
namespace Onoi\CallbackContainer\Tests;
4
5
use Onoi\CallbackContainer\LoggableContainerBuilder;
6
use Onoi\CallbackContainer\CallbackContainerFactory;
7
8
/**
9
 * @covers \Onoi\CallbackContainer\LoggableContainerBuilder
10
 * @group onoi-callback-container
11
 *
12
 * @license GNU GPL v2+
13
 * @since 2.0
14
 *
15
 * @author mwjames
16
 */
17
class LoggableContainerBuilderTest extends \PHPUnit_Framework_TestCase {
18
19
	private $spyLogger;
20
	private $callbackContainerFactory;
21
	private $backtraceSniffer;
22
	private $callFuncMemorySniffer;
23
24
	protected function setUp() {
25
26
		$this->spyLogger = new SpyLogger();
27
		$this->callbackContainerFactory = new CallbackContainerFactory();
28
29
		$this->backtraceSniffer = $this->getMockBuilder( '\Onoi\CallbackContainer\BacktraceSniffer' )
30
			->disableOriginalConstructor()
31
			->getMock();
32
33
		$this->callFuncMemorySniffer = $this->getMockBuilder( '\Onoi\CallbackContainer\CallFuncMemorySniffer' )
34
			->disableOriginalConstructor()
35
			->getMock();
36
	}
37
38
	public function testCanConstruct() {
39
40
		$containerBuilder = $this->getMockBuilder( '\Onoi\CallbackContainer\ContainerBuilder' )
41
			->disableOriginalConstructor()
42
			->getMock();
43
44
		$this->assertInstanceOf(
45
			'\Onoi\CallbackContainer\LoggableContainerBuilder',
46
			new LoggableContainerBuilder( $containerBuilder, $this->backtraceSniffer, $this->callFuncMemorySniffer )
47
		);
48
	}
49
50
	public function testLoadCallbackHandlerWithoutExpectedReturnType() {
51
52
		$instance = new LoggableContainerBuilder(
53
			$this->callbackContainerFactory->newCallbackContainerBuilder()
54
		);
55
56
		$instance->setLogger( $this->spyLogger );
57
		$instance->registerCallback( 'Foo', function() {
58
			return 'abc';
59
		} );
60
61
		$this->assertEquals(
62
			'abc',
63
			$instance->create( 'Foo' )
64
		);
65
66
		$this->assertEquals(
67
			'abc',
68
			$instance->singleton( 'Foo' )
69
		);
70
71
		// Destruct
72
		$instance = null;
0 ignored issues
show
Unused Code introduced by
$instance is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
73
74
		$this->assertNotEmpty(
75
			$this->spyLogger->getLogs()
76
		);
77
	}
78
79
	public function testRegisterFromFile() {
80
81
		$instance = new LoggableContainerBuilder(
82
			$this->callbackContainerFactory->newCallbackContainerBuilder(),
83
			$this->callbackContainerFactory->newBacktraceSniffer(),
84
			$this->callbackContainerFactory->newCallFuncMemorySniffer()
85
		);
86
87
		$instance->setLogger( $this->spyLogger );
88
		$instance->registerFromFile( __DIR__ . '/../Fixtures/fakeCallbackFromFile.php' );
89
90
		$this->assertEquals(
91
			new \stdClass,
92
			$instance->create( 'SomeStdClassFromFile' )
93
		);
94
95
		$this->assertEquals(
96
			new \stdClass,
97
			$instance->singleton( 'SomeStdClassFromFile' )
98
		);
99
100
		// Destruct
101
		$instance = null;
0 ignored issues
show
Unused Code introduced by
$instance is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
102
103
		$this->assertNotEmpty(
104
			$this->spyLogger->getLogs()
105
		);
106
	}
107
108
}
109