Skip to content

Commit

Permalink
invert IN operator
Browse files Browse the repository at this point in the history
  • Loading branch information
pascual.munoz committed Sep 20, 2022
1 parent d44928a commit 3ea219a
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 11 deletions.
4 changes: 2 additions & 2 deletions src/ArrayCriteriaVisitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,11 @@ static function ($item) use ($filter) {
}

if (FilterOperator::IN === $filter->operator()->value()) {
return false !== in_array($filter->value()->value(), $itemValue);
return false !== in_array($itemValue, $filter->value()->value());
}

if (FilterOperator::NOT_IN === $filter->operator()->value()) {
return true !== in_array($filter->value()->value(), $itemValue);
return true !== in_array($itemValue, $filter->value()->value());
}
}

Expand Down
15 changes: 10 additions & 5 deletions tests/ArrayBuilderCriteriaVisitorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

namespace PcComponentes\CriteriaArrayAdapter\Tests;

use Pccomponentes\Criteria\Domain\Criteria\FilterArrayValue;
use PcComponentes\CriteriaArrayAdapter\Tests\Mocks\MemoryArticleRepository;
use PcComponentes\CriteriaArrayAdapter\Tests\Mocks\ArticleObjectMother;
use Pccomponentes\Criteria\Domain\Criteria\AndFilter;
Expand Down Expand Up @@ -314,14 +315,16 @@ public function test_not_equal_operator()
public function test_in_operator()
{
$article = ArticleObjectMother::random();
$withNamePhone = ArticleObjectMother::withNamePhone();
$this->repository->save($article);
$this->repository->save($withNamePhone);

$criteria = new Criteria(
new Filters(
new Filter(
FilterField::from('tags'),
FilterField::from('name'),
FilterOperator::from(FilterOperator::IN),
FilterValue::from(ArticleObjectMother::TAG_INCLUDED),
FilterArrayValue::from(['benito', 'Phone']),
),
),
null,
Expand All @@ -330,21 +333,23 @@ public function test_in_operator()
);

$result = $this->repository->filter($criteria);
$this->assertEquals($article, $result[0]);
$this->assertEquals($withNamePhone, $result[1]);

}

public function test_not_in_operator()
{
$article = ArticleObjectMother::random();
$withNamePhone = ArticleObjectMother::withNamePhone();
$this->repository->save($article);
$this->repository->save($withNamePhone);

$criteria = new Criteria(
new Filters(
new Filter(
FilterField::from('tags'),
FilterField::from('name'),
FilterOperator::from(FilterOperator::NOT_IN),
FilterValue::from(ArticleObjectMother::TAG_NOT_INCLUDED),
FilterArrayValue::from(['Phone']),
),
),
null,
Expand Down
2 changes: 1 addition & 1 deletion tests/Mocks/Article.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
class Article
{
private Uuid $id;
private string $name;
protected string $name;
private float $stock;
private \DateTimeInterface $date;
private array $tags;
Expand Down
15 changes: 12 additions & 3 deletions tests/Mocks/ArticleObjectMother.php
Original file line number Diff line number Diff line change
@@ -1,16 +1,25 @@
<?php

declare(strict_types=1);

namespace PcComponentes\CriteriaArrayAdapter\Tests\Mocks;

use PcComponentes\Ddd\Domain\Model\ValueObject\Uuid;
use DateTimeImmutable;
use Faker\Factory;
use PcComponentes\Ddd\Domain\Model\ValueObject\Uuid;

class ArticleObjectMother
class ArticleObjectMother extends Article
{
const TAG_INCLUDED = 'TAG1';
const TAG_NOT_INCLUDED = 'TAG2';

public static function withNamePhone(): Article
{
$withNamePhone = self::random();
$withNamePhone->name = 'Phone';
return $withNamePhone;
}

public static function random(): Article
{
$faker = Factory::create();
Expand All @@ -19,7 +28,7 @@ public static function random(): Article
Uuid::v4(),
$faker->text(100),
$faker->numberBetween(0, 1000),
\DateTimeImmutable::createFromMutable($faker->dateTimeBetween('-10 year', 'now')),
DateTimeImmutable::createFromMutable($faker->dateTimeBetween('-10 year', 'now')),
[self::TAG_INCLUDED]
);
}
Expand Down

0 comments on commit 3ea219a

Please sign in to comment.