|
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
|
|
|
|