@@ -391,6 +391,336 @@ public function testPartialLoopWithInvalidObjectValuesWillRaiseException()
391
391
392
392
$ this ->helper ->__invoke ('partialLoopParentObject.phtml ' , new \stdClass ());
393
393
}
394
+
395
+ /**
396
+ * @return void
397
+ */
398
+ public function testPartialLoopIteratesOverArrayInLoopMethod ()
399
+ {
400
+ $ data = [
401
+ ['message ' => 'foo ' ],
402
+ ['message ' => 'bar ' ],
403
+ ['message ' => 'baz ' ],
404
+ ['message ' => 'bat ' ],
405
+ ];
406
+
407
+ $ view = new View ();
408
+ $ view ->resolver ()->addPath ($ this ->basePath . '/application/views/scripts ' );
409
+ $ this ->helper ->setView ($ view );
410
+
411
+ $ result = $ this ->helper ->loop ('partialLoop.phtml ' , $ data );
412
+ foreach ($ data as $ item ) {
413
+ $ string = 'This is an iteration: ' . $ item ['message ' ];
414
+ $ this ->assertContains ($ string , $ result );
415
+ }
416
+ }
417
+
418
+ /**
419
+ * @return void
420
+ */
421
+ public function testPartialLoopIteratesOverIteratorInLoopMethod ()
422
+ {
423
+ $ data = [
424
+ ['message ' => 'foo ' ],
425
+ ['message ' => 'bar ' ],
426
+ ['message ' => 'baz ' ],
427
+ ['message ' => 'bat ' ]
428
+ ];
429
+ $ o = new IteratorTest ($ data );
430
+
431
+ $ view = new View ();
432
+ $ view ->resolver ()->addPath ($ this ->basePath . '/application/views/scripts ' );
433
+ $ this ->helper ->setView ($ view );
434
+
435
+ $ result = $ this ->helper ->Loop ('partialLoop.phtml ' , $ o );
436
+ foreach ($ data as $ item ) {
437
+ $ string = 'This is an iteration: ' . $ item ['message ' ];
438
+ $ this ->assertContains ($ string , $ result );
439
+ }
440
+ }
441
+
442
+ /**
443
+ * @return void
444
+ */
445
+ public function testPartialLoopIteratesOverRecursiveIteratorInLoopMethod ()
446
+ {
447
+ $ rIterator = new RecursiveIteratorTest ();
448
+ for ($ i = 0 ; $ i < 5 ; ++$ i ) {
449
+ $ data = [
450
+ 'message ' => 'foo ' . $ i ,
451
+ ];
452
+ $ rIterator ->addItem (new IteratorTest ($ data ));
453
+ }
454
+
455
+ $ view = new View ();
456
+ $ view ->resolver ()->addPath ($ this ->basePath . '/application/views/scripts ' );
457
+ $ this ->helper ->setView ($ view );
458
+
459
+ $ result = $ this ->helper ->Loop ('partialLoop.phtml ' , $ rIterator );
460
+ foreach ($ rIterator as $ item ) {
461
+ foreach ($ item as $ key => $ value ) {
462
+ $ this ->assertContains ($ value , $ result , var_export ($ value , 1 ));
463
+ }
464
+ }
465
+ }
466
+
467
+ /**
468
+ * @return void
469
+ */
470
+ public function testPartialLoopThrowsExceptionWithBadIteratorInLoopMethod ()
471
+ {
472
+ $ data = [
473
+ ['message ' => 'foo ' ],
474
+ ['message ' => 'bar ' ],
475
+ ['message ' => 'baz ' ],
476
+ ['message ' => 'bat ' ]
477
+ ];
478
+ $ o = new BogusIteratorTest ($ data );
479
+
480
+ $ view = new View ();
481
+ $ view ->resolver ()->addPath ($ this ->basePath . '/application/views/scripts ' );
482
+ $ this ->helper ->setView ($ view );
483
+
484
+ try {
485
+ $ result = $ this ->helper ->Loop ('partialLoop.phtml ' , $ o );
486
+ $ this ->fail ('PartialLoop should only work with arrays and iterators ' );
487
+ } catch (\Exception $ e ) {
488
+ }
489
+ }
490
+
491
+ /**
492
+ * @return void
493
+ */
494
+ public function testPassingNullDataThrowsExcpetionInLoopMethod ()
495
+ {
496
+ $ view = new View ();
497
+ $ view ->resolver ()->addPath ($ this ->basePath . '/application/views/scripts ' );
498
+ $ this ->helper ->setView ($ view );
499
+
500
+ $ this ->setExpectedException ('Zend\View\Exception\InvalidArgumentException ' );
501
+ $ result = $ this ->helper ->loop ('partialLoop.phtml ' , null );
502
+ }
503
+
504
+ public function testShouldAllowIteratingOverTraversableObjectsInLoopMethod ()
505
+ {
506
+ $ data = [
507
+ ['message ' => 'foo ' ],
508
+ ['message ' => 'bar ' ],
509
+ ['message ' => 'baz ' ],
510
+ ['message ' => 'bat ' ]
511
+ ];
512
+ $ o = new ArrayObject ($ data );
513
+
514
+ $ view = new View ();
515
+ $ view ->resolver ()->addPath ($ this ->basePath . '/application/views/scripts ' );
516
+ $ this ->helper ->setView ($ view );
517
+
518
+ $ result = $ this ->helper ->loop ('partialLoop.phtml ' , $ o );
519
+ foreach ($ data as $ item ) {
520
+ $ string = 'This is an iteration: ' . $ item ['message ' ];
521
+ $ this ->assertContains ($ string , $ result );
522
+ }
523
+ }
524
+
525
+ public function testShouldAllowIteratingOverObjectsImplementingToArrayInLoopMethod ()
526
+ {
527
+ $ data = [
528
+ ['message ' => 'foo ' ],
529
+ ['message ' => 'bar ' ],
530
+ ['message ' => 'baz ' ],
531
+ ['message ' => 'bat ' ]
532
+ ];
533
+ $ o = new ToArrayTest ($ data );
534
+
535
+ $ view = new View ();
536
+ $ view ->resolver ()->addPath ($ this ->basePath . '/application/views/scripts ' );
537
+ $ this ->helper ->setView ($ view );
538
+
539
+ $ result = $ this ->helper ->loop ('partialLoop.phtml ' , $ o );
540
+ foreach ($ data as $ item ) {
541
+ $ string = 'This is an iteration: ' . $ item ['message ' ];
542
+ $ this ->assertContains ($ string , $ result , $ result );
543
+ }
544
+ }
545
+
546
+ /**
547
+ * @group ZF-3350
548
+ * @group ZF-3352
549
+ */
550
+ public function testShouldNotCastToArrayIfObjectIsTraversableInLoopMethod ()
551
+ {
552
+ $ data = [
553
+ new IteratorWithToArrayTestContainer (['message ' => 'foo ' ]),
554
+ new IteratorWithToArrayTestContainer (['message ' => 'bar ' ]),
555
+ new IteratorWithToArrayTestContainer (['message ' => 'baz ' ]),
556
+ new IteratorWithToArrayTestContainer (['message ' => 'bat ' ]),
557
+ ];
558
+ $ o = new IteratorWithToArrayTest ($ data );
559
+
560
+ $ view = new View ();
561
+ $ view ->resolver ()->addPath ($ this ->basePath . '/application/views/scripts ' );
562
+ $ this ->helper ->setView ($ view );
563
+ $ this ->helper ->setObjectKey ('obj ' );
564
+
565
+ $ result = $ this ->helper ->loop ('partialLoopObject.phtml ' , $ o );
566
+ foreach ($ data as $ item ) {
567
+ $ string = 'This is an iteration: ' . $ item ->message ;
568
+ $ this ->assertContains ($ string , $ result , $ result );
569
+ }
570
+ }
571
+
572
+ /**
573
+ * @group ZF-3083
574
+ */
575
+ public function testEmptyArrayPassedToPartialLoopShouldNotThrowExceptionInLoopMethod ()
576
+ {
577
+ $ view = new View ();
578
+ $ view ->resolver ()->addPath ($ this ->basePath . '/application/views/scripts ' );
579
+ $ this ->helper ->setView ($ view );
580
+
581
+ $ this ->helper ->loop ('partialLoop.phtml ' , []);
582
+ }
583
+
584
+ /**
585
+ * @group ZF-2737
586
+ */
587
+ public function testPartialLoopIncrementsPartialCounterInLoopMethod ()
588
+ {
589
+ $ data = [
590
+ ['message ' => 'foo ' ],
591
+ ['message ' => 'bar ' ],
592
+ ['message ' => 'baz ' ],
593
+ ['message ' => 'bat ' ]
594
+ ];
595
+
596
+ $ view = new View ();
597
+ $ view ->resolver ()->addPath ($ this ->basePath . '/application/views/scripts ' );
598
+ $ this ->helper ->setView ($ view );
599
+
600
+ $ this ->helper ->loop ('partialLoopCouter.phtml ' , $ data );
601
+ $ this ->assertEquals (4 , $ this ->helper ->getPartialCounter ());
602
+ }
603
+
604
+ /**
605
+ * @group ZF-5174
606
+ */
607
+ public function testPartialLoopPartialCounterResetsInLoopMethod ()
608
+ {
609
+ $ data = [
610
+ ['message ' => 'foo ' ],
611
+ ['message ' => 'bar ' ],
612
+ ['message ' => 'baz ' ],
613
+ ['message ' => 'bat ' ]
614
+ ];
615
+
616
+ $ view = new View ();
617
+ $ view ->resolver ()->addPath ($ this ->basePath . '/application/views/scripts ' );
618
+ $ this ->helper ->setView ($ view );
619
+
620
+ $ this ->helper ->loop ('partialLoopCouter.phtml ' , $ data );
621
+ $ this ->assertEquals (4 , $ this ->helper ->getPartialCounter ());
622
+
623
+ $ this ->helper ->loop ('partialLoopCouter.phtml ' , $ data );
624
+ $ this ->assertEquals (4 , $ this ->helper ->getPartialCounter ());
625
+ }
626
+
627
+ public function testShouldNotConvertToArrayRecursivelyIfModelIsTraversableInLoopMethod ()
628
+ {
629
+ $ rIterator = new RecursiveIteratorTest ();
630
+ for ($ i = 0 ; $ i < 5 ; ++$ i ) {
631
+ $ data = [
632
+ 'message ' => 'foo ' . $ i ,
633
+ ];
634
+ $ rIterator ->addItem (new IteratorTest ($ data ));
635
+ }
636
+
637
+ $ view = new View ();
638
+ $ view ->resolver ()->addPath ($ this ->basePath . '/application/views/scripts ' );
639
+ $ this ->helper ->setView ($ view );
640
+ $ this ->helper ->setObjectKey ('obj ' );
641
+
642
+ $ result = $ this ->helper ->loop ('partialLoopShouldNotConvertToArrayRecursively.phtml ' , $ rIterator );
643
+
644
+ foreach ($ rIterator as $ item ) {
645
+ foreach ($ item as $ key => $ value ) {
646
+ $ this ->assertContains ('This is an iteration: ' . $ value , $ result , var_export ($ value , 1 ));
647
+ }
648
+ }
649
+ }
650
+
651
+ /**
652
+ * @group 7093
653
+ */
654
+ public function testNestedCallsShouldNotOverrideObjectKeyInLoopMethod ()
655
+ {
656
+ $ data = [];
657
+ for ($ i = 0 ; $ i < 3 ; $ i ++) {
658
+ $ obj = new \stdClass ();
659
+ $ obj ->helper = $ this ->helper ;
660
+ $ obj ->objectKey = "foo " . $ i ;
661
+ $ obj ->message = "bar " ;
662
+ $ obj ->data = [
663
+ $ obj
664
+ ];
665
+ $ data [] = $ obj ;
666
+ }
667
+
668
+ $ view = new View ();
669
+ $ view ->resolver ()->addPath ($ this ->basePath . '/application/views/scripts ' );
670
+ $ this ->helper ->setView ($ view );
671
+
672
+ $ this ->helper ->setObjectKey ('obj ' );
673
+ $ result = $ this ->helper ->loop ('partialLoopParentObject.phtml ' , $ data );
674
+
675
+ foreach ($ data as $ item ) {
676
+ $ string = 'This is an iteration with objectKey: ' . $ item ->objectKey ;
677
+ $ this ->assertContains ($ string , $ result , $ result );
678
+ }
679
+ }
680
+
681
+ /**
682
+ * @group 7450
683
+ */
684
+ public function testNestedPartialLoopsNestedArrayInLoopMethod ()
685
+ {
686
+ $ data = [[
687
+ 'obj ' => [
688
+ 'helper ' => $ this ->helper ,
689
+ 'message ' => 'foo1 ' ,
690
+ 'data ' => [[
691
+ 'message ' => 'foo2 '
692
+ ]]
693
+ ]
694
+ ]];
695
+
696
+ $ view = new View ();
697
+ $ view ->resolver ()->addPath ($ this ->basePath . '/application/views/scripts ' );
698
+ $ this ->helper ->setView ($ view );
699
+
700
+ $ result = $ this ->helper ->loop ('partialLoopParentObject.phtml ' , $ data );
701
+ $ this ->assertContains ('foo1 ' , $ result , $ result );
702
+ $ this ->assertContains ('foo2 ' , $ result , $ result );
703
+ }
704
+
705
+ public function testPartialLoopWithInvalidValuesWillRaiseExceptionInLoopMethod ()
706
+ {
707
+ $ this ->setExpectedException (
708
+ 'Zend\View\Exception\InvalidArgumentException ' ,
709
+ 'PartialLoop helper requires iterable data, string given '
710
+ );
711
+
712
+ $ this ->helper ->loop ('partialLoopParentObject.phtml ' , 'foo ' );
713
+ }
714
+
715
+ public function testPartialLoopWithInvalidObjectValuesWillRaiseExceptionInLoopMethod ()
716
+ {
717
+ $ this ->setExpectedException (
718
+ 'Zend\View\Exception\InvalidArgumentException ' ,
719
+ 'PartialLoop helper requires iterable data, stdClass given '
720
+ );
721
+
722
+ $ this ->helper ->loop ('partialLoopParentObject.phtml ' , new \stdClass ());
723
+ }
394
724
}
395
725
396
726
class IteratorTest implements Iterator
0 commit comments