Skip to content

Commit 1718aa7

Browse files
authored
Merge pull request #1488 from Labstep/testing/transformer
Testing AbstractElasticaToModelTransformer
2 parents 94b1e60 + 7dbe69b commit 1718aa7

File tree

3 files changed

+73
-18
lines changed

3 files changed

+73
-18
lines changed

src/Transformer/AbstractElasticaToModelTransformer.php

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -31,22 +31,4 @@ public function setPropertyAccessor(PropertyAccessorInterface $propertyAccessor)
3131
{
3232
$this->propertyAccessor = $propertyAccessor;
3333
}
34-
35-
/**
36-
* Returns a sorting closure to be used with usort() to put retrieved objects
37-
* back in the order that they were returned by ElasticSearch.
38-
*
39-
* @param array $idPos
40-
* @param string $identifierPath
41-
*
42-
* @return callable
43-
*/
44-
protected function getSortingClosure(array $idPos, $identifierPath)
45-
{
46-
$propertyAccessor = $this->propertyAccessor;
47-
48-
return function ($a, $b) use ($idPos, $identifierPath, $propertyAccessor) {
49-
return $idPos[(string) $propertyAccessor->getValue($a, $identifierPath)] > $idPos[(string) $propertyAccessor->getValue($b, $identifierPath)];
50-
};
51-
}
5234
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the FOSElasticaBundle package.
5+
*
6+
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace FOS\ElasticaBundle\Tests\Unit\Transformer;
13+
14+
use FOS\ElasticaBundle\Transformer\AbstractElasticaToModelTransformer;
15+
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
16+
use FOS\ElasticaBundle\Tests\Unit\UnitTestHelper;
17+
18+
class AbstractElasticaToModelTransformerTest extends UnitTestHelper
19+
{
20+
public function testSetPropertyAccessor()
21+
{
22+
$propertyAccessor = $this->mockPropertyAccesor();
23+
$transformer = $this->mockAbstractElasticaToModelTransformer();
24+
$transformer->setPropertyAccessor($propertyAccessor);
25+
$this->assertEquals($propertyAccessor, $this->getProtectedProperty($transformer, 'propertyAccessor'));
26+
}
27+
28+
protected function mockAbstractElasticaToModelTransformer()
29+
{
30+
$mock = $this
31+
->getMockBuilder(AbstractElasticaToModelTransformer::class)
32+
->getMockForAbstractClass();
33+
return $mock;
34+
}
35+
36+
protected function mockPropertyAccesor()
37+
{
38+
$mock = $this->createMock(PropertyAccessorInterface::class);
39+
return $mock;
40+
}
41+
}

tests/Unit/UnitTestHelper.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the FOSElasticaBundle package.
5+
*
6+
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace FOS\ElasticaBundle\Tests\Unit;
13+
14+
use PHPUnit\Framework\TestCase;
15+
16+
class UnitTestHelper extends TestCase
17+
{
18+
/**
19+
* Gets a protected property on a given object via reflection.
20+
*
21+
* @param object $object instance in which protected value is being modified
22+
* @param string $property property on instance being modified
23+
*/
24+
protected function getProtectedProperty($object, string $property)
25+
{
26+
$reflection = new \ReflectionClass($object);
27+
$reflectionProperty = $reflection->getProperty($property);
28+
$reflectionProperty->setAccessible(true);
29+
30+
return $reflectionProperty->getValue($object);
31+
}
32+
}

0 commit comments

Comments
 (0)