Skip to content

Commit

Permalink
Merge pull request #245 from CyberiaResurrection/FillMutationHoles
Browse files Browse the repository at this point in the history
Fill mutation holes
  • Loading branch information
CyberiaResurrection committed Jun 1, 2020
2 parents e3d1268 + ae29439 commit e49edb8
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 11 deletions.
2 changes: 0 additions & 2 deletions src/Models/MetadataTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -329,8 +329,6 @@ public function extractGubbins()
$nuField = new EntityField();
$nuField->setName($name);
$nuField->setIsNullable($field['nullable']);
$nuField->setReadOnly(false);
$nuField->setCreateOnly(false);
$nuField->setDefaultValue($field['default']);
$nuField->setIsKeyField($this->getKeyName() == $name);
$nuField->setFieldType(EntityFieldType::PRIMITIVE());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,8 @@ abstract public function morphicType(): string;
*/
protected function checkStringInput($input): bool
{
if (null === $input || !is_string($input) || empty($input)) {
// null inputs are caught by the is_string check
if (!is_string($input) || empty($input)) {
return false;
}
return true;
Expand Down
12 changes: 8 additions & 4 deletions src/Models/ObjectMap/Entities/EntityField.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
use POData\Providers\Metadata\Type\EdmPrimitiveType;
use POData\Providers\Metadata\Type\TypeCode;

/**
* Class EntityField
* @package AlgoWeb\PODataLaravel\Models\ObjectMap\Entities
*/
class EntityField
{
/**
Expand All @@ -22,7 +26,7 @@ class EntityField
/**
* @var bool
*/
private $isNullable;
private $isNullable = false;

/**
* @var mixed
Expand All @@ -32,17 +36,17 @@ class EntityField
/**
* @var bool
*/
private $readOnly;
private $readOnly = false;

/**
* @var bool
*/
private $createOnly;
private $createOnly = false;

/**
* @var bool
*/
private $isKeyField;
private $isKeyField = false;

/**
* @var EntityFieldPrimitiveType
Expand Down
7 changes: 5 additions & 2 deletions src/Query/LaravelWriteQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ protected function createUpdateDeleteProcessInput(array $data, array $paramList,
$varType = isset($spec['type']) ? $spec['type'] : null;
$varName = $spec['name'];
if (null == $varType && null !== $sourceEntityInstance) {
$parms[] = ('id' == $varName) ? $sourceEntityInstance->getKey() : $sourceEntityInstance->{$varName};
$parms[] = $sourceEntityInstance->{$varName};
continue;
}
// TODO: Give this smarts and actively pick up instantiation details
Expand All @@ -60,8 +60,11 @@ protected function createUpdateDeleteProcessInput(array $data, array $paramList,
protected function createUpdateDeleteProcessOutput(JsonResponse $result)
{
$outData = $result->getData(true);
$keys = array_keys($outData);
$expKeys = ['id', 'status', 'errors'];
$isGood = $expKeys === (array_intersect($expKeys, $keys));

if (!(key_exists('id', $outData) && key_exists('status', $outData) && key_exists('errors', $outData))) {
if (!$isGood) {
throw ODataException::createInternalServerError(
'Controller response array missing at least one of id, status and/or errors fields.'
);
Expand Down
31 changes: 31 additions & 0 deletions tests/Orchestra/Unit/Models/ObjectMap/AssociationStubBaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,35 @@ public function testNotCompatibleOnDifferentMorphTypes()
$this->assertFalse($foo->isCompatible($bar));
$this->assertFalse($bar->isCompatible($foo));
}

public function stringInputProvider(): array
{
$result = [];
$result[] = [null, false];
$result[] = [new \stdClass, false];
$result[] = ['', false];
$result[] = ['hiver', true];

return $result;
}

/**
* @dataProvider stringInputProvider
*
* @param $input
* @param bool $expected
* @throws \ReflectionException
*/
public function testCheckStringInput($input, bool $expected)
{
$type = AssociationStubRelationType::NULL_ONE();
$foo = new AssociationStubMonomorphic('', '', [], $type);

$reflec = new \ReflectionClass($foo);
$method = $reflec->getMethod('checkStringInput');
$method->setAccessible(true);

$actual = $method->invokeArgs($foo, [$input]);
$this->assertEquals($expected, $actual);
}
}
48 changes: 48 additions & 0 deletions tests/Orchestra/Unit/Query/LaravelWriteQueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
use AlgoWeb\PODataLaravel\Orchestra\Tests\Models\OrchestraTestModel;
use AlgoWeb\PODataLaravel\Orchestra\Tests\TestCase;
use AlgoWeb\PODataLaravel\Query\LaravelWriteQuery;
use Illuminate\Http\JsonResponse;
use Mockery as m;
use POData\Common\ODataException;
use POData\Providers\Metadata\ResourceSet;
use POData\UriProcessor\ResourcePathProcessor\SegmentParser\KeyDescriptor;

Expand Down Expand Up @@ -68,4 +70,50 @@ public function testDefaultShouldUpdateIsFalseTopLevel()

$foo->updateResource($resourceSet, $model, $keyDesc, $data);
}

public function processOutputProvider(): array
{
$result = [];
$result[] = [ [], false];
$result[] = [ ['id'], false];
$result[] = [ ['status'], false];
$result[] = [ ['errors'], false];
$result[] = [ ['id', 'status'], false];
$result[] = [ ['id', 'errors'], false];
$result[] = [ ['status', 'errors'], false];
$result[] = [ ['id', 'status', 'errors'], true];

return $result;
}

/**
* @dataProvider processOutputProvider
*
* @param array $keys
* @param bool $pass
* @throws \ReflectionException
*/
public function testCreateUpdateDeleteProcessOutput(array $keys, bool $pass)
{
$query = new LaravelWriteQuery();

$reflec = new \ReflectionClass($query);
$method = $reflec->getMethod('createUpdateDeleteProcessOutput');
$method->setAccessible(true);

if (!$pass) {
$this->expectException(ODataException::class);
$this->expectExceptionMessage('Controller response array missing at least one of id, status and/or errors fields.');
}

$data = array_flip($keys);
$response = m::mock(JsonResponse::class)->makePartial();
$response->shouldReceive('getData')->withArgs(['true'])->andReturn($data);

$method->invokeArgs($query, [$response]);

if ($pass) {
$this->assertTrue(true);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use POData\Providers\Metadata\ResourcePropertyKind;
use POData\Providers\Metadata\ResourceType;
use POData\Providers\Metadata\ResourceTypeKind;
use POData\Providers\Metadata\Type\Boolean;
use POData\Providers\Metadata\Type\DateTime;
use POData\Providers\Metadata\Type\StringType;

Expand All @@ -37,9 +38,9 @@ public function testUTF8StringNotMangled()
public function testDateWithNonDateIType()
{
$date = Carbon::create(2019, 1, 1, 0, 0, 0);
$type = new StringType();
$type = new Boolean();

$expected = '2019-01-01 00:00:00';
$expected = 'false';

$actual = SerialiserLowLevelWriters::primitiveToString($type, $date);

Expand Down

0 comments on commit e49edb8

Please sign in to comment.