Skip to content

Commit

Permalink
fix: trim leading/trailing whitespaces from model-values (#28)
Browse files Browse the repository at this point in the history
  • Loading branch information
rommelfreddy authored and hamail-tahir committed Sep 5, 2022
1 parent e747626 commit 35d4fc5
Show file tree
Hide file tree
Showing 4 changed files with 132 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/Model/Request/SubModel/AbstractModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ public function __get($field)
*/
public function commonSetter($field, $arguments)
{
$arguments = $this->cleanValues($arguments);
if (is_array($arguments) && $arguments[0] !== '') {
if (property_exists($this, 'settings') && key_exists($field, $this->settings)) { // If it's a setting, save argument into settings
// @ToDo: find a better structure
Expand All @@ -114,6 +115,17 @@ public function commonSetter($field, $arguments)
return $this;
}

private function cleanValues($value)
{
if (is_array($value)) {
return array_map([$this, 'cleanValues'], $value);
} elseif (is_string($value)) {
return trim($value);
}

return $value;
}

/**
* Common getter.
*
Expand Down
29 changes: 29 additions & 0 deletions tests/Mocks/SimpleRequestModel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace RatePAY\Tests\Mocks;

use RatePAY\Model\Request\SubModel\AbstractModel;

class SimpleRequestModel extends AbstractModel
{
public $admittedFields = [
'FieldDefault' => [
'mandatory' => false,
],
'FieldRequired' => [
'mandatory' => true,
],
'FieldMultiple' => [
'mandatory' => false,
'multiple' => true
]
];

public function __construct($admittedFields = null)
{
$this->admittedFields = $admittedFields ?: $this->admittedFields;

parent::__construct();
}

}
45 changes: 45 additions & 0 deletions tests/Unit/Model/Request/SubModel/AbstractModelTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

namespace RatePAY\Tests\Unit\Model\Request\SubModel;

use PHPUnit\Framework\TestCase;
use RatePAY\ModelBuilder;
use RatePAY\Tests\Mocks\SimpleRequestModel;

class AbstractModelTest extends TestCase
{

public function testRemovingWhiteSpaceMethod_set()
{
$baseString = " \t\n\r leading empty\t\r\nspace (%s) \t\n\r ";
$model = new SimpleRequestModel();
$model->__set('FieldDefault', sprintf($baseString, 'FieldDefault'))
->__set('FieldRequired', sprintf($baseString, 'FieldRequired'))
->__set('FieldMultiple', sprintf($baseString, 'FieldMultiple 1'))
->__set('FieldMultiple', sprintf($baseString, 'FieldMultiple 2'));

$baseCorrectedString = "leading empty\t\r\nspace (%s)";
$data = $model->toArray();
self::assertEquals($data['field-default']['value'], sprintf($baseCorrectedString, 'FieldDefault'));
self::assertEquals($data['field-required']['value'], sprintf($baseCorrectedString, 'FieldRequired'));
self::assertEquals($data['field-multiple']['value'][0], sprintf($baseCorrectedString, 'FieldMultiple 1'));
self::assertEquals($data['field-multiple']['value'][1], sprintf($baseCorrectedString, 'FieldMultiple 2'));
}

public function testRemovingWhiteSpaceMethodAnnotationMethods()
{
$baseString = " \t\n\r leading empty\t\r\nspace (%s) \t\n\r ";
$model = new SimpleRequestModel();
$model->setFieldDefault(sprintf($baseString, 'FieldDefault'))
->setFieldRequired(sprintf($baseString, 'FieldRequired'))
->setFieldMultiple(sprintf($baseString, 'FieldMultiple 1'))
->addFieldMultiple(sprintf($baseString, 'FieldMultiple 2'));

$baseCorrectedString = "leading empty\t\r\nspace (%s)";
$data = $model->toArray();
self::assertEquals($data['field-default']['value'], sprintf($baseCorrectedString, 'FieldDefault'));
self::assertEquals($data['field-required']['value'], sprintf($baseCorrectedString, 'FieldRequired'));
self::assertEquals($data['field-multiple']['value'][0], sprintf($baseCorrectedString, 'FieldMultiple 1'));
self::assertEquals($data['field-multiple']['value'][1], sprintf($baseCorrectedString, 'FieldMultiple 2'));
}
}
46 changes: 46 additions & 0 deletions tests/Unit/ModelBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use RatePAY\Model\Request\SubModel\Head;
use RatePAY\Model\Request\SubModel\Head\Meta;
use RatePAY\ModelBuilder;
use RatePAY\Tests\Mocks\SimpleRequestModel;

class ModelBuilderTest extends TestCase
{
Expand Down Expand Up @@ -104,4 +105,49 @@ public function testSetJsonToModelBuilder()
$this->assertInstanceOf('RatePAY\Model\Request\SubModel\Content\ShoppingBasket\Items', $items);
// TODO: check item values...
}

public function testRemovingWhiteSpaceMethodModelBuilderSetter()
{
$baseString = " \t\n\r leading empty\t\r\nspace (%s) \t\n\r ";
$builder = $this->getModelBuilderWithCustomModel(new SimpleRequestModel());
$builder->setFieldDefault(sprintf($baseString, 'FieldDefault'))
->setFieldRequired(sprintf($baseString, 'FieldRequired'))
->setFieldMultiple(sprintf($baseString, 'FieldMultiple 1'))
->setFieldMultiple(sprintf($baseString, 'FieldMultiple 2'));

$baseCorrectedString = "leading empty\t\r\nspace (%s)";
$data = $builder->getModel()->toArray();
self::assertEquals($data['field-default']['value'], sprintf($baseCorrectedString, 'FieldDefault'));
self::assertEquals($data['field-required']['value'], sprintf($baseCorrectedString, 'FieldRequired'));
self::assertEquals($data['field-multiple']['value'][0], sprintf($baseCorrectedString, 'FieldMultiple 1'));
self::assertEquals($data['field-multiple']['value'][1], sprintf($baseCorrectedString, 'FieldMultiple 2'));
}

public function testRemovingWhiteSpaceMethodModelBuilderArray()
{
$baseString = " \t\n\r leading empty\t\r\nspace (%s) \t\n\r ";
$builder = $this->getModelBuilderWithCustomModel(new SimpleRequestModel());
$builder->setArray([
'FieldDefault' => sprintf($baseString, 'FieldDefault'),
'FieldRequired' => sprintf($baseString, 'FieldRequired'),
'FieldMultiple' => sprintf($baseString, 'FieldMultiple 1')
]);

$baseCorrectedString = "leading empty\t\r\nspace (%s)";
$data = $builder->getModel()->toArray();
self::assertEquals($data['field-default']['value'], sprintf($baseCorrectedString, 'FieldDefault'));
self::assertEquals($data['field-required']['value'], sprintf($baseCorrectedString, 'FieldRequired'));
self::assertEquals($data['field-multiple']['value'][0], sprintf($baseCorrectedString, 'FieldMultiple 1'));
}

private function getModelBuilderWithCustomModel($model)
{
$builder = new ModelBuilder();
$refClass = new \ReflectionClass($builder);
$refProp = $refClass->getProperty('model');
$refProp->setAccessible(true);
$refProp->setValue($builder, $model);

return $builder;
}
}

0 comments on commit 35d4fc5

Please sign in to comment.