Skip to content

Commit

Permalink
fixed year choice test
Browse files Browse the repository at this point in the history
  • Loading branch information
koertho committed Jun 28, 2018
1 parent 709cd1e commit 3510d25
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 28 deletions.
3 changes: 3 additions & 0 deletions src/Choice/YearChoice.php
Expand Up @@ -55,6 +55,9 @@ protected function collect()
break;
case AbstractType::VALUE_TYPE_ARRAY:
$value = array_column(StringUtil::deserialize($entry->initialValueArray), 'value');
if (empty($value) || empty($value[0])) {
continue;
}
$columns[] = $entry->field.' IN ('.implode(',', $value).')';
break;
}
Expand Down
109 changes: 81 additions & 28 deletions tests/Choice/YearChoiceTest.php
Expand Up @@ -12,6 +12,7 @@
use Contao\System;
use Contao\TestCase\ContaoTestCase;
use HeimrichHannot\FilterBundle\Choice\YearChoice;
use HeimrichHannot\FilterBundle\Filter\AbstractType;
use HeimrichHannot\FilterBundle\Model\FilterConfigElementModel;
use HeimrichHannot\UtilsBundle\Model\ModelUtil;
use Symfony\Component\HttpKernel\Kernel;
Expand All @@ -30,71 +31,114 @@ public function testCollect()
$this->assertEmpty($yearChoice->getChoices([]));

$filterMock = ['dataContainer' => 'tl_news'];
$parentElementMock = $this->mockClassWithProperties(FilterConfigElementModel::class, [
$parentElements = [];
$parentElements[] = $this->mockClassWithProperties(FilterConfigElementModel::class, [
'isInitial' => '1',
'initialValueType' => AbstractType::VALUE_TYPE_ARRAY,
'initialValueArray' => serialize([]),
]);
$elementMock = $this->mockClassWithProperties(FilterConfigElementModel::class, [
'id' => 1,
]);
$context = [
'filter' => $filterMock,
'parentElement' => $parentElementMock,
'element' => $elementMock,
'elements' => $parentElements,
];
$this->assertEmpty($yearChoice->getChoices($context));

$parentElementMock = $this->mockClassWithProperties(FilterConfigElementModel::class, [
'isInitial' => '1',
'initialValueArray' => serialize([['value' => '']]),
]);
$parentElementMock = new \stdClass();
$parentElementMock->id = 5;
$parentElementMock->isInitial = '1';
$parentElementMock->initialValueType = AbstractType::VALUE_TYPE_ARRAY;
$parentElementMock->initialValueArray = serialize([['value' => '']]);
$parentElementMock->field = 'pid';
$parentElements = [$parentElementMock];
$context = [
'filter' => $filterMock,
'parentElement' => $parentElementMock,
'element' => $elementMock,
'elements' => $parentElements,
];
$this->assertEmpty($yearChoice->getChoices($context));

$parentElementMock = $this->mockClassWithProperties(FilterConfigElementModel::class, [
'isInitial' => '1',
'initialValueArray' => serialize([['value' => '1'], ['value' => '2']]),
]);
$parentElementMock = new \stdClass();
$parentElementMock->id = 5;
$parentElementMock->isInitial = '1';
$parentElementMock->initialValueType = AbstractType::VALUE_TYPE_ARRAY;
$parentElementMock->initialValueArray = serialize([['value' => '1'], ['value' => '2']]);
$parentElementMock->field = 'pid';
$parentElements = [$parentElementMock];
$context = [
'filter' => $filterMock,
'parentElement' => $parentElementMock,
'element' => $elementMock,
'elements' => $parentElements,
];
$this->assertEmpty($yearChoice->getChoices($context));

$parentElementMock = $this->mockClassWithProperties(FilterConfigElementModel::class, [
'isInitial' => '1',
'initialValueArray' => serialize([['value' => '2'], ['value' => '3']]),
]);
$parentElementMock = new \stdClass();
$parentElementMock->id = 5;
$parentElementMock->isInitial = '1';
$parentElementMock->initialValueType = AbstractType::VALUE_TYPE_ARRAY;
$parentElementMock->initialValueArray = serialize([['value' => '2'], ['value' => '3']]);
$parentElementMock->field = 'pid';
$parentElements = [$parentElementMock];
$context = [
'filter' => $filterMock,
'parentElement' => $parentElementMock,
'element' => $elementMock,
'elements' => $parentElements,
];
$this->assertSame([
2018 => '2018',
2017 => '2017',
2016 => '2016',
], $yearChoice->getChoices($context));

$parentElementMock = $this->mockClassWithProperties(FilterConfigElementModel::class, [
'value' => '1',
]);
$parentElementMock = new \stdClass();
$parentElementMock->id = 5;
$parentElementMock->isInitial = '1';
$parentElementMock->initialValueType = AbstractType::VALUE_TYPE_SCALAR;
$parentElementMock->initialValue = 1;
$parentElementMock->field = 'pid';
$parentElements = [$parentElementMock];
$context = [
'filter' => $filterMock,
'parentElement' => $parentElementMock,
'element' => $elementMock,
'elements' => $parentElements,
];
$this->assertEmpty($yearChoice->getChoices($context));

$parentElementMock = $this->mockClassWithProperties(FilterConfigElementModel::class, [
'value' => '2',
]);
$parentElementMock = new \stdClass();
$parentElementMock->id = 5;
$parentElementMock->isInitial = '1';
$parentElementMock->initialValueType = AbstractType::VALUE_TYPE_SCALAR;
$parentElementMock->initialValue = 2;
$parentElementMock->field = 'pid';
$parentElements = [$parentElementMock];
$context = [
'filter' => $filterMock,
'parentElement' => $parentElementMock,
'element' => $elementMock,
'elements' => $parentElements,
];
$this->assertSame([
2018 => '2018',
2017 => '2017',
2016 => '2016',
], $yearChoice->getChoices($context));

$parentElementMock = new \stdClass();
$parentElementMock->id = 5;
$parentElementMock->isInitial = '1';
$parentElementMock->initialValueType = AbstractType::VALUE_TYPE_SCALAR;
$parentElementMock->initialValue = 2;
$parentElementMock->field = 'pid';
$parentElements = [$parentElementMock];
$context = [
'filter' => $filterMock,
'element' => $elementMock,
'elements' => $parentElements,
'latest' => true,
];
$this->assertSame([2018 => '2018'], $yearChoice->getChoices($context));
}

public function getYearChoiceInstance()
Expand All @@ -108,19 +152,28 @@ public function getYearChoiceInstance()

$framework = $this->mockContaoFramework();
$modelUtil = $this->createMock(ModelUtil::class);
$modelUtil->method('findModelInstancesBy')->willReturnCallback(function ($table, $field, $value) {
switch ($value[0]) {
$modelUtil->method('findModelInstancesBy')->willReturnCallback(function ($table, $fields, $values, $options) {
if (empty($values)) {
$values[0] = substr($fields[0], strpos($fields[0], '(') + 1, 1);
}
$return = null;
switch ($values[0]) {
default:
case '1':
return null;
case '2':
return [
$return = [
$this->mockClassWithProperties(Model::class, ['date' => 1529916218]), //2018
$this->mockClassWithProperties(Model::class, ['date' => 1502496000]), //2017
$this->mockClassWithProperties(Model::class, ['date' => 1486252800]), //2017
$this->mockClassWithProperties(Model::class, ['date' => 1462406400]), //2016
];
}
if (isset($options['limit']) && 1 === $options['limit']) {
return [$return[0]];
}

return $return;
});
$yearChoice = new YearChoice($framework, $modelUtil);

Expand Down

0 comments on commit 3510d25

Please sign in to comment.