2021-10-10 18:05:51 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Tests\Unit\Rules;
|
|
|
|
|
|
|
|
use App\Rules\ValidSmartPlaylistRulePayload;
|
|
|
|
use Tests\TestCase;
|
|
|
|
|
|
|
|
class ValidSmartPlaylistRulePayloadTest extends TestCase
|
|
|
|
{
|
|
|
|
/** @return array<mixed> */
|
|
|
|
public function provideInvalidPayloads(): array
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
'invalid format' => ['foo'],
|
|
|
|
'invalid model' => [
|
|
|
|
[
|
|
|
|
[
|
2022-11-27 15:29:29 +00:00
|
|
|
'id' => 1,
|
2021-10-10 18:05:51 +00:00
|
|
|
'rules' => [
|
|
|
|
[
|
2022-11-27 15:29:29 +00:00
|
|
|
'id' => 1,
|
2021-10-10 18:05:51 +00:00
|
|
|
'model' => 'foo',
|
2022-11-27 15:29:29 +00:00
|
|
|
'operator' => 'contains',
|
2021-10-10 18:05:51 +00:00
|
|
|
'value' => ['bar'],
|
|
|
|
],
|
|
|
|
],
|
|
|
|
],
|
|
|
|
],
|
|
|
|
],
|
|
|
|
'invalid operator' => [
|
|
|
|
[
|
|
|
|
[
|
2022-11-27 15:29:29 +00:00
|
|
|
'id' => 1,
|
2021-10-10 18:05:51 +00:00
|
|
|
'rules' => [
|
|
|
|
[
|
2022-11-27 15:29:29 +00:00
|
|
|
'id' => 1,
|
2021-10-10 18:05:51 +00:00
|
|
|
'model' => 'artist.name',
|
|
|
|
'operator' => '<script>',
|
|
|
|
'value' => ['bar'],
|
|
|
|
],
|
|
|
|
],
|
|
|
|
],
|
|
|
|
],
|
|
|
|
],
|
|
|
|
'values are not an array' => [
|
|
|
|
[
|
|
|
|
[
|
2022-11-27 15:29:29 +00:00
|
|
|
'id' => 1,
|
2021-10-10 18:05:51 +00:00
|
|
|
'rules' => [
|
|
|
|
[
|
2022-11-27 15:29:29 +00:00
|
|
|
'id' => 1,
|
2021-10-10 18:05:51 +00:00
|
|
|
'model' => 'artist.name',
|
|
|
|
'operator' => 'is',
|
|
|
|
'value' => 'bar',
|
|
|
|
],
|
|
|
|
],
|
|
|
|
],
|
|
|
|
],
|
|
|
|
],
|
|
|
|
'values are empty' => [
|
|
|
|
[
|
|
|
|
[
|
2022-11-27 15:29:29 +00:00
|
|
|
'id' => 1,
|
2021-10-10 18:05:51 +00:00
|
|
|
'rules' => [
|
|
|
|
[
|
2022-11-27 15:29:29 +00:00
|
|
|
'id' => 1,
|
2021-10-10 18:05:51 +00:00
|
|
|
'model' => 'artist.name',
|
|
|
|
'operator' => 'is',
|
|
|
|
'value' => [],
|
|
|
|
],
|
|
|
|
],
|
|
|
|
],
|
|
|
|
],
|
|
|
|
],
|
|
|
|
'values item account exceeds 2' => [
|
|
|
|
[
|
|
|
|
[
|
2022-11-27 15:29:29 +00:00
|
|
|
'id' => 1,
|
2021-10-10 18:05:51 +00:00
|
|
|
'rules' => [
|
|
|
|
[
|
2022-11-27 15:29:29 +00:00
|
|
|
'id' => 1,
|
2021-10-10 18:05:51 +00:00
|
|
|
'model' => 'artist.name',
|
|
|
|
'operator' => 'is',
|
|
|
|
'value' => ['bar', 'baz', 'qux'],
|
|
|
|
],
|
|
|
|
],
|
|
|
|
],
|
|
|
|
],
|
|
|
|
],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @dataProvider provideInvalidPayloads */
|
|
|
|
public function testInvalidCases($value): void
|
|
|
|
{
|
|
|
|
self::assertFalse((new ValidSmartPlaylistRulePayload())->passes('rules', $value));
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @return array<mixed> */
|
|
|
|
public function provideValidPayloads(): array
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
'one rule' => [
|
|
|
|
[
|
|
|
|
[
|
2022-11-27 15:29:29 +00:00
|
|
|
'id' => 1,
|
2021-10-10 18:05:51 +00:00
|
|
|
'rules' => [
|
|
|
|
[
|
2022-11-27 15:29:29 +00:00
|
|
|
'id' => 2,
|
2021-10-10 18:05:51 +00:00
|
|
|
'model' => 'artist.name',
|
|
|
|
'operator' => 'is',
|
|
|
|
'value' => ['bar'],
|
|
|
|
],
|
|
|
|
],
|
|
|
|
],
|
|
|
|
],
|
|
|
|
],
|
|
|
|
'multiple rules' => [
|
|
|
|
[
|
|
|
|
[
|
2022-11-27 15:29:29 +00:00
|
|
|
'id' => 1,
|
2021-10-10 18:05:51 +00:00
|
|
|
'rules' => [
|
|
|
|
[
|
2022-11-27 15:29:29 +00:00
|
|
|
'id' => 1,
|
2021-10-10 18:05:51 +00:00
|
|
|
'model' => 'artist.name',
|
|
|
|
'operator' => 'is',
|
|
|
|
'value' => ['bar'],
|
|
|
|
],
|
|
|
|
[
|
2022-11-27 15:29:29 +00:00
|
|
|
'id' => 2,
|
2021-10-10 18:05:51 +00:00
|
|
|
'model' => 'interactions.play_count',
|
|
|
|
'operator' => 'isGreaterThan',
|
|
|
|
'value' => [50],
|
|
|
|
],
|
|
|
|
],
|
|
|
|
],
|
|
|
|
],
|
|
|
|
],
|
|
|
|
'multiple groups' => [
|
|
|
|
[
|
|
|
|
[
|
2022-11-27 15:29:29 +00:00
|
|
|
'id' => 1,
|
2021-10-10 18:05:51 +00:00
|
|
|
'rules' => [
|
|
|
|
[
|
2022-11-27 15:29:29 +00:00
|
|
|
'id' => 1,
|
2021-10-10 18:05:51 +00:00
|
|
|
'model' => 'artist.name',
|
|
|
|
'operator' => 'is',
|
|
|
|
'value' => ['bar'],
|
|
|
|
],
|
|
|
|
[
|
2022-11-27 15:29:29 +00:00
|
|
|
'id' => 2,
|
2021-10-10 18:05:51 +00:00
|
|
|
'model' => 'interactions.play_count',
|
|
|
|
'operator' => 'isGreaterThan',
|
|
|
|
'value' => [50],
|
|
|
|
],
|
|
|
|
],
|
|
|
|
],
|
|
|
|
[
|
2022-11-27 15:29:29 +00:00
|
|
|
'id' => 2,
|
2021-10-10 18:05:51 +00:00
|
|
|
'rules' => [
|
|
|
|
[
|
2022-11-27 15:29:29 +00:00
|
|
|
'id' => 1,
|
2021-10-10 18:05:51 +00:00
|
|
|
'model' => 'album.name',
|
|
|
|
'operator' => 'contains',
|
|
|
|
'value' => ['bar'],
|
|
|
|
],
|
|
|
|
[
|
2022-11-27 15:29:29 +00:00
|
|
|
'id' => 2,
|
2021-10-10 18:05:51 +00:00
|
|
|
'model' => 'interactions.play_count',
|
|
|
|
'operator' => 'isBetween',
|
|
|
|
'value' => [10, 100],
|
|
|
|
],
|
|
|
|
],
|
|
|
|
],
|
|
|
|
],
|
|
|
|
],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @dataProvider provideValidPayloads */
|
|
|
|
public function testValidCases($value): void
|
|
|
|
{
|
|
|
|
self::assertTrue((new ValidSmartPlaylistRulePayload())->passes('rules', $value));
|
|
|
|
}
|
|
|
|
}
|