Skip to content

Commit

Permalink
Add build and update in ForceExecutionHelper
Browse files Browse the repository at this point in the history
  • Loading branch information
NicolasGuilloux committed Jul 28, 2021
1 parent d22d3d6 commit 132fc75
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 6 deletions.
15 changes: 15 additions & 0 deletions .phpstorm.meta.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace PHPSTORM_META;

use RichCongress\TestTools\Helper\ForceExecutionHelper;

override(
ForceExecutionHelper::build(0),
type(0)
);

override(
ForceExecutionHelper::update(0),
type(0)
);
32 changes: 30 additions & 2 deletions Helper/ForceExecutionHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,37 @@ private function __construct()
// Block the instanciation
}

/**
* @param object|class-string<object> $class
* @param array<string, mixed> $data
*
* @return mixed|object
*/
public static function build(string $class, array $data)
{
$object = new $class();

return self::update($object, $data);
}

/**
* @param object|class-string<object> $object
* @param array<string, mixed> $data
*
* @return mixed|object
*/
public static function update($object, array $data)
{
foreach ($data as $key => $value) {
self::setValue($object, $key, $value);
}

return $object;
}

/**
* @param object|class-string<object> $object
* @param array|mixed $args
* @param array|mixed $args
*
* @return mixed
*/
Expand All @@ -42,7 +70,7 @@ public static function executeMethod($object, string $method, ...$args)

/**
* @param object|class-string<object> $object
* @param mixed $value
* @param mixed $value
*/
public static function setValue($object, string $propertyName, $value): void
{
Expand Down
45 changes: 41 additions & 4 deletions Tests/Helper/ForceExecutionHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,16 @@
* @author Nicolas Guilloux <[email protected]>
* @copyright 2014 - 2020 RichCongress (https://www.richcongress.com)
*
* @covers \RichCongress\TestTools\Helper\ForceExecutionHelper
* @covers \RichCongress\TestTools\Helper\ForceExecutionHelper
*/
class ForceExecutionHelperTest extends TestCase
{
public function testCannotInstanciateHelper(): void
{
$this->expectException(\Error::class);
$this->expectExceptionMessage('Call to private RichCongress\TestTools\Helper\ForceExecutionHelper::__construct()');
$this->expectExceptionMessage(
'Call to private RichCongress\TestTools\Helper\ForceExecutionHelper::__construct()'
);

new ForceExecutionHelper();
}
Expand All @@ -37,7 +39,9 @@ public function testExecuteMethod(): void
public function testExecuteMethodWithBadObject(): void
{
$this->expectException(\LogicException::class);
$this->expectExceptionMessage('Cannot call a non static method without the instanciated object for ' . DummyEntity::class);
$this->expectExceptionMessage(
'Cannot call a non static method without the instanciated object for ' . DummyEntity::class
);

ForceExecutionHelper::executeMethod(DummyEntity::class, 'setName', 'ThisIsATest');
}
Expand Down Expand Up @@ -68,7 +72,9 @@ public function testSetValue(): void
public function testSetValueWithBadObject(): void
{
$this->expectException(\LogicException::class);
$this->expectExceptionMessage('Cannot call a non static property without the instanciated object for ' . DummyEntity::class);
$this->expectExceptionMessage(
'Cannot call a non static property without the instanciated object for ' . DummyEntity::class
);

ForceExecutionHelper::setValue(DummyEntity::class, 'name', 'ThisIsATest');
}
Expand All @@ -95,4 +101,35 @@ public function testWithBadObjectType(): void

ForceExecutionHelper::setValue(0.1, 'staticName', 'ThisIsATest');
}

public function testBuild(): void
{
$object = ForceExecutionHelper::build(
DummyEntity::class,
[
'name' => 'ThisIsATest',
'keyname' => 'KeyName',
]
);

self::assertInstanceOf(DummyEntity::class, $object);
self::assertSame('ThisIsATest', $object->getName());
self::assertSame('KeyName', $object->getKeyname());
}

public function testUpdate(): void
{
$object = new DummyEntity();
$result = ForceExecutionHelper::update(
$object,
[
'name' => 'ThisIsATest',
'keyname' => 'KeyName',
]
);

self::assertSame($object, $result);
self::assertSame('ThisIsATest', $object->getName());
self::assertSame('KeyName', $object->getKeyname());
}
}

0 comments on commit 132fc75

Please sign in to comment.