1<?php
2
3/*
4 * This file is part of Twig.
5 *
6 * (c) Fabien Potencier
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
12use Twig\Environment;
13use Twig\Extension\ExtensionInterface;
14
15class CustomExtensionTest extends \PHPUnit\Framework\TestCase
16{
17    /**
18     * @requires PHP 5.3
19     * @dataProvider provideInvalidExtensions
20     */
21    public function testGetInvalidOperators(ExtensionInterface $extension, $expectedExceptionMessage)
22    {
23        if (method_exists($this, 'expectException')) {
24            $this->expectException('InvalidArgumentException');
25            $this->expectExceptionMessage($expectedExceptionMessage);
26        } else {
27            $this->setExpectedException('InvalidArgumentException', $expectedExceptionMessage);
28        }
29
30        $env = new Environment($this->getMockBuilder('\Twig\Loader\LoaderInterface')->getMock());
31        $env->addExtension($extension);
32        $env->getUnaryOperators();
33    }
34
35    public function provideInvalidExtensions()
36    {
37        return [
38            [new InvalidOperatorExtension(new \stdClass()), '"InvalidOperatorExtension::getOperators()" must return an array with operators, got "stdClass".'],
39            [new InvalidOperatorExtension([1, 2, 3]), '"InvalidOperatorExtension::getOperators()" must return an array of 2 elements, got 3.'],
40        ];
41    }
42}
43
44class InvalidOperatorExtension implements ExtensionInterface
45{
46    private $operators;
47
48    public function __construct($operators)
49    {
50        $this->operators = $operators;
51    }
52
53    public function initRuntime(Environment $environment)
54    {
55    }
56
57    public function getTokenParsers()
58    {
59        return [];
60    }
61
62    public function getNodeVisitors()
63    {
64        return [];
65    }
66
67    public function getFilters()
68    {
69        return [];
70    }
71
72    public function getTests()
73    {
74        return [];
75    }
76
77    public function getFunctions()
78    {
79        return [];
80    }
81
82    public function getGlobals()
83    {
84        return [];
85    }
86
87    public function getOperators()
88    {
89        return $this->operators;
90    }
91
92    public function getName()
93    {
94        return __CLASS__;
95    }
96}
97