Yaffs site version 1.1
[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         );
47     }
48
49     public function testIsset()
50     {
51         try {
52             $this->traverser->traverse($this->parse('isset(strtolower("A"))'));
53             $this->fail();
54         } catch (FatalErrorException $e) {
55             if (version_compare(PHP_VERSION, '5.5', '>=')) {
56                 $this->assertContains(
57                     'Cannot use isset() on the result of a function call (you can use "null !== func()" instead)',
58                     $e->getMessage()
59                 );
60             } else {
61                 $this->assertContains("Can't use function return value in write context", $e->getMessage());
62             }
63         }
64     }
65
66     /**
67      * @expectedException \Psy\Exception\FatalErrorException
68      * @expectedExceptionMessage Can't use function return value in write context
69      */
70     public function testEmpty()
71     {
72         if (version_compare(PHP_VERSION, '5.5', '>=')) {
73             $this->markTestSkipped();
74         }
75
76         $this->traverser->traverse($this->parse('empty(strtolower("A"))'));
77     }
78 }