|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* LICENSE: This file is subject to the terms and conditions defined in |
|
5
|
|
|
* file 'LICENSE', which is part of this source code package. |
|
6
|
|
|
* |
|
7
|
|
|
* @copyright 2016 Copyright(c) - All rights reserved. |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
namespace Rafrsr\LibArray2Object\Tests; |
|
11
|
|
|
|
|
12
|
|
|
use Rafrsr\LibArray2Object\Array2ObjectBuilder; |
|
13
|
|
|
use Rafrsr\LibArray2Object\Array2ObjectContext; |
|
14
|
|
|
use Rafrsr\LibArray2Object\Matcher\CamelizeMatcher; |
|
15
|
|
|
use Rafrsr\LibArray2Object\Matcher\LevenshteinMatcher; |
|
16
|
|
|
use Rafrsr\LibArray2Object\Parser\FloatParser; |
|
17
|
|
|
use Rafrsr\LibArray2Object\Parser\IntegerParser; |
|
18
|
|
|
use Rafrsr\LibArray2Object\Parser\StringParser; |
|
19
|
|
|
use Rafrsr\LibArray2Object\Writer\AccessorWriter; |
|
20
|
|
|
use Rafrsr\LibArray2Object\Writer\ReflectionWriter; |
|
21
|
|
|
|
|
22
|
|
|
class Array2ObjectBuilderTest extends \PHPUnit_Framework_TestCase |
|
23
|
|
|
{ |
|
24
|
|
|
|
|
25
|
|
|
public function testCreate() |
|
26
|
|
|
{ |
|
27
|
|
|
static::assertInstanceOf(Array2ObjectBuilder::class, Array2ObjectBuilder::create()); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
public function testGetSetContext() |
|
31
|
|
|
{ |
|
32
|
|
|
$builder = Array2ObjectBuilder::create(); |
|
33
|
|
|
$context = new Array2ObjectContext(); |
|
34
|
|
|
$builder->setContext($context); |
|
35
|
|
|
static::assertEquals($context, $builder->getContext()); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
public function testGetSetParsers() |
|
39
|
|
|
{ |
|
40
|
|
|
$builder = Array2ObjectBuilder::create(); |
|
41
|
|
|
$parsers = [new StringParser(), new IntegerParser()]; |
|
42
|
|
|
$builder->setParsers($parsers); |
|
43
|
|
|
|
|
44
|
|
|
static::assertEquals(['string' => new StringParser(), 'integer' => new IntegerParser()], $builder->getParsers()); |
|
45
|
|
|
|
|
46
|
|
|
static::assertTrue($builder->hasParser('string')); |
|
47
|
|
|
static::assertTrue($builder->hasParser(new StringParser())); |
|
48
|
|
|
static::assertFalse($builder->hasParser(new FloatParser())); |
|
49
|
|
|
|
|
50
|
|
|
static::assertTrue($builder->hasParser('string')); |
|
51
|
|
|
$builder->removeParser('string'); |
|
52
|
|
|
static::assertFalse($builder->hasParser('string')); |
|
53
|
|
|
|
|
54
|
|
|
static::assertTrue($builder->hasParser('integer')); |
|
55
|
|
|
$builder->removeParser(new IntegerParser()); |
|
56
|
|
|
static::assertFalse($builder->hasParser('integer')); |
|
57
|
|
|
|
|
58
|
|
|
static::assertFalse($builder->hasParser('float')); |
|
59
|
|
|
$builder->addParser(new FloatParser()); |
|
60
|
|
|
static::assertTrue($builder->hasParser('float')); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
public function testDisableParsers() |
|
64
|
|
|
{ |
|
65
|
|
|
$builder = Array2ObjectBuilder::create(); |
|
66
|
|
|
$builder->disableParser(new StringParser()); |
|
67
|
|
|
$builder->disableParser('integer'); |
|
68
|
|
|
static::assertEquals(['string', 'integer'], $builder->getDisabledParsers()); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
public function testGetSetPropertyMatcher() |
|
72
|
|
|
{ |
|
73
|
|
|
$builder = Array2ObjectBuilder::create(); |
|
74
|
|
|
$matcher = new CamelizeMatcher(); |
|
75
|
|
|
$builder->setPropertyMatcher($matcher); |
|
76
|
|
|
static::assertEquals($matcher, $builder->getPropertyMatcher()); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
public function testGetSetPropertyWriter() |
|
80
|
|
|
{ |
|
81
|
|
|
$builder = Array2ObjectBuilder::create(); |
|
82
|
|
|
$writer = new AccessorWriter(); |
|
83
|
|
|
$builder->setPropertyWriter($writer); |
|
84
|
|
|
static::assertEquals($writer, $builder->getPropertyWriter()); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
public function testBuild() |
|
88
|
|
|
{ |
|
89
|
|
|
//build basic |
|
90
|
|
|
$builder = Array2ObjectBuilder::create(); |
|
91
|
|
|
$array2Object = $builder->build(); |
|
92
|
|
|
|
|
93
|
|
|
static::assertEquals($array2Object->getContext()->getMatcher(), new CamelizeMatcher()); |
|
94
|
|
|
static::assertEquals($array2Object->getContext()->getWriter(), new AccessorWriter()); |
|
95
|
|
|
|
|
96
|
|
|
$parsers = $array2Object->getContext()->getParsers(); |
|
97
|
|
|
|
|
98
|
|
|
foreach ($parsers as $name => $parser) { |
|
99
|
|
|
$builder->disableParser($name); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
$builder->setPropertyMatcher(new LevenshteinMatcher()); |
|
103
|
|
|
$builder->setPropertyWriter(new ReflectionWriter()); |
|
104
|
|
|
|
|
105
|
|
|
$array2Object = $builder->build(); |
|
106
|
|
|
|
|
107
|
|
|
static::assertEquals($array2Object->getContext()->getMatcher(), new LevenshteinMatcher()); |
|
108
|
|
|
static::assertEquals($array2Object->getContext()->getWriter(), new ReflectionWriter()); |
|
109
|
|
|
static::assertEmpty($array2Object->getContext()->getParsers()); |
|
110
|
|
|
} |
|
111
|
|
|
} |
|
112
|
|
|
|