Skip to content

Commit

Permalink
Updated Varien_Object::getData() and added getDataByKey() & `getD…
Browse files Browse the repository at this point in the history
…ataByPath()` (#18)

* Add test for Varien_Object ... coverage 100%

* Add test for Varien_Object ... coverage 100% (2)

* Updated getData(), see OpenMage#3245

* Fixed path in sonar test

* Fixed path in sonar test

* Fixed typos

* Sonar fixes

* Fixed path in sonar test

* Fixed path in sonar test
  • Loading branch information
sreichel committed Feb 19, 2024
1 parent 0fe2d71 commit 15eb83f
Show file tree
Hide file tree
Showing 4 changed files with 303 additions and 84 deletions.
14 changes: 7 additions & 7 deletions .github/workflows/sonar.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
max-parallel: 5
matrix:
os: [ubuntu-latest]
php: ['7.4', '8.1']
php: ['7.4', '8.3']
steps:
- uses: actions/checkout@v4
- name: Setup PHP
Expand Down Expand Up @@ -49,20 +49,20 @@ jobs:

- name: prepare SonarCloud Scan Data
continue-on-error: true
if: ${{ matrix.php == '8.1' }}
if: ${{ matrix.php == '8.3' }}
run: |
echo $PWD
ls -la
head ./tests/reports/clover-sonar.xml
sed -i 's@'$GITHUB_WORKSPACE'/@/github/workspace/@g' ./dev/tests/logs/junit-sonar.xml
sed -i 's@'$GITHUB_WORKSPACE'/@/github/workspace/@g' ./dev/tests/reports/clover-sonar.xml
head ./tests/reports/clover-sonar.xml
head ./dev/tests/reports/clover.xml
sed -i 's@'$GITHUB_WORKSPACE'/@/github/workspace/@g' ./dev/tests/logs/junit.xml
sed -i 's@'$GITHUB_WORKSPACE'/@/github/workspace/@g' ./dev/tests/reports/clover.xml
head ./dev/tests/reports/clover.xml
ls -la
- name: SonarCloud Scan
uses: SonarSource/sonarcloud-github-action@master
continue-on-error: true
if: ${{ matrix.php == '8.1' }} && SONAR_TOKEN
if: ${{ matrix.php == '8.3' }} && SONAR_TOKEN
with:
args: >
-Dproject.settings=tests/sonar-project.properties
Expand Down
10 changes: 5 additions & 5 deletions dev/sonar-project.properties
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
sonar.projectKey=openmage-lts
sonar.organization=openmage
sonar.projectKey=openmage-future
sonar.organization=sreichel

# This is the name and version displayed in the SonarCloud UI.
#sonar.projectName=openmage-lts
#sonar.projectName=openmage-future
#sonar.projectVersion=1.0

# Path is relative to the sonar-project.properties file. Replace "\" by "/" on Windows.
Expand All @@ -13,5 +13,5 @@ sonar.tests=./dev/tests/
sonar.sourceEncoding=UTF-8

sonar.language=php
sonar.php.tests.reportPath=./dev/tests/logs/junit-sonar.xml
sonar.php.coverage.reportPaths=./dev/tests/reports/clover-sonar.xml
sonar.php.tests.reportPath=./dev/tests/logs/junit.xml
sonar.php.coverage.reportPaths=./dev/tests/reports/clover.xml
227 changes: 227 additions & 0 deletions dev/tests/unit/Varien/ObjectTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,227 @@
<?php
declare(strict_types=1);

namespace unit\Varien;

use StdClass;
use Varien_Exception;
use Varien_Object;
use PHPUnit\Framework\TestCase;

class ObjectTest extends TestCase
{
/**
* @var Varien_Object
*/
private Varien_Object $subject;

public function setUp(): void
{
$this->subject = new Varien_Object();
}

/**
* @dataProvider provideGetDataData
* @param mixed $expectedResult
* @param string|array $setKey
* @param mixed $setValue
* @param string|array $key
* @param string|int|null $index
* @return void
*/
public function testGetData($expectedResult, $setKey, $setValue, string $key, $index = null): void
{
$this->subject->setData($setKey, $setValue);
self::assertEquals($expectedResult, $this->subject->getData($key, $index));
}

/**
* @return string[][]
*/
public function provideGetDataData(): array
{
return [
'empty_key' => [
['empty_key' => ['empty_value']],
'empty_key',
['empty_value'],
''
],
'string' => [
'value',
'string',
'value',
'string'
],
'int' => [
1,
'int',
1,
'int'
],
'numeric' => [
'1',
'numeric',
'1',
'numeric'
],
'array' => [
['string', 1],
'array',
['string', 1],
'array',
],
'array_index_int' => [
'string',
'array_index_int',
['string', 1],
'array_index_int',
0,
],
'array_index_int_invalid' => [
null,
'array_index_int_invalid',
['string', 1],
'array_index_int_invalid',
999,
],
'array_index_string' => [
1,
'array_index_string',
['string' => 'string', 'int' => 1],
'array_index_string',
'int',
],
'array_index_string_string' => [
null,
'array_index_string_string',
'some_string',
'array_index_string_string',
'not-exists',
],
'array_index_string_varien_object' => [
[],
'array_index_string_varien_object',
new Varien_Object(['array' => []]),
'array_index_string_varien_object',
'array',
],
'array_index_string_std_class' => [
null,
'array_index_string_std_class',
new StdClass(),
'array_index_string_std_class',
'not-exists',
],
'array_nested' => [
1,
'array_nested',
['nested' => ['string' => 'string', 'int' => 1]],
'array_nested/nested/int',
],
'array_nested_invalid_key' => [
null,
'array_nested',
['nested' => ['string' => 'string', 'int' => 1]],
'array_nested/nested/invalid_key',
],
'array_nested_empty_key' => [
null,
'array_nested',
['nested' => ['string' => 'string', 'int' => '']],
'array_nested/nested/',
],
'array_nested_string' => [
'some"\n"string',
'array_nested_string',
['nested' => 'some"\n"string'],
'array_nested_string/nested',
],
'array_nested_varien_object' => [
null,
'array_nested_varien_object',
new Varien_Object(),
'array_nested_varien_object/nested',
],
'array_nested_std_class' => [
null,
'array_nested_std_class',
new StdClass(),
'array_nested_std_class/nested',
],
'array_nested_key_not_exists' => [
null,
'array_nested_key_not_exists',
['nested' => ['string' => 'string', 'int' => 1]],
'array_nested_key_not_exists_test/nested/int',
],
];
}

public function testGetSetUnsData(): void
{
self::assertTrue($this->subject->isEmpty());
$this->subject->setABC('abc');
$this->subject->setData('efg', 'efg');
$this->subject->set123('123');
$this->subject->setData('345', '345');
$this->subject->setKeyAFirst('value_a_first');
$this->subject->setData('key_a_2nd', 'value_a_2nd');
$this->subject->setKeyA3rd('value_a_3rd');
$this->subject->setData('left', 'over');
self::assertFalse($this->subject->isEmpty());

self::assertSame('abc', $this->subject->getData('a_b_c'));
self::assertSame('abc', $this->subject->getABC());
$this->subject->unsetData('a_b_c');

self::assertSame('efg', $this->subject->getData('efg'));
self::assertSame('efg', $this->subject->getEfg());
$this->subject->unsEfg();

self::assertSame('123', $this->subject->getData('123'));
self::assertSame('123', $this->subject->get123());
$this->subject->uns123();

$this->subject->unsetData('345');

self::assertSame('value_a_first', $this->subject->getData('key_a_first'));
self::assertSame('value_a_first', $this->subject->getKeyAFirst());
$this->subject->unsetData('key_a_first');

self::assertSame('value_a_2nd', $this->subject->getData('key_a_2nd'));
self::assertSame('value_a_2nd', $this->subject->getKeyA_2nd());
$this->subject->unsetData('key_a_2nd');

self::assertSame('value_a_3rd', $this->subject->getData('key_a3rd'));
self::assertSame('value_a_3rd', $this->subject->getKeyA3rd());
$this->subject->unsetData('key_a3rd');

self::assertSame(['left' => 'over'], $this->subject->getData());

$this->subject->unsetData();
self::assertSame([], $this->subject->getData());
self::assertTrue($this->subject->isEmpty());

try {
$this->subject->notData();
self::fail('Invalid __call');
} catch (Varien_Exception $exception) {
self::assertStringStartsWith('Invalid method', $exception->getMessage());
}

}

public function testOffset(): void
{
self::assertFalse($this->subject->offsetExists('off'));

$this->subject->offsetSet('off', 'set');
self::assertTrue($this->subject->offsetExists('off'));
self::assertSame('set', $this->subject->offsetGet('off'));
self::assertSame(null, $this->subject->offsetGet('not-exists'));

$this->subject->offsetUnset('off');
self::assertFalse($this->subject->offsetExists('off'));
}
}
Loading

0 comments on commit 15eb83f

Please sign in to comment.