2021-10-10 18:05:51 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Tests\Unit\Rules;
|
|
|
|
|
|
|
|
use App\Rules\ValidSmartPlaylistRulePayload;
|
2024-09-30 22:15:38 +00:00
|
|
|
use Exception;
|
2021-10-10 18:05:51 +00:00
|
|
|
use Tests\TestCase;
|
|
|
|
|
|
|
|
class ValidSmartPlaylistRulePayloadTest extends TestCase
|
|
|
|
{
|
|
|
|
/** @return array<mixed> */
|
2024-04-18 19:27:20 +00:00
|
|
|
public static function provideInvalidPayloads(): array
|
2021-10-10 18:05:51 +00:00
|
|
|
{
|
|
|
|
return [
|
|
|
|
'invalid format' => ['foo'],
|
|
|
|
'invalid model' => [
|
|
|
|
[
|
|
|
|
[
|
2023-04-17 19:45:43 +00:00
|
|
|
'id' => '2a4548cd-c67f-44d4-8fec-34ff75c8a026',
|
2021-10-10 18:05:51 +00:00
|
|
|
'rules' => [
|
|
|
|
[
|
2023-04-17 19:45:43 +00:00
|
|
|
'id' => '2a4548cd-c67f-44d4-8fec-34ff75c8a026',
|
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' => [
|
|
|
|
[
|
|
|
|
[
|
2023-04-17 19:45:43 +00:00
|
|
|
'id' => '2a4548cd-c67f-44d4-8fec-34ff75c8a026',
|
2021-10-10 18:05:51 +00:00
|
|
|
'rules' => [
|
|
|
|
[
|
2023-04-17 19:45:43 +00:00
|
|
|
'id' => '2a4548cd-c67f-44d4-8fec-34ff75c8a026',
|
2021-10-10 18:05:51 +00:00
|
|
|
'model' => 'artist.name',
|
|
|
|
'operator' => '<script>',
|
|
|
|
'value' => ['bar'],
|
|
|
|
],
|
|
|
|
],
|
|
|
|
],
|
|
|
|
],
|
|
|
|
],
|
|
|
|
'values are not an array' => [
|
|
|
|
[
|
|
|
|
[
|
2023-04-17 19:45:43 +00:00
|
|
|
'id' => '2a4548cd-c67f-44d4-8fec-34ff75c8a026',
|
2021-10-10 18:05:51 +00:00
|
|
|
'rules' => [
|
|
|
|
[
|
2023-04-17 19:45:43 +00:00
|
|
|
'id' => 'f5fcc10f-eb6a-40f6-baf9-db573de088f8',
|
2021-10-10 18:05:51 +00:00
|
|
|
'model' => 'artist.name',
|
|
|
|
'operator' => 'is',
|
|
|
|
'value' => 'bar',
|
|
|
|
],
|
|
|
|
],
|
|
|
|
],
|
|
|
|
],
|
|
|
|
],
|
|
|
|
'values are empty' => [
|
|
|
|
[
|
|
|
|
[
|
2023-04-17 19:45:43 +00:00
|
|
|
'id' => '2a4548cd-c67f-44d4-8fec-34ff75c8a026',
|
2021-10-10 18:05:51 +00:00
|
|
|
'rules' => [
|
|
|
|
[
|
2023-04-17 19:45:43 +00:00
|
|
|
'id' => '2a4548cd-c67f-44d4-8fec-34ff75c8a026',
|
2021-10-10 18:05:51 +00:00
|
|
|
'model' => 'artist.name',
|
|
|
|
'operator' => 'is',
|
|
|
|
'value' => [],
|
|
|
|
],
|
|
|
|
],
|
|
|
|
],
|
|
|
|
],
|
|
|
|
],
|
|
|
|
'values item account exceeds 2' => [
|
|
|
|
[
|
|
|
|
[
|
2023-04-17 19:45:43 +00:00
|
|
|
'id' => '2a4548cd-c67f-44d4-8fec-34ff75c8a026',
|
2021-10-10 18:05:51 +00:00
|
|
|
'rules' => [
|
|
|
|
[
|
2023-04-17 19:45:43 +00:00
|
|
|
'id' => '2a4548cd-c67f-44d4-8fec-34ff75c8a026',
|
2021-10-10 18:05:51 +00:00
|
|
|
'model' => 'artist.name',
|
|
|
|
'operator' => 'is',
|
|
|
|
'value' => ['bar', 'baz', 'qux'],
|
|
|
|
],
|
|
|
|
],
|
|
|
|
],
|
|
|
|
],
|
|
|
|
],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @dataProvider provideInvalidPayloads */
|
|
|
|
public function testInvalidCases($value): void
|
|
|
|
{
|
2024-09-30 22:15:38 +00:00
|
|
|
$this->expectExceptionMessage('Invalid smart playlist rules');
|
|
|
|
|
|
|
|
$fail = static fn (string $message) => throw new Exception($message);
|
|
|
|
(new ValidSmartPlaylistRulePayload())->validate('rules', $value, $fail);
|
2021-10-10 18:05:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/** @return array<mixed> */
|
2024-04-18 19:27:20 +00:00
|
|
|
public static function provideValidPayloads(): array
|
2021-10-10 18:05:51 +00:00
|
|
|
{
|
|
|
|
return [
|
|
|
|
'one rule' => [
|
|
|
|
[
|
|
|
|
[
|
2023-04-17 19:45:43 +00:00
|
|
|
'id' => '2a4548cd-c67f-44d4-8fec-34ff75c8a026',
|
2021-10-10 18:05:51 +00:00
|
|
|
'rules' => [
|
|
|
|
[
|
2023-04-17 19:45:43 +00:00
|
|
|
'id' => '45368b8f-fec8-4b72-b826-6b295af0da65',
|
2021-10-10 18:05:51 +00:00
|
|
|
'model' => 'artist.name',
|
|
|
|
'operator' => 'is',
|
|
|
|
'value' => ['bar'],
|
|
|
|
],
|
|
|
|
],
|
|
|
|
],
|
|
|
|
],
|
|
|
|
],
|
|
|
|
'multiple rules' => [
|
|
|
|
[
|
|
|
|
[
|
2023-04-17 19:45:43 +00:00
|
|
|
'id' => '2a4548cd-c67f-44d4-8fec-34ff75c8a026',
|
2021-10-10 18:05:51 +00:00
|
|
|
'rules' => [
|
|
|
|
[
|
2023-04-17 19:45:43 +00:00
|
|
|
'id' => '45368b8f-fec8-4b72-b826-6b295af0da65',
|
2021-10-10 18:05:51 +00:00
|
|
|
'model' => 'artist.name',
|
|
|
|
'operator' => 'is',
|
|
|
|
'value' => ['bar'],
|
|
|
|
],
|
|
|
|
[
|
2023-04-17 19:45:43 +00:00
|
|
|
'id' => '2a4548cd-c67f-44d4-8fec-34ff75c8a026',
|
2021-10-10 18:05:51 +00:00
|
|
|
'model' => 'interactions.play_count',
|
|
|
|
'operator' => 'isGreaterThan',
|
|
|
|
'value' => [50],
|
|
|
|
],
|
|
|
|
],
|
|
|
|
],
|
|
|
|
],
|
|
|
|
],
|
|
|
|
'multiple groups' => [
|
|
|
|
[
|
|
|
|
[
|
2023-04-17 19:45:43 +00:00
|
|
|
'id' => '54989bb8-9e4f-4f6e-a28f-320834f9435e',
|
2021-10-10 18:05:51 +00:00
|
|
|
'rules' => [
|
|
|
|
[
|
2023-04-17 19:45:43 +00:00
|
|
|
'id' => '59e95d10-e297-4f33-b2d8-de55e64a02fa',
|
2021-10-10 18:05:51 +00:00
|
|
|
'model' => 'artist.name',
|
|
|
|
'operator' => 'is',
|
|
|
|
'value' => ['bar'],
|
|
|
|
],
|
|
|
|
[
|
2023-04-17 19:45:43 +00:00
|
|
|
'id' => 'fefa409c-5539-4612-949f-47f71d06c828',
|
2021-10-10 18:05:51 +00:00
|
|
|
'model' => 'interactions.play_count',
|
|
|
|
'operator' => 'isGreaterThan',
|
|
|
|
'value' => [50],
|
|
|
|
],
|
|
|
|
],
|
|
|
|
],
|
|
|
|
[
|
2023-04-17 19:45:43 +00:00
|
|
|
'id' => '45b23131-ece6-4461-8c1b-4d865f06a395',
|
2021-10-10 18:05:51 +00:00
|
|
|
'rules' => [
|
|
|
|
[
|
2023-04-17 19:45:43 +00:00
|
|
|
'id' => 'e3e2f1cc-bde1-43fc-9fb2-96ea7d64412c',
|
2021-10-10 18:05:51 +00:00
|
|
|
'model' => 'album.name',
|
|
|
|
'operator' => 'contains',
|
|
|
|
'value' => ['bar'],
|
|
|
|
],
|
|
|
|
[
|
2023-04-17 19:45:43 +00:00
|
|
|
'id' => '39bba5c4-e9cb-4b72-a241-6b7c6cc14c3c',
|
2021-10-10 18:05:51 +00:00
|
|
|
'model' => 'interactions.play_count',
|
|
|
|
'operator' => 'isBetween',
|
|
|
|
'value' => [10, 100],
|
|
|
|
],
|
|
|
|
],
|
|
|
|
],
|
|
|
|
],
|
|
|
|
],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @dataProvider provideValidPayloads */
|
|
|
|
public function testValidCases($value): void
|
|
|
|
{
|
2024-05-31 05:40:34 +00:00
|
|
|
(new ValidSmartPlaylistRulePayload())->validate('rules', $value, static fn ($foo) => $foo);
|
|
|
|
self::addToAssertionCount(1);
|
2021-10-10 18:05:51 +00:00
|
|
|
}
|
|
|
|
}
|