Skip to content

Instantly share code, notes, and snippets.

@dfeyer dfeyer/ContextSpec.php
Last active Nov 11, 2016

Embed
What would you like to do?
<?php
namespace TYPO3\Eel\Tests\Spec;
use TYPO3\Eel\Context;
use TYPO3\Eel\Tests\Unit\Fixtures\TestObject;
describe(Context::class, function () {
describe('->unwrap', function() {
it('unwrap simple value', function () {
$values = [
['Test', 'Test'],
[true, true],
[42, 42],
[7.0, 7.0],
[null, null]
];
foreach ($values as $value) {
list ($value, $expectedUnwrappedValue) = $value;
$context = new Context($value);
expect($context->unwrap())->toBe($expectedUnwrappedValue);
}
});
it('unwrap array value', function () {
$values = [
[[], []],
[[1, 2, 3], [1, 2, 3]],
// Unwrap has to be recursive
[[new Context('Foo')], ['Foo']],
[['arr' => [new Context('Foo')]], ['arr' => ['Foo']]]
];
foreach ($values as $value) {
list ($value, $expectedUnwrappedValue) = $value;
$context = new Context($value);
expect($context->unwrap())->toBe($expectedUnwrappedValue);
}
});
});
describe('->get', function() {
it('get value by path for array values', function () {
$values = [
[[], 'foo', null],
[['foo' => 'bar'], 'foo', 'bar'],
[[1, 2, 3], '1', 2],
[['foo' => ['bar' => 'baz']], 'foo', ['bar' => 'baz']],
[new \ArrayObject(['foo' => 'bar']), 'foo', 'bar']
];
foreach ($values as $value) {
list ($value, $path, $expectedGetValue) = $value;
$context = new Context($value);
expect($context->get($path))->toBe($expectedGetValue);
}
});
it('get value by path for object values', function () {
$simpleObject = new \stdClass();
$simpleObject->foo = 'bar';
$getterObject = new TestObject();
$getterObject->setProperty('some value');
$getterObject->setBooleanProperty(true);
$values = [
[$simpleObject, 'bar', null],
[$simpleObject, 'foo', 'bar'],
[$getterObject, 'foo', null],
[$getterObject, 'callMe', null],
[$getterObject, 'booleanProperty', true]
];
foreach ($values as $value) {
list ($value, $path, $expectedGetValue) = $value;
$context = new Context($value);
expect($context->get($path))->toBe($expectedGetValue);
}
});
});
});
<?php
namespace TYPO3\Eel\Tests\Spec\Validation;
use Kahlan\Plugin\Double;
use TYPO3\Eel\Validation\ExpressionSyntaxValidator;
describe(ExpressionSyntaxValidator::class, function () {
beforeAll(function () {
$this->validator = Double::instance(['extends' => ExpressionSyntaxValidator::class]);
});
describe('->validate', function () {
context('with valid expression', function () {
it('passes', function () {
expect($this->validator->validate('foo.bar() * (18 + 2)')->hasErrors())->toBe(false);
});
});
context('with invalid expression', function () {
it('returns errors', function () {
expect($this->validator->validate('foo.bar( + (18 + 2)')->hasErrors())->toBe(true);
});
it('gives error position information', function () {
$errorArguments = $this->validator
->validate('foo.bar( + (18 + 2)')
->getFirstError()
->getArguments();
expect($errorArguments[0])->toBe('foo.bar( + (18 + 2)');
expect($errorArguments[1])->toBe(7);
expect($errorArguments[2])->toBe('( + (18 + 2)');
});
});
});
});
TYPO3\Eel\Context
->unwrap
✓ it unwrap simple value
✓ it unwrap array value
->get
✓ it get value by path for array values
✓ it get value by path for object values
TYPO3\Eel\Validation\ExpressionSyntaxValidator
->validate
with valid expression
✓ it passes
with invalid expression
✓ it returns errors
✓ it gives error position information
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.