123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784 |
- <?php
- namespace Illuminate\Tests\View;
- use Closure;
- use ErrorException;
- use Illuminate\Container\Container;
- use Illuminate\Contracts\Events\Dispatcher as DispatcherContract;
- use Illuminate\Contracts\View\Engine;
- use Illuminate\Contracts\View\View as ViewContract;
- use Illuminate\Events\Dispatcher;
- use Illuminate\Filesystem\Filesystem;
- use Illuminate\Support\HtmlString;
- use Illuminate\View\Compilers\CompilerInterface;
- use Illuminate\View\Engines\CompilerEngine;
- use Illuminate\View\Engines\EngineResolver;
- use Illuminate\View\Engines\PhpEngine;
- use Illuminate\View\Factory;
- use Illuminate\View\View;
- use Illuminate\View\ViewFinderInterface;
- use InvalidArgumentException;
- use Mockery as m;
- use PHPUnit\Framework\TestCase;
- use ReflectionFunction;
- use stdClass;
- class ViewFactoryTest extends TestCase
- {
- protected function tearDown(): void
- {
- m::close();
- }
- public function testMakeCreatesNewViewInstanceWithProperPathAndEngine()
- {
- unset($_SERVER['__test.view']);
- $factory = $this->getFactory();
- $factory->getFinder()->shouldReceive('find')->once()->with('view')->andReturn('path.php');
- $factory->getEngineResolver()->shouldReceive('resolve')->once()->with('php')->andReturn($engine = m::mock(Engine::class));
- $factory->getFinder()->shouldReceive('addExtension')->once()->with('php');
- $factory->setDispatcher(new Dispatcher);
- $factory->creator('view', function ($view) {
- $_SERVER['__test.view'] = $view;
- });
- $factory->addExtension('php', 'php');
- $view = $factory->make('view', ['foo' => 'bar'], ['baz' => 'boom']);
- $this->assertSame($engine, $view->getEngine());
- $this->assertSame($_SERVER['__test.view'], $view);
- unset($_SERVER['__test.view']);
- }
- public function testExistsPassesAndFailsViews()
- {
- $factory = $this->getFactory();
- $factory->getFinder()->shouldReceive('find')->once()->with('foo')->andThrow(InvalidArgumentException::class);
- $factory->getFinder()->shouldReceive('find')->once()->with('bar')->andReturn('path.php');
- $this->assertFalse($factory->exists('foo'));
- $this->assertTrue($factory->exists('bar'));
- }
- public function testRenderingOnceChecks()
- {
- $factory = $this->getFactory();
- $this->assertFalse($factory->hasRenderedOnce('foo'));
- $factory->markAsRenderedOnce('foo');
- $this->assertTrue($factory->hasRenderedOnce('foo'));
- $factory->flushState();
- $this->assertFalse($factory->hasRenderedOnce('foo'));
- }
- public function testFirstCreatesNewViewInstanceWithProperPath()
- {
- unset($_SERVER['__test.view']);
- $factory = $this->getFactory();
- $factory->getFinder()->shouldReceive('find')->twice()->with('view')->andReturn('path.php');
- $factory->getFinder()->shouldReceive('find')->once()->with('bar')->andThrow(InvalidArgumentException::class);
- $factory->getEngineResolver()->shouldReceive('resolve')->once()->with('php')->andReturn($engine = m::mock(Engine::class));
- $factory->getFinder()->shouldReceive('addExtension')->once()->with('php');
- $factory->setDispatcher(new Dispatcher);
- $factory->creator('view', function ($view) {
- $_SERVER['__test.view'] = $view;
- });
- $factory->addExtension('php', 'php');
- $view = $factory->first(['bar', 'view'], ['foo' => 'bar'], ['baz' => 'boom']);
- $this->assertInstanceOf(ViewContract::class, $view);
- $this->assertSame($engine, $view->getEngine());
- $this->assertSame($_SERVER['__test.view'], $view);
- unset($_SERVER['__test.view']);
- }
- public function testFirstThrowsInvalidArgumentExceptionIfNoneFound()
- {
- $this->expectException(InvalidArgumentException::class);
- $factory = $this->getFactory();
- $factory->getFinder()->shouldReceive('find')->once()->with('view')->andThrow(InvalidArgumentException::class);
- $factory->getFinder()->shouldReceive('find')->once()->with('bar')->andThrow(InvalidArgumentException::class);
- $factory->getEngineResolver()->shouldReceive('resolve')->with('php')->andReturn($engine = m::mock(Engine::class));
- $factory->getFinder()->shouldReceive('addExtension')->with('php');
- $factory->addExtension('php', 'php');
- $factory->first(['bar', 'view'], ['foo' => 'bar'], ['baz' => 'boom']);
- }
- public function testRenderEachCreatesViewForEachItemInArray()
- {
- $factory = m::mock(Factory::class.'[make]', $this->getFactoryArgs());
- $factory->shouldReceive('make')->once()->with('foo', ['key' => 'bar', 'value' => 'baz'])->andReturn($mockView1 = m::mock(stdClass::class));
- $factory->shouldReceive('make')->once()->with('foo', ['key' => 'breeze', 'value' => 'boom'])->andReturn($mockView2 = m::mock(stdClass::class));
- $mockView1->shouldReceive('render')->once()->andReturn('dayle');
- $mockView2->shouldReceive('render')->once()->andReturn('rees');
- $result = $factory->renderEach('foo', ['bar' => 'baz', 'breeze' => 'boom'], 'value');
- $this->assertSame('daylerees', $result);
- }
- public function testEmptyViewsCanBeReturnedFromRenderEach()
- {
- $factory = m::mock(Factory::class.'[make]', $this->getFactoryArgs());
- $factory->shouldReceive('make')->once()->with('foo')->andReturn($mockView = m::mock(stdClass::class));
- $mockView->shouldReceive('render')->once()->andReturn('empty');
- $this->assertSame('empty', $factory->renderEach('view', [], 'iterator', 'foo'));
- }
- public function testRawStringsMayBeReturnedFromRenderEach()
- {
- $this->assertSame('foo', $this->getFactory()->renderEach('foo', [], 'item', 'raw|foo'));
- }
- public function testEnvironmentAddsExtensionWithCustomResolver()
- {
- $factory = $this->getFactory();
- $resolver = function () {
- //
- };
- $factory->getFinder()->shouldReceive('addExtension')->once()->with('foo');
- $factory->getEngineResolver()->shouldReceive('register')->once()->with('bar', $resolver);
- $factory->getFinder()->shouldReceive('find')->once()->with('view')->andReturn('path.foo');
- $factory->getEngineResolver()->shouldReceive('resolve')->once()->with('bar')->andReturn($engine = m::mock(Engine::class));
- $factory->getDispatcher()->shouldReceive('dispatch');
- $factory->addExtension('foo', 'bar', $resolver);
- $view = $factory->make('view', ['data']);
- $this->assertSame($engine, $view->getEngine());
- }
- public function testAddingExtensionPrependsNotAppends()
- {
- $factory = $this->getFactory();
- $factory->getFinder()->shouldReceive('addExtension')->once()->with('foo');
- $factory->addExtension('foo', 'bar');
- $extensions = $factory->getExtensions();
- $this->assertSame('bar', reset($extensions));
- $this->assertSame('foo', key($extensions));
- }
- public function testPrependedExtensionOverridesExistingExtensions()
- {
- $factory = $this->getFactory();
- $factory->getFinder()->shouldReceive('addExtension')->once()->with('foo');
- $factory->getFinder()->shouldReceive('addExtension')->once()->with('baz');
- $factory->addExtension('foo', 'bar');
- $factory->addExtension('baz', 'bar');
- $extensions = $factory->getExtensions();
- $this->assertSame('bar', reset($extensions));
- $this->assertSame('baz', key($extensions));
- }
- public function testComposersAreProperlyRegistered()
- {
- $factory = $this->getFactory();
- $factory->getDispatcher()->shouldReceive('listen')->once()->with('composing: foo', m::type(Closure::class));
- $callback = $factory->composer('foo', function () {
- return 'bar';
- });
- $callback = $callback[0];
- $this->assertSame('bar', $callback());
- }
- public function testComposersCanBeMassRegistered()
- {
- $factory = $this->getFactory();
- $factory->getDispatcher()->shouldReceive('listen')->once()->with('composing: bar', m::type(Closure::class));
- $factory->getDispatcher()->shouldReceive('listen')->once()->with('composing: qux', m::type(Closure::class));
- $factory->getDispatcher()->shouldReceive('listen')->once()->with('composing: foo', m::type(Closure::class));
- $composers = $factory->composers([
- 'foo' => 'bar',
- 'baz@baz' => ['qux', 'foo'],
- ]);
- $this->assertCount(3, $composers);
- $reflections = [
- new ReflectionFunction($composers[0]),
- new ReflectionFunction($composers[1]),
- ];
- $this->assertEquals(['class' => 'foo', 'method' => 'compose'], $reflections[0]->getStaticVariables());
- $this->assertEquals(['class' => 'baz', 'method' => 'baz'], $reflections[1]->getStaticVariables());
- }
- public function testClassCallbacks()
- {
- $factory = $this->getFactory();
- $factory->getDispatcher()->shouldReceive('listen')->once()->with('composing: foo', m::type(Closure::class));
- $factory->setContainer($container = m::mock(Container::class));
- $container->shouldReceive('make')->once()->with('FooComposer')->andReturn($composer = m::mock(stdClass::class));
- $composer->shouldReceive('compose')->once()->with('view')->andReturn('composed');
- $callback = $factory->composer('foo', 'FooComposer');
- $callback = $callback[0];
- $this->assertSame('composed', $callback('view'));
- }
- public function testClassCallbacksWithMethods()
- {
- $factory = $this->getFactory();
- $factory->getDispatcher()->shouldReceive('listen')->once()->with('composing: foo', m::type(Closure::class));
- $factory->setContainer($container = m::mock(Container::class));
- $container->shouldReceive('make')->once()->with('FooComposer')->andReturn($composer = m::mock(stdClass::class));
- $composer->shouldReceive('doComposer')->once()->with('view')->andReturn('composed');
- $callback = $factory->composer('foo', 'FooComposer@doComposer');
- $callback = $callback[0];
- $this->assertSame('composed', $callback('view'));
- }
- public function testCallComposerCallsProperEvent()
- {
- $factory = $this->getFactory();
- $view = m::mock(View::class);
- $view->shouldReceive('name')->once()->andReturn('name');
- $factory->getDispatcher()->shouldReceive('dispatch')->once()->with('composing: name', [$view]);
- $factory->callComposer($view);
- }
- public function testComposersAreRegisteredWithSlashAndDot()
- {
- $factory = $this->getFactory();
- $factory->getDispatcher()->shouldReceive('listen')->with('composing: foo.bar', m::any())->twice();
- $factory->composer('foo.bar', '');
- $factory->composer('foo/bar', '');
- }
- public function testRenderCountHandling()
- {
- $factory = $this->getFactory();
- $factory->incrementRender();
- $this->assertFalse($factory->doneRendering());
- $factory->decrementRender();
- $this->assertTrue($factory->doneRendering());
- }
- public function testYieldDefault()
- {
- $factory = $this->getFactory();
- $this->assertSame('hi', $factory->yieldContent('foo', 'hi'));
- }
- public function testYieldDefaultIsEscaped()
- {
- $factory = $this->getFactory();
- $this->assertSame('<p>hi</p>', $factory->yieldContent('foo', '<p>hi</p>'));
- }
- public function testYieldDefaultViewIsNotEscapedTwice()
- {
- $factory = $this->getFactory();
- $view = m::mock(View::class);
- $view->shouldReceive('__toString')->once()->andReturn('<p>hi</p><p>already escaped</p>');
- $this->assertSame('<p>hi</p><p>already escaped</p>', $factory->yieldContent('foo', $view));
- }
- public function testBasicSectionHandling()
- {
- $factory = $this->getFactory();
- $factory->startSection('foo');
- echo 'hi';
- $factory->stopSection();
- $this->assertSame('hi', $factory->yieldContent('foo'));
- }
- public function testBasicSectionDefault()
- {
- $factory = $this->getFactory();
- $factory->startSection('foo', 'hi');
- $this->assertSame('hi', $factory->yieldContent('foo'));
- }
- public function testBasicSectionDefaultIsEscaped()
- {
- $factory = $this->getFactory();
- $factory->startSection('foo', '<p>hi</p>');
- $this->assertSame('<p>hi</p>', $factory->yieldContent('foo'));
- }
- public function testBasicSectionDefaultViewIsNotEscapedTwice()
- {
- $factory = $this->getFactory();
- $view = m::mock(View::class);
- $view->shouldReceive('__toString')->once()->andReturn('<p>hi</p><p>already escaped</p>');
- $factory->startSection('foo', $view);
- $this->assertSame('<p>hi</p><p>already escaped</p>', $factory->yieldContent('foo'));
- }
- public function testSectionExtending()
- {
- $placeholder = Factory::parentPlaceholder('foo');
- $factory = $this->getFactory();
- $factory->startSection('foo');
- echo 'hi '.$placeholder;
- $factory->stopSection();
- $factory->startSection('foo');
- echo 'there';
- $factory->stopSection();
- $this->assertSame('hi there', $factory->yieldContent('foo'));
- }
- public function testSectionMultipleExtending()
- {
- $placeholder = Factory::parentPlaceholder('foo');
- $factory = $this->getFactory();
- $factory->startSection('foo');
- echo 'hello '.$placeholder.' nice to see you '.$placeholder;
- $factory->stopSection();
- $factory->startSection('foo');
- echo 'my '.$placeholder;
- $factory->stopSection();
- $factory->startSection('foo');
- echo 'friend';
- $factory->stopSection();
- $this->assertSame('hello my friend nice to see you my friend', $factory->yieldContent('foo'));
- }
- public function testComponentHandling()
- {
- $factory = $this->getFactory();
- $factory->getFinder()->shouldReceive('find')->andReturn(__DIR__.'/fixtures/component.php');
- $factory->getEngineResolver()->shouldReceive('resolve')->andReturn(new PhpEngine(new Filesystem));
- $factory->getDispatcher()->shouldReceive('dispatch');
- $factory->startComponent('component', ['name' => 'Taylor']);
- $factory->slot('title');
- $factory->slot('website', 'laravel.com', []);
- echo 'title<hr>';
- $factory->endSlot();
- echo 'component';
- $contents = $factory->renderComponent();
- $this->assertSame('title<hr> component Taylor laravel.com', $contents);
- }
- public function testComponentHandlingUsingViewObject()
- {
- $factory = $this->getFactory();
- $factory->getFinder()->shouldReceive('find')->andReturn(__DIR__.'/fixtures/component.php');
- $factory->getEngineResolver()->shouldReceive('resolve')->andReturn(new PhpEngine(new Filesystem));
- $factory->getDispatcher()->shouldReceive('dispatch');
- $factory->startComponent($factory->make('component'), ['name' => 'Taylor']);
- $factory->slot('title');
- $factory->slot('website', 'laravel.com', []);
- echo 'title<hr>';
- $factory->endSlot();
- echo 'component';
- $contents = $factory->renderComponent();
- $this->assertSame('title<hr> component Taylor laravel.com', $contents);
- }
- public function testComponentHandlingUsingClosure()
- {
- $factory = $this->getFactory();
- $factory->getFinder()->shouldReceive('find')->andReturn(__DIR__.'/fixtures/component.php');
- $factory->getEngineResolver()->shouldReceive('resolve')->andReturn(new PhpEngine(new Filesystem));
- $factory->getDispatcher()->shouldReceive('dispatch');
- $factory->startComponent(function ($data) use ($factory) {
- $this->assertArrayHasKey('name', $data);
- $this->assertSame($data['name'], 'Taylor');
- return $factory->make('component');
- }, ['name' => 'Taylor']);
- $factory->slot('title');
- $factory->slot('website', 'laravel.com', []);
- echo 'title<hr>';
- $factory->endSlot();
- echo 'component';
- $contents = $factory->renderComponent();
- $this->assertSame('title<hr> component Taylor laravel.com', $contents);
- }
- public function testComponentHandlingUsingHtmlable()
- {
- $factory = $this->getFactory();
- $factory->startComponent(new HtmlString('laravel.com'));
- $contents = $factory->renderComponent();
- $this->assertSame('laravel.com', $contents);
- }
- public function testTranslation()
- {
- $container = new Container;
- $container->instance('translator', $translator = m::mock(stdClass::class));
- $translator->shouldReceive('get')->with('Foo', ['name' => 'taylor'])->andReturn('Bar');
- $factory = $this->getFactory();
- $factory->setContainer($container);
- $factory->startTranslation(['name' => 'taylor']);
- echo 'Foo';
- $string = $factory->renderTranslation();
- $this->assertSame('Bar', $string);
- }
- public function testSingleStackPush()
- {
- $factory = $this->getFactory();
- $factory->startPush('foo');
- echo 'hi';
- $factory->stopPush();
- $this->assertSame('hi', $factory->yieldPushContent('foo'));
- }
- public function testMultipleStackPush()
- {
- $factory = $this->getFactory();
- $factory->startPush('foo');
- echo 'hi';
- $factory->stopPush();
- $factory->startPush('foo');
- echo ', Hello!';
- $factory->stopPush();
- $this->assertSame('hi, Hello!', $factory->yieldPushContent('foo'));
- }
- public function testSingleStackPrepend()
- {
- $factory = $this->getFactory();
- $factory->startPrepend('foo');
- echo 'hi';
- $factory->stopPrepend();
- $this->assertSame('hi', $factory->yieldPushContent('foo'));
- }
- public function testMultipleStackPrepend()
- {
- $factory = $this->getFactory();
- $factory->startPrepend('foo');
- echo ', Hello!';
- $factory->stopPrepend();
- $factory->startPrepend('foo');
- echo 'hi';
- $factory->stopPrepend();
- $this->assertSame('hi, Hello!', $factory->yieldPushContent('foo'));
- }
- public function testSessionAppending()
- {
- $factory = $this->getFactory();
- $factory->startSection('foo');
- echo 'hi';
- $factory->appendSection();
- $factory->startSection('foo');
- echo 'there';
- $factory->appendSection();
- $this->assertSame('hithere', $factory->yieldContent('foo'));
- }
- public function testYieldSectionStopsAndYields()
- {
- $factory = $this->getFactory();
- $factory->startSection('foo');
- echo 'hi';
- $this->assertSame('hi', $factory->yieldSection());
- }
- public function testInjectStartsSectionWithContent()
- {
- $factory = $this->getFactory();
- $factory->inject('foo', 'hi');
- $this->assertSame('hi', $factory->yieldContent('foo'));
- }
- public function testEmptyStringIsReturnedForNonSections()
- {
- $factory = $this->getFactory();
- $this->assertEmpty($factory->yieldContent('foo'));
- }
- public function testSectionFlushing()
- {
- $factory = $this->getFactory();
- $factory->startSection('foo');
- echo 'hi';
- $factory->stopSection();
- $this->assertCount(1, $factory->getSections());
- $factory->flushSections();
- $this->assertCount(0, $factory->getSections());
- }
- public function testHasSection()
- {
- $factory = $this->getFactory();
- $factory->startSection('foo');
- echo 'hi';
- $factory->stopSection();
- $this->assertTrue($factory->hasSection('foo'));
- $this->assertFalse($factory->hasSection('bar'));
- }
- public function testSectionMissing()
- {
- $factory = $this->getFactory();
- $factory->startSection('foo');
- echo 'hello world';
- $factory->stopSection();
- $this->assertTrue($factory->sectionMissing('bar'));
- $this->assertFalse($factory->sectionMissing('foo'));
- }
- public function testGetSection()
- {
- $factory = $this->getFactory();
- $factory->startSection('foo');
- echo 'hi';
- $factory->stopSection();
- $this->assertSame('hi', $factory->getSection('foo'));
- $this->assertNull($factory->getSection('bar'));
- $this->assertSame('default', $factory->getSection('bar', 'default'));
- }
- public function testMakeWithSlashAndDot()
- {
- $factory = $this->getFactory();
- $factory->getFinder()->shouldReceive('find')->twice()->with('foo.bar')->andReturn('path.php');
- $factory->getEngineResolver()->shouldReceive('resolve')->twice()->with('php')->andReturn(m::mock(Engine::class));
- $factory->getDispatcher()->shouldReceive('dispatch');
- $factory->make('foo/bar');
- $factory->make('foo.bar');
- }
- public function testNamespacedViewNamesAreNormalizedProperly()
- {
- $factory = $this->getFactory();
- $factory->getFinder()->shouldReceive('find')->twice()->with('vendor/package::foo.bar')->andReturn('path.php');
- $factory->getEngineResolver()->shouldReceive('resolve')->twice()->with('php')->andReturn(m::mock(Engine::class));
- $factory->getDispatcher()->shouldReceive('dispatch');
- $factory->make('vendor/package::foo/bar');
- $factory->make('vendor/package::foo.bar');
- }
- public function testExceptionIsThrownForUnknownExtension()
- {
- $this->expectException(InvalidArgumentException::class);
- $factory = $this->getFactory();
- $factory->getFinder()->shouldReceive('find')->once()->with('view')->andReturn('view.foo');
- $factory->make('view');
- }
- public function testExceptionsInSectionsAreThrown()
- {
- $this->expectException(ErrorException::class);
- $this->expectExceptionMessage('section exception message');
- $engine = new CompilerEngine(m::mock(CompilerInterface::class), new Filesystem);
- $engine->getCompiler()->shouldReceive('getCompiledPath')->andReturnUsing(function ($path) {
- return $path;
- });
- $engine->getCompiler()->shouldReceive('isExpired')->twice()->andReturn(false);
- $factory = $this->getFactory();
- $factory->getEngineResolver()->shouldReceive('resolve')->twice()->andReturn($engine);
- $factory->getFinder()->shouldReceive('find')->once()->with('layout')->andReturn(__DIR__.'/fixtures/section-exception-layout.php');
- $factory->getFinder()->shouldReceive('find')->once()->with('view')->andReturn(__DIR__.'/fixtures/section-exception.php');
- $factory->getDispatcher()->shouldReceive('dispatch')->times(4);
- $factory->make('view')->render();
- }
- public function testExtraStopSectionCallThrowsException()
- {
- $this->expectException(InvalidArgumentException::class);
- $this->expectExceptionMessage('Cannot end a section without first starting one.');
- $factory = $this->getFactory();
- $factory->startSection('foo');
- $factory->stopSection();
- $factory->stopSection();
- }
- public function testExtraAppendSectionCallThrowsException()
- {
- $this->expectException(InvalidArgumentException::class);
- $this->expectExceptionMessage('Cannot end a section without first starting one.');
- $factory = $this->getFactory();
- $factory->startSection('foo');
- $factory->stopSection();
- $factory->appendSection();
- }
- public function testAddingLoops()
- {
- $factory = $this->getFactory();
- $factory->addLoop([1, 2, 3]);
- $expectedLoop = [
- 'iteration' => 0,
- 'index' => 0,
- 'remaining' => 3,
- 'count' => 3,
- 'first' => true,
- 'last' => false,
- 'odd' => false,
- 'even' => true,
- 'depth' => 1,
- 'parent' => null,
- ];
- $this->assertEquals([$expectedLoop], $factory->getLoopStack());
- $factory->addLoop([1, 2, 3, 4]);
- $secondExpectedLoop = [
- 'iteration' => 0,
- 'index' => 0,
- 'remaining' => 4,
- 'count' => 4,
- 'first' => true,
- 'last' => false,
- 'odd' => false,
- 'even' => true,
- 'depth' => 2,
- 'parent' => (object) $expectedLoop,
- ];
- $this->assertEquals([$expectedLoop, $secondExpectedLoop], $factory->getLoopStack());
- $factory->popLoop();
- $this->assertEquals([$expectedLoop], $factory->getLoopStack());
- }
- public function testAddingLoopDoesNotCloseGenerator()
- {
- $factory = $this->getFactory();
- $data = (new class
- {
- public function generate()
- {
- for ($count = 0; $count < 3; $count++) {
- yield ['a', 'b'];
- }
- }
- })->generate();
- $factory->addLoop($data);
- foreach ($data as $chunk) {
- $this->assertEquals(['a', 'b'], $chunk);
- }
- }
- public function testAddingUncountableLoop()
- {
- $factory = $this->getFactory();
- $factory->addLoop('');
- $expectedLoop = [
- 'iteration' => 0,
- 'index' => 0,
- 'remaining' => null,
- 'count' => null,
- 'first' => true,
- 'last' => null,
- 'odd' => false,
- 'even' => true,
- 'depth' => 1,
- 'parent' => null,
- ];
- $this->assertEquals([$expectedLoop], $factory->getLoopStack());
- }
- public function testIncrementingLoopIndices()
- {
- $factory = $this->getFactory();
- $factory->addLoop([1, 2, 3, 4]);
- $factory->incrementLoopIndices();
- $this->assertEquals(1, $factory->getLoopStack()[0]['iteration']);
- $this->assertEquals(0, $factory->getLoopStack()[0]['index']);
- $this->assertEquals(3, $factory->getLoopStack()[0]['remaining']);
- $this->assertTrue($factory->getLoopStack()[0]['odd']);
- $this->assertFalse($factory->getLoopStack()[0]['even']);
- $factory->incrementLoopIndices();
- $this->assertEquals(2, $factory->getLoopStack()[0]['iteration']);
- $this->assertEquals(1, $factory->getLoopStack()[0]['index']);
- $this->assertEquals(2, $factory->getLoopStack()[0]['remaining']);
- $this->assertFalse($factory->getLoopStack()[0]['odd']);
- $this->assertTrue($factory->getLoopStack()[0]['even']);
- }
- public function testReachingEndOfLoop()
- {
- $factory = $this->getFactory();
- $factory->addLoop([1, 2]);
- $factory->incrementLoopIndices();
- $factory->incrementLoopIndices();
- $this->assertTrue($factory->getLoopStack()[0]['last']);
- }
- public function testIncrementingLoopIndicesOfUncountable()
- {
- $factory = $this->getFactory();
- $factory->addLoop('');
- $factory->incrementLoopIndices();
- $factory->incrementLoopIndices();
- $this->assertEquals(2, $factory->getLoopStack()[0]['iteration']);
- $this->assertEquals(1, $factory->getLoopStack()[0]['index']);
- $this->assertFalse($factory->getLoopStack()[0]['first']);
- $this->assertNull($factory->getLoopStack()[0]['remaining']);
- $this->assertNull($factory->getLoopStack()[0]['last']);
- }
- public function testMacro()
- {
- $factory = $this->getFactory();
- $factory->macro('getFoo', function () {
- return 'Hello World';
- });
- $this->assertSame('Hello World', $factory->getFoo());
- }
- protected function getFactory()
- {
- return new Factory(
- m::mock(EngineResolver::class),
- m::mock(ViewFinderInterface::class),
- m::mock(DispatcherContract::class)
- );
- }
- protected function getFactoryArgs()
- {
- return [
- m::mock(EngineResolver::class),
- m::mock(ViewFinderInterface::class),
- m::mock(DispatcherContract::class),
- ];
- }
- }
|