/** @test */ public function method_find() { $oneThroughTen = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; $moreThanFive = Spy::returnsUsing(function ($n) { return $n > 5; }); $this->assertEquals(6, Fn\find($oneThroughTen, $moreThanFive), 'Should return the first value to pass the predicate'); // Should only call predicate until value is found (for performance) $moreThanFive->shouldHaveBeenCalled()->times(6); $this->assertNull(Fn\find([1, 2, 3], $moreThanFive), 'Should return null if none match.'); $this->assertNull(Fn\find([], Fn\always()), 'Should return null if collection is empty'); $spy = Spy::returns(true); $this->assertEquals('bar', Fn\find(['foo' => 'bar'], $spy), 'Should work with associateive arrays'); // Should call spy with ($val, $key); $spy->shouldHaveBeenCalled()->with('bar', 'foo'); }
/** @test */ public function returnsUsing_shouldCreateASpyWhichReturnsUsingTheCallable() { $spy = Spy::returnsUsing(function ($arg) { return strtoupper($arg); }); $this->assertEquals('FOO', $spy('foo')); }