StringFieldTest   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 73
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 4
dl 0
loc 73
c 0
b 0
f 0
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A testGetDocumentValueForMixedArray() 0 13 1
A testGetDocumentValueForScalar() 0 26 1
A testGetDocumentValueForObject() 0 11 1
A testGetDocumentValueForArrayOfObjects() 0 13 1
1
<?php
2
3
namespace Mdiyakov\DoctrineSolrBundle\Tests\Schema\Field\Entity;
4
5
use Mdiyakov\DoctrineSolrBundle\Schema\Field\Entity\StringField;
6
7
class StringObject
8
{
9
    private $value;
10
11
    public function __construct($value)
12
    {
13
        $this->value = strval($value);
14
    }
15
16
    public function __toString()
17
    {
18
        return $this->value;
19
    }
20
}
21
22
class StringFieldTest extends \PHPUnit_Framework_TestCase
23
{
24
25
    public function testGetDocumentValueForScalar()
26
    {
27
        $string = 'string';
28
        $int = 123;
29
        $bool = false;
30
        $null = null;
31
32
        $entity = new \stdClass();
33
        $field = new StringField('title', 'd_title', false, 10, false);
0 ignored issues
show
Bug introduced by
false of type false is incompatible with the type string expected by parameter $suggester of Mdiyakov\DoctrineSolrBun...ingField::__construct(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

33
        $field = new StringField('title', 'd_title', false, 10, /** @scrutinizer ignore-type */ false);
Loading history...
34
35
        $entity->title = $string;
36
        $fieldValue = $field->getDocumentFieldValue($entity);
37
        $this->assertEquals($string, $fieldValue);
38
39
        $entity->title = $int;
40
        $fieldValue = $field->getDocumentFieldValue($entity);
41
        $this->assertEquals(strval($int) ,$fieldValue);
42
43
        $entity->title = $bool;
44
        $fieldValue = $field->getDocumentFieldValue($entity);
45
        $this->assertEquals(strval($bool) ,$fieldValue);
46
47
        $entity->title = $null;
48
        $fieldValue = $field->getDocumentFieldValue($entity);
49
50
        $this->assertEquals($null ,$fieldValue);
51
    }
52
53
    public function testGetDocumentValueForObject()
54
    {
55
        $value = 'value';
56
        $object = new StringObject($value);
57
58
        $entity = new \stdClass();
59
        $field = new StringField('title', 'd_title', false, 10, false);
0 ignored issues
show
Bug introduced by
false of type false is incompatible with the type string expected by parameter $suggester of Mdiyakov\DoctrineSolrBun...ingField::__construct(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

59
        $field = new StringField('title', 'd_title', false, 10, /** @scrutinizer ignore-type */ false);
Loading history...
60
61
        $entity->title = $object;
62
        $fieldValue = $field->getDocumentFieldValue($entity);
63
        $this->assertEquals($value, $fieldValue);
64
    }
65
66
    public function testGetDocumentValueForArrayOfObjects()
67
    {
68
        $value1 = 'value';
69
        $value2 = 'value2';
70
71
        $array = [new StringObject($value1), new StringObject($value2)];
72
73
        $entity = new \stdClass();
74
        $field = new StringField('title', 'd_title', false, 10, false);
0 ignored issues
show
Bug introduced by
false of type false is incompatible with the type string expected by parameter $suggester of Mdiyakov\DoctrineSolrBun...ingField::__construct(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

74
        $field = new StringField('title', 'd_title', false, 10, /** @scrutinizer ignore-type */ false);
Loading history...
75
76
        $entity->title = $array;
77
        $fieldValue = $field->getDocumentFieldValue($entity);
78
        $this->assertEquals(join(',', [$value1, $value2]), $fieldValue);
79
    }
80
81
82
    public function testGetDocumentValueForMixedArray()
83
    {
84
        $value1 = 'value';
85
        $value2 = 'value2';
86
87
        $array = [new StringObject($value1), $value2];
88
89
        $entity = new \stdClass();
90
        $field = new StringField('title', 'd_title', false, 10, false);
0 ignored issues
show
Bug introduced by
false of type false is incompatible with the type string expected by parameter $suggester of Mdiyakov\DoctrineSolrBun...ingField::__construct(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

90
        $field = new StringField('title', 'd_title', false, 10, /** @scrutinizer ignore-type */ false);
Loading history...
91
92
        $entity->title = $array;
93
        $fieldValue = $field->getDocumentFieldValue($entity);
94
        $this->assertEquals(join(',', [$value1, $value2]), $fieldValue);
95
    }
96
}