Security update for Core, with self-updated composer
[yaffs-website] / vendor / psy / psysh / test / Psy / Test / CodeCleaner / FunctionReturnInWriteContextPassTest.php
1 <?php
2
3 /*
4  * This file is part of Psy Shell.
5  *
6  * (c) 2012-2017 Justin Hileman
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11
12 namespace Psy\Test\CodeCleaner;
13
14 use PhpParser\NodeTraverser;
15 use Psy\CodeCleaner\FunctionReturnInWriteContextPass;
16 use Psy\Exception\FatalErrorException;
17
18 class FunctionReturnInWriteContextPassTest extends CodeCleanerTestCase
19 {
20     public function setUp()
21     {
22         $this->pass      = new FunctionReturnInWriteContextPass();
23         $this->traverser = new NodeTraverser();
24         $this->traverser->addVisitor($this->pass);
25     }
26
27     /**
28      * @dataProvider invalidStatements
29      * @expectedException \Psy\Exception\FatalErrorException
30      * @expectedExceptionMessage Can't use function return value in write context
31      */
32     public function testProcessStatementFails($code)
33     {
34         $stmts = $this->parse($code);
35         $this->traverser->traverse($stmts);
36     }
37
38     public function invalidStatements()
39     {
40         return array(
41             array('f(&g())'),
42             array('array(& $object->method())'),
43             array('$a->method(& $closure())'),
44             array('array(& A::b())'),
45             array('f() = 5'),
46             array('unset(h())'),
47         );
48     }
49
50     public function testIsset()
51     {
52         try {
53             $this->traverser->traverse($this->parse('isset(strtolower("A"))'));
54             $this->fail();
55         } catch (FatalErrorException $e) {
56             if (version_compare(PHP_VERSION, '5.5', '>=')) {
57                 $this->assertContains(
58                     'Cannot use isset() on the result of a function call (you can use "null !== func()" instead)',
59                     $e->getMessage()
60                 );
61             } else {
62                 $this->assertContains("Can't use function return value in write context", $e->getMessage());
63             }
64         }
65     }
66
67     /**
68      * @expectedException \Psy\Exception\FatalErrorException
69      * @expectedExceptionMessage Can't use function return value in write context
70      */
71     public function testEmpty()
72     {
73         if (version_compare(PHP_VERSION, '5.5', '>=')) {
74             $this->markTestSkipped();
75         }
76
77         $this->traverser->traverse($this->parse('empty(strtolower("A"))'));
78     }
79 }