testMockedClassNotFound()   A
last analyzed

Complexity

Conditions 2
Paths 3

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.9332
c 0
b 0
f 0
cc 2
nc 3
nop 0
1
<?php declare(strict_types = 1);
2
3
namespace Mocktainer;
4
5
class FunctionalityOverviewTest extends \Mocktainer\TestCase
6
{
7
8
	public function testMockedClassNotFound()
9
	{
10
		try {
11
			$this->getMocktainer()->create('Mocktainer\Nonexistent');
12
			$this->fail();
13
		} catch (\Mocktainer\ClassNotFoundException $e) {
14
			$this->assertSame('Class Mocktainer\Nonexistent not found', $e->getMessage());
15
			$this->assertSame('Mocktainer\Nonexistent', $e->getClassName());
16
		}
17
	}
18
19 View Code Duplication
	public function testCreateServiceWithAllMocks()
20
	{
21
		$fooService = $this->getMocktainer()->create(FooService::class);
22
		$this->assertInstanceOf(FooService::class, $fooService);
23
		$barService = $fooService->barService;
24
		$this->assertInstanceOf(BarService::class, $barService);
25
		$this->assertStringContainsString('Mock_', get_class($barService));
26
	}
27
28
	public function testPassNonexistentConstructorArgument()
29
	{
30
		try {
31
			$this->getMocktainer()->create(FooService::class, [
32
				'barService' => new BarService(),
33
				'baz' => 'bazValue',
34
			]);
35
			$this->fail();
36
		} catch (\Mocktainer\UnknownConstructorArgumentsException $e) {
37
			$this->assertSame('Passed unknown constructor arguments for class Mocktainer\FooService: baz', $e->getMessage());
38
			$this->assertSame(FooService::class, $e->getClassName());
39
			$this->assertSame(['baz' => 'bazValue'], $e->getArguments());
40
		}
41
	}
42
43 View Code Duplication
	public function testPassConstructorArgumentToConstructor()
44
	{
45
		$barService = new BarService();
46
		$fooService = $this->getMocktainer()->create(FooService::class, [
47
			'barService' => $barService,
48
		]);
49
		$this->assertInstanceOf(FooService::class, $fooService);
50
		$this->assertSame($barService, $fooService->barService);
51
	}
52
53
	public function testRequireUnmockableConstructorArgument()
54
	{
55
		try {
56
			$this->getMocktainer()->create(ClassWithRequiredArrayInConstructor::class);
57
			$this->fail();
58
		} catch (\Mocktainer\UnmockableConstructorArgumentException $e) {
59
			$this->assertSame('Constructor argument "options" of class Mocktainer\ClassWithRequiredArrayInConstructor cannot be mocked', $e->getMessage());
60
			$this->assertSame(ClassWithRequiredArrayInConstructor::class, $e->getClassName());
61
			$this->assertSame('options', $e->getArgumentName());
62
		}
63
	}
64
65 View Code Duplication
	public function testBeQuietAboutOptionalConstructorArgument()
66
	{
67
		$mock = $this->getMocktainer()->create(ClassWithOptionalArrayInConstructor::class);
68
		$this->assertInstanceOf(ClassWithOptionalArrayInConstructor::class, $mock);
69
		$fooService = $mock->fooService;
70
		$this->assertInstanceOf(FooService::class, $fooService);
71
		$this->assertStringContainsString('Mock_', get_class($fooService));
72
		$this->assertSame([], $mock->options);
73
	}
74
75
	public function testUseDefaultConstructorArgument()
76
	{
77
		$mock = $this->getMocktainer()->create(ClassWithDefaultArgumentValueBetweenArguments::class);
78
		$this->assertInstanceOf(ClassWithDefaultArgumentValueBetweenArguments::class, $mock);
79
		$fooService = $mock->fooService;
80
		$this->assertInstanceOf(FooService::class, $fooService);
81
		$this->assertStringContainsString('Mock_', get_class($fooService));
82
		$this->assertSame(['foobar' => 'foobarValue'], $mock->options);
83
		$barService = $mock->barService;
84
		$this->assertInstanceOf(BarService::class, $barService);
85
		$this->assertStringContainsString('Mock_', get_class($barService));
86
	}
87
88
}
89