Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Parameter Tokenizer omitting first char of key #29542

Merged
merged 3 commits into from
Sep 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,33 @@
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\Framework\Filter\Template\Tokenizer;

class ParameterTest extends \PHPUnit\Framework\TestCase
use Magento\Catalog\Block\Product\Widget\NewWidget;
use Magento\TestFramework\Helper\Bootstrap;
use PHPUnit\Framework\TestCase;

/**
* Test for \Magento\Framework\Filter\Template\Tokenizer\Parameter.
*/
class ParameterTest extends TestCase
{
/**
* Test for getValue
*
* @dataProvider getValueDataProvider
*
* @param string $string
* @param array $values
* @dataProvider getValueDataProvider
* @return void
*/
public function testGetValue($string, $values)
public function testGetValue($string, $values): void
{
$objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
/** @var \Magento\Framework\Filter\Template\Tokenizer\Parameter $parameter */
$parameter = $objectManager->create(\Magento\Framework\Filter\Template\Tokenizer\Parameter::class);
$objectManager = Bootstrap::getObjectManager();
/** @var Parameter $parameter */
$parameter = $objectManager->create(Parameter::class);
$parameter->setString($string);

foreach ($values as $value) {
Expand All @@ -25,30 +38,36 @@ public function testGetValue($string, $values)
}

/**
* Test for tokenize
*
* @dataProvider tokenizeDataProvider
*
* @param string $string
* @param array $params
* @return void
*/
public function testTokenize($string, $params)
public function testTokenize(string $string, array $params): void
{
$objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
/** @var \Magento\Framework\Filter\Template\Tokenizer\Parameter $parameter */
$parameter = $objectManager->create(\Magento\Framework\Filter\Template\Tokenizer\Parameter::class);
$objectManager = Bootstrap::getObjectManager();
$parameter = $objectManager->create(Parameter::class);
$parameter->setString($string);

$this->assertEquals($params, $parameter->tokenize());
}

/**
* DataProvider for testTokenize
*
* @return array
*/
public function tokenizeDataProvider()
public function tokenizeDataProvider(): array
{
return [
[
' type="Magento\\Catalog\\Block\\Product\\Widget\\NewWidget" display_type="all_products"'
. ' products_count="10" template="product/widget/new/content/new_grid.phtml"',
[
'type' => \Magento\Catalog\Block\Product\Widget\NewWidget::class,
'type' => NewWidget::class,
'display_type' => 'all_products',
'products_count' => 10,
'template' => 'product/widget/new/content/new_grid.phtml'
Expand All @@ -58,12 +77,24 @@ public function tokenizeDataProvider()
' type="Magento\Catalog\Block\Product\Widget\NewWidget" display_type="all_products"'
. ' products_count="10" template="product/widget/new/content/new_grid.phtml"',
[
'type' => \Magento\Catalog\Block\Product\Widget\NewWidget::class,
'type' => NewWidget::class,
'display_type' => 'all_products',
'products_count' => 10,
'template' => 'product/widget/new/content/new_grid.phtml'
]
]
],
[
sprintf(
'type="%s" display_type="all_products" products_count="1" template="content/new_grid.phtml"',
NewWidget::class
),
[
'type' => NewWidget::class,
'display_type' => 'all_products',
'products_count' => 1,
'template' => 'content/new_grid.phtml'
],
],
];
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ public function reset()
*/
public function isWhiteSpace()
{
return trim($this->char()) != $this->char();
return $this->_string === '' ?: trim($this->char()) !== $this->char();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,18 @@ public function tokenize()
{
$parameters = [];
$parameterName = '';
while ($this->next()) {
do {
if ($this->isWhiteSpace()) {
continue;
} elseif ($this->char() != '=') {
}

if ($this->char() !== '=') {
$parameterName .= $this->char();
} else {
$parameters[$parameterName] = $this->getValue();
$parameterName = '';
}
}
} while ($this->next());
return $parameters;
}

Expand Down