Skip to content

Commit

Permalink
test improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
frasmage committed Apr 18, 2024
1 parent 4b165f9 commit b468161
Show file tree
Hide file tree
Showing 7 changed files with 203 additions and 87 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/automated-test.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: PHPUnit Tests
on: [push, pull_request]
on: [push]
jobs:
phpunit:
runs-on: ubuntu-latest
Expand Down
5 changes: 2 additions & 3 deletions src/Commands/RefreshMeta.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public function handle(): void
while ($count < $total) {
$query = Meta::query()
->orderBy('id')
->limit(100);
->limit(config('metable.refreshPageSize', 100));
if ($lastId) {
$query->where('id', '>', $lastId);
}
Expand All @@ -48,5 +48,4 @@ public function handle(): void

$this->info('Refresh complete.');
}

}
}
2 changes: 2 additions & 0 deletions src/DataType/ModelCollectionHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ public function unserializeValue(string $serializedValue): mixed

$models = $this->loadModels($data['items']);


// Repopulate collection keys with loaded models.
foreach ($data['items'] as $key => $item) {
if (empty($item['key'])) {
Expand Down Expand Up @@ -107,6 +108,7 @@ private function loadModels(array $items)
if (!class_exists($class)
|| !is_a($class, Model::class, true)
) {

continue;
}

Expand Down
5 changes: 3 additions & 2 deletions tests/Integration/Commands/RefreshMetaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ public function test_it_refreshes_all_meta_values(): void
]);
config()->set('metable.indexComplexDataTypes', true);

config()->set('metable.refreshPageSize', 2);

$complexValue = ['a' => 'b'];

DB::table('meta')->insert([
Expand Down Expand Up @@ -79,5 +81,4 @@ public function test_it_refreshes_all_meta_values(): void
$this->assertEquals('2020-01-01 00:00:00.000000+0000', $result[2]->string_value);
$this->assertEquals(1577836800, $result[2]->numeric_value);
}

}
}
201 changes: 122 additions & 79 deletions tests/Integration/DataType/HandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,108 +41,144 @@ public static function handlerProvider(): array

return [
'array' => [
new ArrayHandler(),
'array',
['foo' => ['bar'], 'baz'],
[new stdClass()],
null,
null,
'handler' => new ArrayHandler(),
'type' => 'array',
'value' => ['foo' => ['bar'], 'baz'],
'invalid' => [new stdClass()],
'numericValue' => null,
'stringValue' => null,
'stringValueComplex' => json_encode(['foo' => ['bar'], 'baz']),
'isIdempotent' => true,
],
'boolean' => [
new BooleanHandler(),
'boolean',
true,
[1, 0, '', [], null],
1,
'true'
'handler' => new BooleanHandler(),
'type' => 'boolean',
'value' => true,
'invalid' => [1, 0, '', [], null],
'numericValue' => 1,
'stringValue' => 'true',
'stringValueComplex' => 'true',
'isIdempotent' => true,
],
'datetime' => [
new DateTimeHandler(),
'datetime',
$datetime,
[2017, '2017-01-01'],
$timestamp,
$dateString,
'handler' => new DateTimeHandler(),
'type' => 'datetime',
'value' => $datetime,
'invalid' => [2017, '2017-01-01'],
'numericValue' => $timestamp,
'stringValue' => $dateString,
'stringValueComplex' => $dateString,
'isIdempotent' => true,
],
'float' => [
new FloatHandler(),
'float',
1.1,
['1.1', 1],
1.1,
'1.1',
'handler' => new FloatHandler(),
'type' => 'float',
'value' => 1.1,
'invalid' => ['1.1', 1],
'numericValue' => 1.1,
'stringValue' => '1.1',
'stringValueComplex' => '1.1',
'isIdempotent' => true,
],
'integer' => [
new IntegerHandler(),
'integer',
3,
[1.1, '1'],
3,
'3',
'handler' => new IntegerHandler(),
'type' => 'integer',
'value' => 3,
'invalid' => [1.1, '1'],
'numericValue' => 3,
'stringValue' => '3',
'stringValueComplex' => '3',
'isIdempotent' => true,
],
'model' => [
new ModelHandler(),
'model',
$model,
[new stdClass()],
null,
SampleMetable::class,
'handler' => new ModelHandler(),
'type' => 'model',
'value' => $model,
'invalid' => [new stdClass()],
'numericValue' => null,
'stringValue' => SampleMetable::class,
'stringValueComplex' => SampleMetable::class,
'isIdempotent' => true,
],
'model collection' => [
new ModelCollectionHandler(),
'collection',
new Collection([new SampleMetable()]),
[collect()],
null,
null,
'handler' => new ModelCollectionHandler(),
'type' => 'collection',
'value' => new Collection([new SampleMetable()]),
'invalid' => [collect()],
'numericValue' => null,
'stringValue' => null,
'stringValueComplex' => null,
'isIdempotent' => true,
],
'null' => [
new NullHandler(),
'null',
null,
[0, '', 'null', [], false],
null,
null,
'handler' => new NullHandler(),
'type' => 'null',
'value' => null,
'invalid' => [0, '', 'null', [], false],
'numericValue' => null,
'stringValue' => null,
'stringValueComplex' => null,
'isIdempotent' => true,
],
'object' => [
new ObjectHandler(),
'object',
$object,
[[]],
null,
null,
'handler' => new ObjectHandler(),
'type' => 'object',
'value' => $object,
'invalid' => [[]],
'numericValue' => null,
'stringValue' => null,
'stringValueComplex' => json_encode($object),
'isIdempotent' => true,
],
'serialize' => [
new SerializeHandler(),
'serialized',
['foo' => 'bar', 'baz' => [3]],
[self::$resource],
null,
null,
'handler' => new SerializeHandler(),
'type' => 'serialized',
'value' => ['foo' => 'bar', 'baz' => [3]],
'invalid' => [self::$resource],
'numericValue' => null,
'stringValue' => null,
'stringValueComplex' => serialize(['foo' => 'bar', 'baz' => [3]]),
'isIdempotent' => false,
],
'serializable' => [
new SerializableHandler(),
'serializable',
new SampleSerializable(['foo' => 'bar']),
[],
null,
null,
'handler' => new SerializableHandler(),
'type' => 'serializable',
'value' => new SampleSerializable(['foo' => 'bar']),
'invalid' => [],
'numericValue' => null,
'stringValue' => null,
'stringValueComplex' => serialize(new SampleSerializable(['foo' => 'bar'])),
'isIdempotent' => true,
],
'string' => [
new StringHandler(),
'string',
'foo',
[1, 1.1],
null,
'foo',
'handler' => new StringHandler(),
'type' => 'string',
'value' => 'foo',
'invalid' => [1, 1.1],
'numericValue' => null,
'stringValue' => 'foo',
'stringValueComplex' => 'foo',
'isIdempotent' => true,
],
'long-string' => [
'handler' => new StringHandler(),
'type' => 'string',
'value' => str_repeat('a', 300),
'invalid' => [1, 1.1],
'numericValue' => null,
'stringValue' => str_repeat('a', 255),
'stringValueComplex' => str_repeat('a', 255),
'isIdempotent' => true,
],
'numeric-string' => [
new StringHandler(),
'string',
'1.2345',
[1, 1.1],
1.2345,
'1.2345',
'handler' => new StringHandler(),
'type' => 'string',
'value' => '1.2345',
'invalid' => [1, 1.1],
'numericValue' => 1.2345,
'stringValue' => '1.2345',
'stringValueComplex' => '1.2345',
'isIdempotent' => true,
],
];
}
Expand All @@ -165,7 +201,9 @@ public function test_it_can_verify_and_serialize_data(
mixed $value,
array $incompatible,
null|int|float $numericValue,
null|string $stringValue
null|string $stringValue,
null|string $stringValueComplex,
bool $isIdempotent
): void {
$this->assertEquals($type, $handler->getDataType());
$this->assertTrue($handler->canHandleValue($value));
Expand All @@ -179,6 +217,11 @@ public function test_it_can_verify_and_serialize_data(

$this->assertEquals($value, $unserialized);
$this->assertEquals($numericValue, $handler->getNumericValue($value));
config()->set('metable.indexComplexDataTypes', false);
$this->assertEquals($stringValue, $handler->getStringValue($value));
config()->set('metable.indexComplexDataTypes', true);
$this->assertEquals($stringValueComplex, $handler->getStringValue($value));

$this->assertEquals($isIdempotent, $handler->isIdempotent());
}
}
37 changes: 35 additions & 2 deletions tests/Integration/DataType/ModelCollectionHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,29 @@ public function test_it_reloads_model_instances(): void
$this->assertEquals(1, $unserialized['foo']->getKey());
}

public function test_it_handles_invalid_model_class(): void
public function test_it_handles_invalid_collection_class(): void
{
$this->useDatabase();
$metable = SampleMetable::create();
$handler = new ModelCollectionHandler();
$serialized = json_encode([
'class' => 'stdClass',
'items' => [[
'class' => SampleMetable::class,
'key' => $metable->getKey()
]]
]);
$unserialized = $handler->unserializeValue($serialized);

$this->assertInstanceOf(Collection::class, $unserialized);
$this->assertEquals([$metable->getKey()], $unserialized->modelKeys());
}

public function test_it_handles_invalid_model_class(): void
{
$handler = new ModelCollectionHandler();
$serialized = json_encode([
'class' => Collection::class,
'items' => [
'class' => 'stdClass',
'key' => '1'
Expand All @@ -45,6 +63,21 @@ public function test_it_handles_invalid_model_class(): void
$unserialized = $handler->unserializeValue($serialized);

$this->assertInstanceOf(Collection::class, $unserialized);
$this->assertEmpty($unserialized);
$this->assertCount(0, $unserialized);
}

public function test_it_handles_invalid_model_class_no_key(): void
{
$handler = new ModelCollectionHandler();
$serialized = json_encode([
'class' => Collection::class,
'items' => [
'class' => 'stdClass',
]
]);
$unserialized = $handler->unserializeValue($serialized);

$this->assertInstanceOf(Collection::class, $unserialized);
$this->assertCount(0, $unserialized);
}
}
Loading

0 comments on commit b468161

Please sign in to comment.