419369b496334921cc65a3f93b6c41443b07dd3a
[yaffs-website] / vendor / psy / psysh / test / CodeCleaner / FunctionReturnInWriteContextPassTest.php
1 <?php
2
3 /*
4  * This file is part of Psy Shell.
5  *
6  * (c) 2012-2018 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 Psy\CodeCleaner\FunctionReturnInWriteContextPass;
15 use Psy\Exception\FatalErrorException;
16
17 class FunctionReturnInWriteContextPassTest extends CodeCleanerTestCase
18 {
19     public function setUp()
20     {
21         $this->setPass(new FunctionReturnInWriteContextPass());
22     }
23
24     /**
25      * @dataProvider invalidStatements
26      * @expectedException \Psy\Exception\FatalErrorException
27      * @expectedExceptionMessage Can't use function return value in write context
28      */
29     public function testProcessStatementFails($code)
30     {
31         $this->parseAndTraverse($code);
32     }
33
34     public function invalidStatements()
35     {
36         return [
37             ['f(&g())'],
38             ['array(& $object->method())'],
39             ['$a->method(& $closure())'],
40             ['array(& A::b())'],
41             ['f() = 5'],
42             ['unset(h())'],
43         ];
44     }
45
46     public function testIsset()
47     {
48         try {
49             $this->traverser->traverse($this->parse('isset(strtolower("A"))'));
50             $this->fail();
51         } catch (FatalErrorException $e) {
52             if (version_compare(PHP_VERSION, '5.5', '>=')) {
53                 $this->assertContains(
54                     'Cannot use isset() on the result of a function call (you can use "null !== func()" instead)',
55                     $e->getMessage()
56                 );
57             } else {
58                 $this->assertContains("Can't use function return value in write context", $e->getMessage());
59             }
60         }
61     }
62
63     /**
64      * @expectedException \Psy\Exception\FatalErrorException
65      * @expectedExceptionMessage Can't use function return value in write context
66      */
67     public function testEmpty()
68     {
69         if (version_compare(PHP_VERSION, '5.5', '>=')) {
70             $this->markTestSkipped();
71         }
72
73         $this->traverser->traverse($this->parse('empty(strtolower("A"))'));
74     }
75 }