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 bb86286
Show file tree
Hide file tree
Showing 4 changed files with 162 additions and 83 deletions.
3 changes: 1 addition & 2 deletions src/Commands/RefreshMeta.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,4 @@ public function handle(): void

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

}
}
3 changes: 1 addition & 2 deletions tests/Integration/Commands/RefreshMetaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,5 +79,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());
}
}
38 changes: 38 additions & 0 deletions tests/Integration/MetableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -353,14 +353,17 @@ public function test_it_can_be_queried_by_meta_value(): void
$metable = $this->createMetable();
$metable->setMeta('foo', 'bar');
$metable->setMeta('datetime', $now);
$metable->setMeta('long', str_repeat('a', 300));

$result1 = SampleMetable::whereMeta('foo', 'bar')->first();
$result2 = SampleMetable::whereMeta('foo', 'baz')->first();
$result3 = SampleMetable::whereMeta('datetime', $now)->first();
$result4 = SampleMetable::whereMeta('long', str_repeat('a', 300))->first();

$this->assertEquals($metable->getKey(), $result1->getKey());
$this->assertNull($result2);
$this->assertEquals($metable->getKey(), $result3->getKey());
$this->assertEquals($metable->getKey(), $result4->getKey());
}

public function test_it_can_be_queried_by_numeric_meta_value(): void
Expand All @@ -371,9 +374,11 @@ public function test_it_can_be_queried_by_numeric_meta_value(): void

$result = SampleMetable::whereMetaNumeric('foo', '>', 4)->get();
$result2 = SampleMetable::whereMetaNumeric('foo', '<', 4)->get();
$result3 = SampleMetable::whereMetaNumeric('foo', 123)->get();

$this->assertEquals([$metable->getKey()], $result->modelKeys());
$this->assertEquals([], $result2->modelKeys());
$this->assertEquals([$metable->getKey()], $result3->modelKeys());
}

public function test_it_can_be_queried_by_in_array(): void
Expand Down Expand Up @@ -450,6 +455,27 @@ public function test_it_can_be_queried_by_meta_between(): void
$this->assertEquals([], $result2->modelKeys());
}

public function test_it_can_be_queried_by_meta_not_between(): void
{
$this->useDatabase();
$metable = $this->createMetable();
$metable->setMeta('foo', 'c');

$result1 = SampleMetable::whereMetaNotBetween(
'foo',
'a',
'd'
)->get();
$result2 = SampleMetable::whereMetaNotBetween(
'foo',
'd',
'z'
)->get();

$this->assertEquals([], $result1->modelKeys());
$this->assertEquals([$metable->getKey()], $result2->modelKeys());
}

public function test_it_can_be_queried_by_meta_between_numeric(): void
{
$this->useDatabase();
Expand Down Expand Up @@ -665,6 +691,18 @@ public function test_it_can_serialize_properly(): void
$this->assertEquals('baz', $result->getMeta('foo'));
}

public function test_it_throws_for_param_that_cannot_be_converted_to_string(): void
{
$this->expectException(\LogicException::class);
SampleMetable::whereMeta('foo', null)->get();
}

public function test_it_throws_for_param_that_cannot_be_converted_to_numeric(): void
{
$this->expectException(\LogicException::class);
SampleMetable::whereMetaNumeric('foo', null)->get();
}

private function makeMeta(array $attributes = []): Meta
{
return factory(Meta::class)->make($attributes);
Expand Down

0 comments on commit bb86286

Please sign in to comment.