Skip to content

Commit

Permalink
Fix test
Browse files Browse the repository at this point in the history
follow-up of b8c83de
  • Loading branch information
W0rma committed Nov 1, 2024
1 parent ea28741 commit 0d627b4
Showing 1 changed file with 12 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@

namespace Hateoas\Tests\Expression;

use Hateoas\Expression\ExpressionEvaluator;
use Hateoas\Expression\LinkExpressionFunction;
use Hateoas\Helper\LinkHelper;
use Hateoas\Tests\TestCase;
use JMS\Serializer\Expression\ExpressionEvaluator;
use Symfony\Component\ExpressionLanguage\ExpressionLanguage;

class LinkExpressionFunctionTest extends TestCase
Expand All @@ -18,12 +18,14 @@ public function testEvaluate()

$linkHelperMock = $this->mockHelper('/foo', $object, 'self', false);

$expressionEvaluator = new ExpressionEvaluator(new ExpressionLanguage());
$expressionEvaluator->registerFunction(new LinkExpressionFunction($linkHelperMock));
$expressionLanguage = new ExpressionLanguage();
$expressionLanguage->registerProvider(new LinkExpressionFunction());

$expressionEvaluator = new ExpressionEvaluator($expressionLanguage, ['link_helper' => $linkHelperMock]);

$this->assertEquals(
'/foo',
$expressionEvaluator->evaluate('expr(link(object, "self", false))', $object)
$expressionEvaluator->evaluate('link(object, "self", false)', ['object' => $object])
);
}

Expand All @@ -34,37 +36,29 @@ public function testCompile()
$linkHelperMock = $this->mockHelper('/foo', $object, 'self', false);

$expressionLanguage = new ExpressionLanguage();
$expressionLanguage->registerProvider(new LinkExpressionFunction());

$expressionEvaluator = new ExpressionEvaluator($expressionLanguage);
$expressionEvaluator->registerFunction(new LinkExpressionFunction($linkHelperMock));

$compiledExpression = $expressionLanguage->compile('link(object, "self", false)', ['object', 'link_helper']);
$compiledExpression = $expressionLanguage->compile('link(object, "self", false)', ['object']);

// setup variables for expression eval
$object = $object;
$link_helper = $linkHelperMock;

$this->assertEquals('/foo', eval(sprintf('return %s;', $compiledExpression)));
}

/**
* @param string $result
* @param \stdClass $expectedObject
* @param string $expectedRel
* @param bool $expectedAbsolute
*
* @return LinkHelper
*/
private function mockHelper($result, $expectedObject, $expectedRel, $expectedAbsolute)
private function mockHelper(string $result, \stdClass $expectedObject, string $expectedRel, bool $expectedAbsolute): LinkHelper
{
$linkHelperMock = $this
->getMockBuilder('Hateoas\Helper\LinkHelper')
->getMockBuilder(LinkHelper::class)
->disableOriginalConstructor()
->getMock();

$linkHelperMock
->expects($this->once())
->method('getLinkHref')
->will($this->returnValue('/foo'))
->will($this->returnValue($result))
->with($expectedObject, $expectedRel, $expectedAbsolute);

return $linkHelperMock;
Expand Down

0 comments on commit 0d627b4

Please sign in to comment.