Skip to content

Commit 7ca752f

Browse files
committed
Merge branch 'ACP2E-1381' of https://github.com/magento-l3/magento2ce into PR-L3-2023-01-31
2 parents 1fb5666 + cc40088 commit 7ca752f

File tree

6 files changed

+190
-3
lines changed

6 files changed

+190
-3
lines changed
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Framework\Api;
9+
10+
use Magento\Framework\Api\Fixture\DataObjectInterface;
11+
use Magento\Framework\Api\Fixture\DataObjectFactory;
12+
use Magento\TestFramework\Helper\Bootstrap;
13+
use PHPUnit\Framework\TestCase;
14+
15+
class DataObjectHelperTest extends TestCase
16+
{
17+
/**
18+
* @var DataObjectHelper
19+
*/
20+
private $dataObjectHelper;
21+
22+
/**
23+
* @var DataObjectFactory
24+
*/
25+
private $dataObjectFactory;
26+
27+
/**
28+
* @inheritdoc
29+
*/
30+
protected function setUp(): void
31+
{
32+
parent::setUp();
33+
$this->dataObjectHelper = Bootstrap::getObjectManager()->get(DataObjectHelper::class);
34+
$this->dataObjectFactory = Bootstrap::getObjectManager()->get(DataObjectFactory::class);
35+
}
36+
37+
/**
38+
* Test object is populated with data from array.
39+
*
40+
* @return void
41+
*/
42+
public function testPopulateWithArray(): void
43+
{
44+
$inputArray = [
45+
'first_a_second' => '1',
46+
'first_at_second' => '1',
47+
'first_a_t_m_second' => '1',
48+
'random_attribute' => 'random'
49+
];
50+
$expectedData = [
51+
'first_a_second' => '1',
52+
'first_at_second' => '1',
53+
'first_a_t_m_second' => '1',
54+
];
55+
$object = $this->dataObjectFactory->create();
56+
$this->dataObjectHelper->populateWithArray($object, $inputArray, DataObjectInterface::class);
57+
$this->assertEquals($expectedData, $object->getData());
58+
}
59+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Framework\Api\Fixture;
9+
10+
use Magento\Framework\Model\AbstractExtensibleModel;
11+
12+
class DataObject extends AbstractExtensibleModel implements DataObjectInterface
13+
{
14+
/**
15+
* @inheritDoc
16+
*/
17+
public function setFirstASecond(string $value): void
18+
{
19+
$this->setData('first_a_second', $value);
20+
}
21+
22+
/**
23+
* @inheritDoc
24+
*/
25+
public function setFirstAtSecond(string $value): void
26+
{
27+
$this->setData('first_at_second', $value);
28+
}
29+
30+
/**
31+
* @inheritDoc
32+
*/
33+
public function setFirstATMSecond(string$value): void
34+
{
35+
$this->setData('first_a_t_m_second', $value);
36+
}
37+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Framework\Api\Fixture;
9+
10+
interface DataObjectInterface
11+
{
12+
/**
13+
* Set A
14+
*
15+
* @param string $value
16+
*
17+
* @return void
18+
*/
19+
public function setFirstASecond(string $value): void;
20+
21+
/**
22+
* Set At
23+
*
24+
* @param string $value
25+
*
26+
* @return void
27+
*/
28+
public function setFirstAtSecond(string $value): void;
29+
30+
/**
31+
* Set ATM
32+
*
33+
* @param string $value
34+
*
35+
* @return void
36+
*/
37+
public function setFirstATMSecond(string $value): void;
38+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Framework\Api;
9+
10+
use Magento\TestFramework\Helper\Bootstrap;
11+
use PHPUnit\Framework\TestCase;
12+
13+
class SimpleDataObjectConverterTest extends TestCase
14+
{
15+
/**
16+
* @var SimpleDataObjectConverter
17+
*/
18+
private $simpleDataObjectConverter;
19+
20+
/**
21+
* @inheritdoc
22+
*/
23+
protected function setUp(): void
24+
{
25+
parent::setUp();
26+
$this->simpleDataObjectConverter = Bootstrap::getObjectManager()->get(SimpleDataObjectConverter::class);
27+
}
28+
29+
/**
30+
* Test snake case to camel case conversion and vice versa.
31+
*
32+
* @return void
33+
*/
34+
public function testCaseConversion(): void
35+
{
36+
$snakeCaseToCamelCase = [
37+
'first_a_second' => 'firstASecond',
38+
'first_at_second' => 'firstAtSecond',
39+
'first_a_t_m_second' => 'firstATMSecond',
40+
];
41+
42+
foreach ($snakeCaseToCamelCase as $snakeCase => $camelCase) {
43+
$this->assertEquals(
44+
$camelCase,
45+
$this->simpleDataObjectConverter->snakeCaseToCamelCase($snakeCase)
46+
);
47+
$this->assertEquals(
48+
$snakeCase,
49+
$this->simpleDataObjectConverter->camelCaseToSnakeCase($camelCase)
50+
);
51+
}
52+
}
53+
}

lib/internal/Magento/Framework/Api/DataObjectHelper.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -305,8 +305,8 @@ private function getSetters(object $dataObject): array
305305
// (2) remove set_ in start of name
306306
// (3) add name without is_ prefix
307307
preg_replace(
308-
['/(^|,)(?!set)[^,]*/S','/(.)([A-Z])/S', '/(^|,)set_/iS', '/(^|,)is_([^,]+)/is'],
309-
['', '$1_$2', '$1', '$1$2,is_$2'],
308+
['/(^|,)(?!set)[^,]*/S','/([A-Z])/S', '/(^|,)set_/iS', '/(^|,)is_([^,]+)/is'],
309+
['', '_$1', '$1', '$1$2,is_$2'],
310310
implode(',', $dataObjectMethods)
311311
)
312312
)

lib/internal/Magento/Framework/Api/SimpleDataObjectConverter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,6 @@ public static function snakeCaseToCamelCase($input)
172172
*/
173173
public static function camelCaseToSnakeCase($name)
174174
{
175-
return $name !== null ? strtolower(preg_replace('/(.)([A-Z])/', "$1_$2", $name)) : '';
175+
return $name !== null ? strtolower(ltrim(preg_replace('/([A-Z])/m', "_$1", $name), '_')) : '';
176176
}
177177
}

0 commit comments

Comments
 (0)