RewindableGeneratorTest.php 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\DependencyInjection\Tests\Argument;
  11. use PHPUnit\Framework\TestCase;
  12. use Symfony\Component\DependencyInjection\Argument\RewindableGenerator;
  13. class RewindableGeneratorTest extends TestCase
  14. {
  15. public function testImplementsCountable()
  16. {
  17. $this->assertInstanceOf(\Countable::class, new RewindableGenerator(function () {
  18. yield 1;
  19. }, 1));
  20. }
  21. public function testCountUsesProvidedValue()
  22. {
  23. $generator = new RewindableGenerator(function () {
  24. yield 1;
  25. }, 3);
  26. $this->assertCount(3, $generator);
  27. }
  28. public function testCountUsesProvidedValueAsCallback()
  29. {
  30. $called = 0;
  31. $generator = new RewindableGenerator(function () {
  32. yield 1;
  33. }, function () use (&$called) {
  34. ++$called;
  35. return 3;
  36. });
  37. $this->assertSame(0, $called, 'Count callback is called lazily');
  38. $this->assertCount(3, $generator);
  39. count($generator);
  40. $this->assertSame(1, $called, 'Count callback is called only once');
  41. }
  42. }