BuilderTest   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 0
Metric Value
dl 0
loc 64
c 0
b 0
f 0
wmc 6
lcom 1
cbo 8
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 4 1
A testCallingBuildWithAnInstantiatedObjectWillReturnTheObject() 0 6 1
A testCallingBuildWithANonClassWillThrowAnException() 0 6 1
A testBuildCallSetLoggerIfTheTargetIsALoggerAwareClass() 0 21 1
A testBuildCallSetBuilderIfTargetIsBuilderAware() 0 7 1
A testArgumentsArePassedToConstructor() 0 9 1
1
<?php
2
/**
3
 * This file is part of graze/data-file
4
 *
5
 * Copyright (c) 2016 Nature Delivered Ltd. <https://www.graze.com>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 *
10
 * @license https://github.com/graze/data-file/blob/master/LICENSE.md
11
 * @link    https://github.com/graze/data-file
12
 */
13
14
namespace Graze\DataFile\Test\Unit\Helper\Builder;
15
16
use Graze\DataFile\Helper\Builder\Builder;
17
use Graze\DataFile\Test\Helper\Builder\FakeBuilderAware;
18
use Graze\DataFile\Test\Helper\Builder\FakeConstructable;
19
use Graze\DataFile\Test\Helper\FakeOptionalLogger;
20
use Graze\DataFile\Test\TestCase;
21
use InvalidArgumentException;
22
use Mockery as m;
23
use Psr\Log\LoggerInterface;
24
use Psr\Log\LogLevel;
25
26
class BuilderTest extends TestCase
27
{
28
    /** @var Builder */
29
    private $builder;
30
31
    public function setUp()
32
    {
33
        $this->builder = new Builder();
34
    }
35
36
    public function testCallingBuildWithAnInstantiatedObjectWillReturnTheObject()
37
    {
38
        $object = new \stdClass();
39
40
        static::assertSame($object, $this->builder->build($object));
41
    }
42
43
    public function testCallingBuildWithANonClassWillThrowAnException()
44
    {
45
        static::expectException(InvalidArgumentException::class);
46
47
        $this->builder->build('SomeRandomClassThatDoesNotExist');
48
    }
49
50
    public function testBuildCallSetLoggerIfTheTargetIsALoggerAwareClass()
51
    {
52
        $logger = m::mock(LoggerInterface::class);
53
        $this->builder->setLogger($logger);
54
55
        $logger->shouldReceive('log')
56
               ->with(
57
                   LogLevel::DEBUG,
58
                   Builder::class . ": Building class: {class}",
59
                   ['class' => FakeOptionalLogger::class]
60
               )
61
               ->once();
62
63
        /** @var FakeOptionalLogger $object */
64
        $object = $this->builder->build(FakeOptionalLogger::class);
65
66
        $logger->shouldReceive('log')
67
               ->with(LogLevel::INFO, FakeOptionalLogger::class . ': some text', [])
68
               ->once();
69
        $object->doLog('some text');
70
    }
71
72
    public function testBuildCallSetBuilderIfTargetIsBuilderAware()
73
    {
74
        /** @var FakeBuilderAware $object */
75
        $object = $this->builder->build(FakeBuilderAware::class);
76
77
        static::assertSame($this->builder, $object->getBuilder());
78
    }
79
80
    public function testArgumentsArePassedToConstructor()
81
    {
82
        /** @var FakeConstructable $object */
83
        $object = $this->builder->build(FakeConstructable::class, 'first', 'second');
84
85
        static::assertEquals('first', $object->getFirst());
86
        static::assertEquals('second', $object->getSecond());
87
        static::assertEquals(null, $object->getThird());
88
    }
89
}
90