@@ -554,6 +554,146 @@ public function testBarePhp()
554554 ], true ), $ items );
555555 }
556556
557+ /**
558+ * @dataProvider foreachProvider
559+ */
560+ public function testForeach (Position $ position , array $ expectedItems )
561+ {
562+ $ completionUri = pathToUri (__DIR__ . '/../../../fixtures/completion/foreach.php ' );
563+ $ this ->loader ->open ($ completionUri , file_get_contents ($ completionUri ));
564+ $ items = $ this ->textDocument ->completion (
565+ new TextDocumentIdentifier ($ completionUri ),
566+ $ position
567+ )->wait ();
568+ $ this ->assertCompletionsListSubset (new CompletionList ($ expectedItems , true ), $ items );
569+ }
570+
571+ public function foreachProvider (): array
572+ {
573+ return [
574+ 'foreach value ' => [
575+ new Position (18 , 6 ),
576+ [
577+ new CompletionItem (
578+ '$value ' ,
579+ CompletionItemKind::VARIABLE ,
580+ '\\Foo \\Bar ' ,
581+ null ,
582+ null ,
583+ null ,
584+ null ,
585+ new TextEdit (new Range (new Position (18 , 6 ), new Position (18 , 6 )), 'alue ' )
586+ ),
587+ ]
588+ ],
589+ 'foreach value resolved ' => [
590+ new Position (19 , 12 ),
591+ [
592+ new CompletionItem (
593+ 'foo ' ,
594+ CompletionItemKind::PROPERTY ,
595+ 'mixed '
596+ ),
597+ new CompletionItem (
598+ 'test ' ,
599+ CompletionItemKind::METHOD ,
600+ '\\Foo \\Bar[] '
601+ ),
602+ ]
603+ ],
604+ 'array creation with multiple objects ' => [
605+ new Position (23 , 5 ),
606+ [
607+ new CompletionItem (
608+ '$value ' ,
609+ CompletionItemKind::VARIABLE ,
610+ '\\Foo \\Bar| \\stdClass ' ,
611+ null ,
612+ null ,
613+ null ,
614+ null ,
615+ new TextEdit (new Range (new Position (23 , 5 ), new Position (23 , 5 )), 'value ' )
616+ ),
617+ new CompletionItem (
618+ '$key ' ,
619+ CompletionItemKind::VARIABLE ,
620+ 'int ' ,
621+ null ,
622+ null ,
623+ null ,
624+ null ,
625+ new TextEdit (new Range (new Position (23 , 5 ), new Position (23 , 5 )), 'key ' )
626+ ),
627+ ]
628+ ],
629+ 'array creation with string/int keys and object values ' => [
630+ new Position (27 , 5 ),
631+ [
632+ new CompletionItem (
633+ '$value ' ,
634+ CompletionItemKind::VARIABLE ,
635+ '\\Foo \\Bar ' ,
636+ null ,
637+ null ,
638+ null ,
639+ null ,
640+ new TextEdit (new Range (new Position (27 , 5 ), new Position (27 , 5 )), 'value ' )
641+ ),
642+ new CompletionItem (
643+ '$key ' ,
644+ CompletionItemKind::VARIABLE ,
645+ 'string|int ' ,
646+ null ,
647+ null ,
648+ null ,
649+ null ,
650+ new TextEdit (new Range (new Position (27 , 5 ), new Position (27 , 5 )), 'key ' )
651+ ),
652+ ]
653+ ],
654+ 'array creation with only string keys ' => [
655+ new Position (31 , 5 ),
656+ [
657+ new CompletionItem (
658+ '$value ' ,
659+ CompletionItemKind::VARIABLE ,
660+ '\\Foo \\Bar ' ,
661+ null ,
662+ null ,
663+ null ,
664+ null ,
665+ new TextEdit (new Range (new Position (31 , 5 ), new Position (31 , 5 )), 'value ' )
666+ ),
667+ new CompletionItem (
668+ '$key ' ,
669+ CompletionItemKind::VARIABLE ,
670+ 'string ' ,
671+ null ,
672+ null ,
673+ null ,
674+ null ,
675+ new TextEdit (new Range (new Position (31 , 5 ), new Position (31 , 5 )), 'key ' )
676+ ),
677+ ]
678+ ],
679+ 'foreach function call ' => [
680+ new Position (35 , 5 ),
681+ [
682+ new CompletionItem (
683+ '$value ' ,
684+ CompletionItemKind::VARIABLE ,
685+ '\\Foo \\Bar ' ,
686+ null ,
687+ null ,
688+ null ,
689+ null ,
690+ new TextEdit (new Range (new Position (35 , 5 ), new Position (35 , 5 )), 'value ' )
691+ ),
692+ ]
693+ ],
694+ ];
695+ }
696+
557697 public function testMethodReturnType ()
558698 {
559699 $ completionUri = pathToUri (__DIR__ . '/../../../fixtures/completion/method_return_type.php ' );
0 commit comments