SelectQueryHydratorTest::testWrongEntitiesCount()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 31

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 31
rs 9.424
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Mdiyakov\DoctrineSolrBundle\Tests\Query\Hydrator;
4
5
use Doctrine\ORM\EntityRepository;
6
use Mdiyakov\DoctrineSolrBundle\Query\Hydrator\SelectQueryHydrator;
7
use Mdiyakov\DoctrineSolrBundle\Schema\Field\ConfigEntityField;
8
use Mdiyakov\DoctrineSolrBundle\Schema\Field\Entity\IntField;
9
use Mdiyakov\DoctrineSolrBundle\Schema\Schema;
10
11
class Query
12
{
13
14
    private $result;
15
16
    public function __construct($result)
17
    {
18
        $this->result = $result;
19
    }
20
21
22
    public function getResult()
23
    {
24
        return $this->result;
25
    }
26
27
}
28
29
class SelectQueryHydratorTest extends \PHPUnit_Framework_TestCase
30
{
31
32
    /**
33
     * @var \PHPUnit_Framework_MockObject_MockObject
34
     */
35
    private $entityRepository;
36
37
38
    /**
39
     * @expectedException \Mdiyakov\DoctrineSolrBundle\Exception\DiscriminatorFieldException
40
     * @expectedExceptionMessage Discriminator field "d_article" must be presented in dataset
41
     */
42
    public function testDiscriminatorValueNotDefined()
43
    {
44
        $configFieldName = 'type';
45
        $configFieldValue = 'article';
46
        $documentFieldName = 'd_article';
47
        $hydrator = $this->createHydrator($configFieldName, $configFieldValue, $documentFieldName);
48
49
        $hydrator->hydrate([
50
            ['id' => 1, 'title' => 'string'],
51
            ['id' => 3, 'title' => 'string2'],
52
        ]);
53
    }
54
55
56
    /**
57
     * @expectedException \Mdiyakov\DoctrineSolrBundle\Exception\DiscriminatorFieldException
58
     * @expectedExceptionMessage Discriminator field value "type" must be "article" but "page" is provided
59
     */
60
    public function testDiscriminatorValueNotCorrect()
61
    {
62
        $configFieldName = 'type';
63
        $configFieldValue = 'article';
64
        $documentFieldName = 'd_article';
65
        $hydrator = $this->createHydrator($configFieldName, $configFieldValue, $documentFieldName);
66
67
        $hydrator->hydrate([
68
            ['id' => 1, 'title' => 'string', $documentFieldName => 'page'],
69
            ['id' => 3, 'title' => 'string2', $documentFieldName => 'page'],
70
        ]);
71
    }
72
73
    /**
74
     * @expectedException \Mdiyakov\DoctrineSolrBundle\Exception\HydratorException
75
     * @expectedExceptionMessage Entities of "ArticleEntity" with "1, 3" primary keys are not found in database
76
     */
77
    public function testWrongEntitiesCount()
78
    {
79
        $configFieldName = 'type';
80
        $configFieldValue = 'article';
81
        $documentFieldName = 'd_article';
82
        $primaryKeyDocumentName = 'd_id';
83
        $primaryKeyFieldName = 'id';
84
        $entityConfigClass = 'ArticleEntity';
85
        $hydrator = $this->createHydrator($configFieldName, $configFieldValue, $documentFieldName, $primaryKeyDocumentName, $entityConfigClass, $primaryKeyFieldName);
86
        $queryBuilder = $this->getMockBuilder('Doctrine\ORM\QueryBuilder')->disableOriginalConstructor()->getMock();
87
88
        $entity = new \stdClass();
89
        $entity->$primaryKeyFieldName = 32;
90
91
        $query = new Query([ $entity ]);
92
93
        $this->entityRepository->expects($this->at(0))
94
            ->method('createQueryBuilder')
95
            ->will($this->returnValue($queryBuilder));
96
97
        $queryBuilder->expects($this->any())->method('addSelect')
98
            ->with('(CASE WHEN entity.id = 1 THEN 2 WHEN entity.id = 3 THEN 1 ELSE 0 END) AS HIDDEN score')
99
            ->will($this->returnValue($queryBuilder));
100
        $queryBuilder->expects($this->any())->method('where')->will($this->returnValue($queryBuilder));
101
        $queryBuilder->expects($this->any())->method('setParameter')->will($this->returnValue($queryBuilder));
102
        $queryBuilder->expects($this->any())->method('addOrderBy')->will($this->returnValue($queryBuilder));
103
        $queryBuilder->expects($this->any())->method('getQuery')->will($this->returnValue($query));
104
105
        $hydrator->hydrate([
106
            [$primaryKeyDocumentName => 1, 'title' => 'string', $documentFieldName => $configFieldValue],
107
            [$primaryKeyDocumentName => 3, 'title' => 'string2', $documentFieldName => $configFieldValue],
108
        ]);
109
    }
110
111
    private function createHydrator(
112
        $configFieldName,
113
        $configFieldValue,
114
        $configDocumentFieldName,
115
        $primaryKeyDocumentName = 'd_id',
116
        $entityConfigClass = 'class',
117
        $primaryKeyFieldName = 'id'
118
    )
119
    {
120
        /** @var EntityRepository|\PHPUnit_Framework_MockObject_MockObject $entityRepository */
121
        $this->entityRepository = $this->getMockBuilder('Doctrine\ORM\EntityRepository')->disableOriginalConstructor()->getMock();
122
        /** @var Schema|\PHPUnit_Framework_MockObject_MockObject $entityRepository $schema */
123
        $schema = $this->getMockBuilder('Mdiyakov\DoctrineSolrBundle\Schema\Schema')->disableOriginalConstructor()->getMock();
124
        $entityConfig = [
125
            'class' => $entityConfigClass,
126
            'config' => [
127
                [ 'name' => $configFieldName, 'value' => $configFieldValue]
128
            ]
129
        ];
130
        $discriminatorField = new ConfigEntityField($configFieldName, $configDocumentFieldName, true, 10);
131
        $primaryKeyField = new IntField($primaryKeyFieldName, $primaryKeyDocumentName, true, 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...IntField::__construct(). ( Ignorable by Annotation )

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

131
        $primaryKeyField = new IntField($primaryKeyFieldName, $primaryKeyDocumentName, true, 10, /** @scrutinizer ignore-type */ false);
Loading history...
132
133
        $schema->expects($this->any())->method('getDiscriminatorConfigField')->will($this->returnValue($discriminatorField));
134
        $schema->expects($this->any())->method('getEntityPrimaryKeyField')->will($this->returnValue($primaryKeyField));
135
136
        return new SelectQueryHydrator(
137
            $this->entityRepository,
138
            $schema,
139
            $entityConfig
140
        );
141
142
    }
143
}