Skip to content

Commit

Permalink
Creates a test case for and fixes a bug created by lack of is_string …
Browse files Browse the repository at this point in the history
…before str_contains.
  • Loading branch information
cage-is committed Jan 9, 2025
1 parent e9e4bc3 commit a0d41ba
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Query/Constraint/Comparison.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public function compare(mixed $subject, mixed $reference): bool
self::NotIn => !in_array($subject, $reference, self::isSingleValue($subject)), /* @phpstan-ignore-line */
self::Regexp => is_string($subject) && 1 === preg_match($reference, $subject), /* @phpstan-ignore-line */
self::NotRegexp => is_string($subject) && 1 !== preg_match($reference, $subject), /* @phpstan-ignore-line */
self::Contains => str_contains($subject, $reference), /* @phpstan-ignore-line */
self::Contains => is_string($subject) && str_contains($subject, $reference), /* @phpstan-ignore-line */
self::NotContain => is_string($subject) && !str_contains($subject, $reference), /* @phpstan-ignore-line */
self::StartsWith => is_string($subject) && str_starts_with($subject, $reference), /* @phpstan-ignore-line */
self::EndsWith => is_string($subject) && str_ends_with($subject, $reference), /* @phpstan-ignore-line */
Expand Down
19 changes: 19 additions & 0 deletions src/StatementTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

namespace League\Csv;

use League\Csv\Query\Constraint\Comparison;
use League\Csv\Query\QueryException;
use OutOfBoundsException;
use PHPUnit\Framework\Attributes\DataProvider;
Expand Down Expand Up @@ -240,4 +241,22 @@ public function testOrderByDoesNotThrowOnInvalidOffsetOrLimit(): void

self::assertSame([], $constraints->process($csv)->nth(42));
}

public function testItCanCompareNullValue(): void
{
$document = <<<CSV
Title,Name,Number
Commander,Fred,104
Officer,John,117
Major,Avery
CSV;

$csv = Reader::createFromString($document)->setHeaderOffset(0);
$statement = Statement::create()->andWhere('Number', Comparison::Contains, '117');

// The count is really just to have the comparison iterate the entire
// list. The last item in the list will break the comparison because of
// the lack of is_string(...).
self::assertSame(1, $statement->process($csv)->count());

Check failure on line 260 in src/StatementTest.php

View workflow job for this annotation

GitHub Actions / PHP on 8.3 - prefer-stable -

You should use assertCount($expectedCount, $variable) instead of assertSame($expectedCount, $variable->count()).
}
}

0 comments on commit a0d41ba

Please sign in to comment.