File: Php85Test.php

package info (click to toggle)
php-symfony-polyfill 1.33.0-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 8,520 kB
  • sloc: php: 127,232; makefile: 69
file content (133 lines) | stat: -rw-r--r-- 3,802 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
<?php

/*
 * This file is part of the Symfony package.
 *
 * (c) Fabien Potencier <fabien@symfony.com>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Symfony\Polyfill\Tests\Php85;

use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;

class Php85Test extends TestCase
{
    /**
    * @dataProvider provideHandler
    */
    #[DataProvider('provideHandler')]
    public function testGetErrorHandler($expected, $handler): void
    {
        set_error_handler($handler);
        try {
            $result = get_error_handler();
        } finally {
            restore_error_handler();
        }

        $this->assertSame($expected, $result);
    }

    public function testErrorStableReturnValue(): void
    {
        $this->assertSame(get_error_handler(), get_error_handler());
    }

    /**
    * @dataProvider provideHandler
    */
    #[DataProvider('provideHandler')]
    public function testGetExceptionHandler($expected, $handler): void
    {
        set_exception_handler($handler);
        try {
            $result = get_exception_handler();
        } finally {
            restore_exception_handler();
        }

        $this->assertSame($expected, $result);
    }

    public function testExceptionStableReturnValue(): void
    {
        $this->assertSame(get_exception_handler(), get_exception_handler());

    }

    public static function provideHandler()
    {
        // String handler
        yield ['var_dump', 'var_dump'];

        // Null handler
        yield [null, null];

        // Static method array handler
        yield [[TestHandler::class, 'handleStatic'], [TestHandler::class, 'handleStatic']];

        // Static method string handler
        yield ['Symfony\Polyfill\Tests\Php85\TestHandler::handleStatic', 'Symfony\Polyfill\Tests\Php85\TestHandler::handleStatic'];

        // Instance method array
        $handler = new TestHandler();
        yield [[$handler, 'handle'], [$handler, 'handle']];

        // Invokable object
        $handler = new TestHandlerInvokable();
        yield [$handler, $handler];
    }

    public function testArrayFirstArrayLast()
    {
        $this->assertNull(array_first([]));
        $this->assertNull(array_last([]));

        $array = [1, 2, 3];
        unset($array[0], $array[1], $array[2]);

        $this->assertNull(array_first([]));
        $this->assertNull(array_last([]));


        $this->assertSame('single element', array_first(['single element']));
        $this->assertSame('single element', array_last(['single element']));

        $str = 'hello world';
        $this->assertSame($str, array_first([&$str, 1]));
        $this->assertSame(1, array_last([&$str, 1]));

        $this->assertSame(1, array_first([1, &$str]));
        $this->assertSame($str, array_last([1, &$str]));

        $this->assertSame(1, array_first([1 => 1, 0 => 0, 3 => 3, 2 => 2]));
        $this->assertSame(2, array_last([1 => 1, 0 => 0, 3 => 3, 2 => 2]));

        $this->assertSame('a1', array_first(['a' => 'a1', 'b' => 'b1', 'c' => 'c1']));
        $this->assertSame('c1', array_last(['a' => 'a1', 'b' => 'b1', 'c' => 'c1']));

        $this->assertSame([], array_first([100 => []]));
        $this->assertSame([], array_last([100 => []]));

        $this->assertEquals(new \stdClass(), array_first([new \stdClass, false]));
        $this->assertFalse(array_last([new \stdClass, false]));

        $this->assertTrue(array_first([true, new \stdClass]));
        $this->assertEquals(new \stdClass(), array_last([true, new \stdClass]));
    }
}

class TestHandler
{
    public static function handleStatic() {}
    public function handle() {}
}

class TestHandlerInvokable
{
    public function __invoke() {}
}