|
652 | 652 | ['handle' => 'alpha', 'ok' => true], |
653 | 653 | ['handle' => 'delta', 'ok' => true], |
654 | 654 | ]; |
655 | | - |
656 | 655 | expect($this->filters->invoke($this->context, 'where', $input, ['ok', true]))->toBe($expectation); |
657 | 656 | expect($this->filters->invoke($this->context, 'where', $input, ['ok']))->toBe($expectation); |
| 657 | + |
| 658 | + $template = "{{ array | where: 'ok' | map: 'handle' | join: ' ' }}"; |
| 659 | + assertTemplateResult('alpha delta', $template, ['array' => $input]); |
| 660 | +}); |
| 661 | + |
| 662 | +test('where with value', function () { |
| 663 | + $input = [ |
| 664 | + ['handle' => 'alpha', 'ok' => true], |
| 665 | + ['handle' => 'beta', 'ok' => false], |
| 666 | + ['handle' => 'gamma', 'ok' => false], |
| 667 | + ['handle' => 'delta', 'ok' => true], |
| 668 | + ]; |
| 669 | + |
| 670 | + $template = "{{ array | where: 'ok', true | map: 'handle' | join: ' ' }}"; |
| 671 | + assertTemplateResult('alpha delta', $template, ['array' => $input]); |
| 672 | +}); |
| 673 | + |
| 674 | +test('where with false value', function () { |
| 675 | + $input = [ |
| 676 | + ['handle' => 'alpha', 'ok' => true], |
| 677 | + ['handle' => 'beta', 'ok' => false], |
| 678 | + ['handle' => 'gamma', 'ok' => false], |
| 679 | + ['handle' => 'delta', 'ok' => true], |
| 680 | + ]; |
| 681 | + |
| 682 | + $template = "{{ array | where: 'ok', false | map: 'handle' | join: ' ' }}"; |
| 683 | + assertTemplateResult('beta gamma', $template, ['array' => $input]); |
658 | 684 | }); |
659 | 685 |
|
660 | 686 | test('where string keys', function () { |
|
774 | 800 |
|
775 | 801 | expect($t->value)->toBe(1); |
776 | 802 | }); |
| 803 | + |
| 804 | +test('find with value', function () { |
| 805 | + $products = [ |
| 806 | + ['title' => 'Pro goggles', 'price' => 1299], |
| 807 | + ['title' => 'Thermal gloves', 'price' => 1499], |
| 808 | + ['title' => 'Alpine jacket', 'price' => 3999], |
| 809 | + ['title' => 'Mountain boots', 'price' => 3899], |
| 810 | + ['title' => 'Safety helmet', 'price' => 1999], |
| 811 | + ]; |
| 812 | + |
| 813 | + $template = <<<'LIQUID' |
| 814 | + {%- assign product = products | find: 'price', 3999 -%} |
| 815 | + {{- product.title -}} |
| 816 | + LIQUID; |
| 817 | + |
| 818 | + assertTemplateResult('Alpine jacket', $template, ['products' => $products]); |
| 819 | +}); |
| 820 | + |
| 821 | +test('find on empty array', function () { |
| 822 | + expect($this->filters->invoke($this->context, 'find', [], ['foo', 'bar']))->toBeNull(); |
| 823 | + |
| 824 | + $template = <<<'LIQUID' |
| 825 | + {%- assign product = products | find: 'title.content', 'Not found' -%} |
| 826 | + {%- unless product -%} |
| 827 | + Product not found. |
| 828 | + {%- endunless -%} |
| 829 | + LIQUID; |
| 830 | + |
| 831 | + assertTemplateResult('Product not found.', $template, ['products' => []]); |
| 832 | +}); |
| 833 | + |
| 834 | +test('find index with value', function () { |
| 835 | + $products = [ |
| 836 | + ['title' => 'Pro goggles', 'price' => 1299], |
| 837 | + ['title' => 'Thermal gloves', 'price' => 1499], |
| 838 | + ['title' => 'Alpine jacket', 'price' => 3999], |
| 839 | + ['title' => 'Mountain boots', 'price' => 3899], |
| 840 | + ['title' => 'Safety helmet', 'price' => 1999], |
| 841 | + ]; |
| 842 | + |
| 843 | + $template = <<<'LIQUID' |
| 844 | + {%- assign index = products | find_index: 'price', 3999 -%} |
| 845 | + {{- index -}} |
| 846 | + LIQUID; |
| 847 | + |
| 848 | + assertTemplateResult('2', $template, ['products' => $products]); |
| 849 | +}); |
| 850 | + |
| 851 | +test('find index on empty array', function () { |
| 852 | + expect($this->filters->invoke($this->context, 'find_index', [], ['foo', 'bar']))->toBeNull(); |
| 853 | + |
| 854 | + $template = <<<'LIQUID' |
| 855 | + {%- assign index = products | find_index: 'title.content', 'Not found' -%} |
| 856 | + {%- unless index -%} |
| 857 | + Index not found. |
| 858 | + {%- endunless -%} |
| 859 | + LIQUID; |
| 860 | + |
| 861 | + assertTemplateResult('Index not found.', $template, ['products' => []]); |
| 862 | +}); |
| 863 | + |
| 864 | +test('has', function () { |
| 865 | + $input = [ |
| 866 | + ['handle' => 'alpha', 'ok' => true], |
| 867 | + ['handle' => 'beta', 'ok' => false], |
| 868 | + ['handle' => 'gamma', 'ok' => false], |
| 869 | + ['handle' => 'delta', 'ok' => true], |
| 870 | + ]; |
| 871 | + |
| 872 | + assertTemplateResult( |
| 873 | + 'true', |
| 874 | + '{{ array | has: "ok" }}', |
| 875 | + ['array' => $input], |
| 876 | + ); |
| 877 | + |
| 878 | + assertTemplateResult( |
| 879 | + 'true', |
| 880 | + '{{ array | has: "ok", true }}', |
| 881 | + ['array' => $input], |
| 882 | + ); |
| 883 | +}); |
| 884 | + |
| 885 | +test('has when does not have it', function () { |
| 886 | + $input = [ |
| 887 | + ['handle' => 'alpha', 'ok' => false], |
| 888 | + ['handle' => 'beta', 'ok' => false], |
| 889 | + ['handle' => 'gamma', 'ok' => false], |
| 890 | + ['handle' => 'delta', 'ok' => false], |
| 891 | + ]; |
| 892 | + |
| 893 | + assertTemplateResult( |
| 894 | + 'false', |
| 895 | + '{{ array | has: "ok" }}', |
| 896 | + ['array' => $input], |
| 897 | + ); |
| 898 | + |
| 899 | + assertTemplateResult( |
| 900 | + 'false', |
| 901 | + '{{ array | has: "ok", true }}', |
| 902 | + ['array' => $input], |
| 903 | + ); |
| 904 | +}); |
| 905 | + |
| 906 | +test('has on empty array', function () { |
| 907 | + expect($this->filters->invoke($this->context, 'has', [], ['foo', 'bar']))->toBe(false); |
| 908 | + |
| 909 | + $template = <<<'LIQUID' |
| 910 | + {%- assign has_product = products | has: 'title.content', 'Not found' -%} |
| 911 | + {%- unless has_product -%} |
| 912 | + Product not found. |
| 913 | + {%- endunless -%} |
| 914 | + LIQUID; |
| 915 | + assertTemplateResult('Product not found.', $template, ['products' => []]); |
| 916 | +}); |
| 917 | + |
| 918 | +test('has with false value', function () { |
| 919 | + $input = [ |
| 920 | + ['handle' => 'alpha', 'ok' => true], |
| 921 | + ['handle' => 'beta', 'ok' => false], |
| 922 | + ['handle' => 'gamma', 'ok' => false], |
| 923 | + ['handle' => 'delta', 'ok' => true], |
| 924 | + ]; |
| 925 | + |
| 926 | + assertTemplateResult( |
| 927 | + 'true', |
| 928 | + '{{ array | has: "ok", false }}', |
| 929 | + ['array' => $input], |
| 930 | + ); |
| 931 | +}); |
| 932 | + |
| 933 | +test('has with false value when does not have it', function () { |
| 934 | + $input = [ |
| 935 | + ['handle' => 'alpha', 'ok' => true], |
| 936 | + ['handle' => 'beta', 'ok' => true], |
| 937 | + ['handle' => 'gamma', 'ok' => true], |
| 938 | + ['handle' => 'delta', 'ok' => true], |
| 939 | + ]; |
| 940 | + |
| 941 | + assertTemplateResult( |
| 942 | + 'false', |
| 943 | + '{{ array | has: "ok", false }}', |
| 944 | + ['array' => $input], |
| 945 | + ); |
| 946 | +}); |
| 947 | + |
| 948 | +test('join calls to liquid on each element', function () { |
| 949 | + $drop = new class implements \Keepsuit\Liquid\Contracts\MapsToLiquid |
| 950 | + { |
| 951 | + public function toLiquid(): string |
| 952 | + { |
| 953 | + return 'i did it'; |
| 954 | + } |
| 955 | + }; |
| 956 | + |
| 957 | + expect($this->filters->invoke($this->context, 'join', [$drop, $drop], [', ']))->toBe('i did it, i did it'); |
| 958 | +}); |
| 959 | + |
| 960 | +test('reject', function () { |
| 961 | + $input = [ |
| 962 | + ['handle' => 'alpha', 'ok' => true], |
| 963 | + ['handle' => 'beta', 'ok' => false], |
| 964 | + ['handle' => 'gamma', 'ok' => false], |
| 965 | + ['handle' => 'delta', 'ok' => true], |
| 966 | + ]; |
| 967 | + |
| 968 | + assertTemplateResult( |
| 969 | + 'beta gamma', |
| 970 | + '{{ array | reject: "ok" | map: "handle" | join: " " }}', |
| 971 | + ['array' => $input], |
| 972 | + ); |
| 973 | +}); |
| 974 | + |
| 975 | +test('reject with value', function () { |
| 976 | + $input = [ |
| 977 | + ['handle' => 'alpha', 'ok' => true], |
| 978 | + ['handle' => 'beta', 'ok' => false], |
| 979 | + ['handle' => 'gamma', 'ok' => false], |
| 980 | + ['handle' => 'delta', 'ok' => true], |
| 981 | + ]; |
| 982 | + |
| 983 | + assertTemplateResult( |
| 984 | + 'beta gamma', |
| 985 | + '{{ array | reject: "ok", true | map: "handle" | join: " " }}', |
| 986 | + ['array' => $input], |
| 987 | + ); |
| 988 | +}); |
| 989 | + |
| 990 | +test('reject with false value', function () { |
| 991 | + $input = [ |
| 992 | + ['handle' => 'alpha', 'ok' => true], |
| 993 | + ['handle' => 'beta', 'ok' => false], |
| 994 | + ['handle' => 'gamma', 'ok' => false], |
| 995 | + ['handle' => 'delta', 'ok' => true], |
| 996 | + ]; |
| 997 | + |
| 998 | + assertTemplateResult( |
| 999 | + 'alpha delta', |
| 1000 | + '{{ array | reject: "ok", false | map: "handle" | join: " " }}', |
| 1001 | + ['array' => $input], |
| 1002 | + ); |
| 1003 | +}); |
0 commit comments