* * Full license available in separate LICENSE file */ use Kadet\Functional\Collections as c; use Kadet\Functional\Exception\DsUnavailableException; /** * Runs without php-ds, see phpunit.no-ds.xml. */ class NoDsTest extends \PHPUnit\Framework\TestCase { public function testDsIsHidden() { $this->assertFalse(class_exists(\Ds\Seq::class)); } public function testTransformsOnArraysWork() { $this->assertSame(['a' => 2, 'b' => 4], c\map(fn ($v) => $v * 2)(['a' => 1, 'b' => 2])); $this->assertSame(['b' => 2], c\filter(fn ($v) => $v > 1)(['a' => 1, 'b' => 2])); $this->assertSame([1, 2], c\values()(['a' => 1, 'b' => 2])); $this->assertSame([1, 1, 2, 2], c\flatMap(fn ($v) => [$v, $v])([1, 2])); } public function testEagerTransformOnGeneratorGivesList() { $gen = static function () { yield 'a' => 1; yield 'b' => 2; }; $this->assertSame([1, 2], c\map(fn ($v) => $v)($gen())); $this->assertSame([2], c\filter(fn ($v) => $v > 1)($gen())); $this->assertSame([1, 2], c\values()($gen())); $this->assertSame([1, 1, 2, 2], c\flatMap(fn ($v) => [$v, $v])($gen())); } public function testToSeqThrowsNamedException() { $this->expectException(DsUnavailableException::class); $this->expectExceptionMessage('toSeq() needs php-ds: install ext-ds or require php-ds/php-ds'); c\toSeq(); } public function testGroupByThrows() { $this->expectException(DsUnavailableException::class); $this->expectExceptionMessage('groupBy()'); c\groupBy(fn ($v) => $v); } public function testOtherDsCollectorsThrow() { foreach (['toMap' => fn () => c\toMap(), 'toSet' => fn () => c\toSet(), 'keyBy' => fn () => c\keyBy(fn ($v) => $v)] as $name => $build) { try { $build(); $this->fail("$name() should throw"); } catch (DsUnavailableException $e) { $this->assertStringContainsString("$name()", $e->getMessage()); } } } public function testToArrayWorks() { $this->assertSame(['a' => 1], c\toArray()(['a' => 1])); } }