Skip to content

Commit

Permalink
test: update existing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kenjis committed Sep 30, 2023
1 parent c2bdf60 commit 9995e42
Showing 1 changed file with 69 additions and 46 deletions.
115 changes: 69 additions & 46 deletions tests/system/Entity/EntityTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public function testSetArrayToPropertyNamedAttributes()
2 => 3,
],
];
$this->assertSame($expected, $entity->toRawArray());
$this->assertSame($expected, $entity->toDatabase());
}

public function testSimpleSetAndGet(): void
Expand Down Expand Up @@ -347,23 +347,31 @@ public function testCastIntBool(): void
];
};

$entity->setAttributes(['active' => '1']);
$entity->active = '1';

$this->assertTrue($entity->active);

$entity->setAttributes(['active' => '0']);
$entity->active = '0';

$this->assertFalse($entity->active);

$entity->active = 1;

$this->assertTrue($entity->active);

$entity->active = 0;

$this->assertFalse($entity->active);

$entity->active = true;

$this->assertTrue($entity->active);
$this->assertSame(['active' => 1], $entity->toRawArray());
$this->assertSame(['active' => 1], $entity->toDatabase());

$entity->active = false;

$this->assertFalse($entity->active);
$this->assertSame(['active' => 0], $entity->toRawArray());
$this->assertSame(['active' => 0], $entity->toDatabase());
}

public function testCastFloat(): void
Expand Down Expand Up @@ -423,11 +431,12 @@ public function testCastBoolean(): void

public function testCastCSV(): void
{
$entity = $this->getCastEntity();
$entity = $this->getCastEntity();

$data = ['foo', 'bar', 'bam'];
$entity->twelfth = $data;

$result = $entity->toRawArray();
$result = $entity->toDatabase();

$this->assertIsString($result['twelfth']);
$this->assertSame('foo,bar,bam', $result['twelfth']);
Expand Down Expand Up @@ -486,8 +495,9 @@ public function testCastArray(): void

$entity->seventh = ['foo' => 'bar'];

$check = $this->getPrivateProperty($entity, 'attributes')['seventh'];
$this->assertSame(serialize(['foo' => 'bar']), $check);
$result = $entity->toDatabase();

$this->assertSame(serialize(['foo' => 'bar']), $result['seventh']);
$this->assertSame(['foo' => 'bar'], $entity->seventh);
}

Expand All @@ -497,11 +507,12 @@ public function testCastArrayByStringSerialize(): void

$entity->seventh = 'foobar';

$result = $entity->toDatabase();

// Should be a serialized string now...
$check = $this->getPrivateProperty($entity, 'attributes')['seventh'];
$this->assertSame(serialize('foobar'), $check);
$this->assertSame(serialize('foobar'), $result['seventh']);

$this->assertSame(['foobar'], $entity->seventh);
$this->assertSame('foobar', $entity->seventh);
}

public function testCastArrayByArraySerialize(): void
Expand All @@ -510,9 +521,10 @@ public function testCastArrayByArraySerialize(): void

$entity->seventh = ['foo' => 'bar'];

$result = $entity->toDatabase();

// Should be a serialized string now...
$check = $this->getPrivateProperty($entity, 'attributes')['seventh'];
$this->assertSame(serialize(['foo' => 'bar']), $check);
$this->assertSame(serialize(['foo' => 'bar']), $result['seventh']);

$this->assertSame(['foo' => 'bar'], $entity->seventh);
}
Expand All @@ -524,9 +536,10 @@ public function testCastArrayByFill(): void
$data = ['seventh' => [1, 2, 3]];
$entity->fill($data);

$result = $entity->toDatabase();

// Check if serialiazed
$check = $this->getPrivateProperty($entity, 'attributes')['seventh'];
$this->assertSame(serialize([1, 2, 3]), $check);
$this->assertSame(serialize([1, 2, 3]), $result['seventh']);
// Check if unserialized
$this->assertSame([1, 2, 3], $entity->seventh);
}
Expand All @@ -536,9 +549,10 @@ public function testCastArrayByConstructor(): void
$data = ['seventh' => [1, 2, 3]];
$entity = $this->getCastEntity($data);

$result = $entity->toDatabase();

// Check if serialiazed
$check = $this->getPrivateProperty($entity, 'attributes')['seventh'];
$this->assertSame(serialize([1, 2, 3]), $check);
$this->assertSame(serialize([1, 2, 3]), $result['seventh']);
// Check if unserialized
$this->assertSame([1, 2, 3], $entity->seventh);
}
Expand Down Expand Up @@ -584,12 +598,12 @@ public function testCastAsJSON(): void

$entity->tenth = ['foo' => 'bar'];

$result = $entity->toDatabase();

// Should be a JSON-encoded string now...
$check = $this->getPrivateProperty($entity, 'attributes')['tenth'];
$this->assertSame('{"foo":"bar"}', $check);
$this->assertSame('{"foo":"bar"}', $result['tenth']);

$this->assertInstanceOf('stdClass', $entity->tenth);
$this->assertSame(['foo' => 'bar'], (array) $entity->tenth);
$this->assertSame(['foo' => 'bar'], $entity->tenth);
}

public function testCastAsJSONArray(): void
Expand All @@ -599,9 +613,10 @@ public function testCastAsJSONArray(): void
$data = ['Sun', 'Mon', 'Tue'];
$entity->eleventh = $data;

$result = $entity->toDatabase();

// Should be a JSON-encoded string now...
$check = $this->getPrivateProperty($entity, 'attributes')['eleventh'];
$this->assertSame('["Sun","Mon","Tue"]', $check);
$this->assertSame('["Sun","Mon","Tue"]', $result['eleventh']);

$this->assertSame($data, $entity->eleventh);
}
Expand All @@ -613,9 +628,10 @@ public function testCastAsJsonByFill(): void
$data = ['eleventh' => [1, 2, 3]];
$entity->fill($data);

$result = $entity->toDatabase();

// Check if serialiazed
$check = $this->getPrivateProperty($entity, 'attributes')['eleventh'];
$this->assertSame(json_encode([1, 2, 3]), $check);
$this->assertSame(json_encode([1, 2, 3]), $result['eleventh']);
// Check if unserialized
$this->assertSame([1, 2, 3], $entity->eleventh);
}
Expand All @@ -625,9 +641,10 @@ public function testCastAsJsonByConstructor(): void
$data = ['eleventh' => [1, 2, 3]];
$entity = $this->getCastEntity($data);

$result = $entity->toDatabase();

// Check if serialiazed
$check = $this->getPrivateProperty($entity, 'attributes')['eleventh'];
$this->assertSame(json_encode([1, 2, 3]), $check);
$this->assertSame(json_encode([1, 2, 3]), $result['eleventh']);
// Check if unserialized
$this->assertSame([1, 2, 3], $entity->eleventh);
}
Expand All @@ -651,6 +668,8 @@ public function testCastAsJSONErrorDepth(): void
}
$current = $value;
$entity->tenth = $array;

$entity->toDatabase();
}

public function testCastAsJSONErrorUTF8(): void
Expand All @@ -661,6 +680,8 @@ public function testCastAsJSONErrorUTF8(): void
$entity = $this->getCastEntity();

$entity->tenth = "\xB1\x31";

$entity->toDatabase();
}

/**
Expand All @@ -675,7 +696,7 @@ public function testCastAsJSONSyntaxError(): void
$entity = new Entity();
$entity->casts['dummy'] = 'json[array]';

return $entity->castAs($value, 'dummy');
return $entity->castAs($value, 'dummy', 'fromDatabase');
}, null, Entity::class))('{ this is bad string');
}

Expand All @@ -692,7 +713,7 @@ public function testCastAsJSONAnotherErrorDepth(): void
$entity = new Entity();
$entity->casts['dummy'] = 'json[array]';

return $entity->castAs($value, 'dummy');
return $entity->castAs($value, 'dummy', 'fromDatabase');
}, null, Entity::class))($string);
}

Expand All @@ -709,7 +730,7 @@ public function testCastAsJSONControlCharCheck(): void
$entity = new Entity();
$entity->casts['dummy'] = 'json[array]';

return $entity->castAs($value, 'dummy');
return $entity->castAs($value, 'dummy', 'fromDatabase');
}, null, Entity::class))($string);
}

Expand All @@ -726,22 +747,24 @@ public function testCastAsJSONStateMismatch(): void
$entity = new Entity();
$entity->casts['dummy'] = 'json[array]';

return $entity->castAs($value, 'dummy');
return $entity->castAs($value, 'dummy', 'fromDatabase');
}, null, Entity::class))($string);
}

public function testCastSetter(): void
{
$string = '321 String with numbers 123';
$entity = $this->getCastEntity();
$entity->first = $string;
$entity = $this->getCastEntity();

$entity->cast(false);
$string = '321 String with numbers 123';
$entity->first = $string;

$this->assertIsString($entity->first);
$this->assertSame($string, $entity->first);

$entity->cast(true);
$string = '321 String with numbers 123';
$entity->first = $string;

$this->assertIsInt($entity->first);
$this->assertSame((int) $string, $entity->first);
Expand Down Expand Up @@ -863,7 +886,7 @@ public function testDataMappingIssetSwapped(): void
$this->assertTrue($isset);
$this->assertSame('222', $entity->bar);

$result = $entity->toRawArray();
$result = $entity->toDatabase();

$this->assertSame([
'foo' => '222',
Expand Down Expand Up @@ -916,11 +939,11 @@ public function testAsArrayOnlyChanged(): void
], $result);
}

public function testToRawArray(): void
public function testToDatabase(): void
{
$entity = $this->getEntity();

$result = $entity->toRawArray();
$result = $entity->toDatabase();

$this->assertSame([
'foo' => null,
Expand All @@ -930,12 +953,12 @@ public function testToRawArray(): void
], $result);
}

public function testToRawArrayRecursive(): void
public function testToDatabaseRecursive(): void
{
$entity = $this->getEntity();
$entity->entity = $this->getEntity();

$result = $entity->toRawArray(false, true);
$result = $entity->toDatabase(false, true);

$this->assertSame([
'foo' => null,
Expand All @@ -951,12 +974,12 @@ public function testToRawArrayRecursive(): void
], $result);
}

public function testToRawArrayOnlyChanged(): void
public function testToDatabaseOnlyChanged(): void
{
$entity = $this->getEntity();
$entity->bar = 'foo';

$result = $entity->toRawArray(true);
$result = $entity->toDatabase(true);

$this->assertSame([
'bar' => 'bar:foo',
Expand Down Expand Up @@ -1285,16 +1308,16 @@ protected function getCastNullableEntity()
return new class () extends Entity {
protected $attributes = [
'string_null' => null,
'string_empty' => null,
'string_empty' => '',
'integer_null' => null,
'integer_0' => null,
'integer_0' => 0,
'string_value_not_null' => 'value',
];
protected $_original = [
'string_null' => null,
'string_empty' => null,
'string_empty' => '',
'integer_null' => null,
'integer_0' => null,
'integer_0' => 0,
'string_value_not_null' => 'value',
];

Expand Down

0 comments on commit 9995e42

Please sign in to comment.