|
| 1 | +<?php declare(strict_types=1); |
| 2 | + |
| 3 | +/** |
| 4 | + * Test: Nette\PhpGenerator\Dumper context validation |
| 5 | + */ |
| 6 | + |
| 7 | +use Nette\PhpGenerator\DumpContext; |
| 8 | +use Nette\PhpGenerator\Dumper; |
| 9 | +use Tester\Assert; |
| 10 | + |
| 11 | +require __DIR__ . '/../bootstrap.php'; |
| 12 | + |
| 13 | + |
| 14 | +// Constant context (class constant, property default) |
| 15 | + |
| 16 | +test('Constant context rejects stdClass', function () { |
| 17 | + $dumper = new Dumper; |
| 18 | + $dumper->context = DumpContext::Constant; |
| 19 | + Assert::exception( |
| 20 | + fn() => $dumper->dump((object) ['a' => 1]), |
| 21 | + Nette\InvalidStateException::class, |
| 22 | + '%a% stdClass %a% Constant %a%', |
| 23 | + ); |
| 24 | +}); |
| 25 | + |
| 26 | + |
| 27 | +test('Constant context rejects DateTime', function () { |
| 28 | + $dumper = new Dumper; |
| 29 | + $dumper->context = DumpContext::Constant; |
| 30 | + Assert::exception( |
| 31 | + fn() => $dumper->dump(new DateTime('2024-01-01')), |
| 32 | + Nette\InvalidStateException::class, |
| 33 | + '%a% DateTime %a% Constant %a%', |
| 34 | + ); |
| 35 | +}); |
| 36 | + |
| 37 | + |
| 38 | +test('Constant context rejects custom objects', function () { |
| 39 | + $dumper = new Dumper; |
| 40 | + $dumper->context = DumpContext::Constant; |
| 41 | + Assert::exception( |
| 42 | + fn() => $dumper->dump(new ArrayObject), |
| 43 | + Nette\InvalidStateException::class, |
| 44 | + '%a% ArrayObject %a% Constant %a%', |
| 45 | + ); |
| 46 | +}); |
| 47 | + |
| 48 | + |
| 49 | +test('Constant context allows first-class callable', function () { |
| 50 | + $dumper = new Dumper; |
| 51 | + $dumper->context = DumpContext::Constant; |
| 52 | + Assert::contains('(...)', $dumper->dump(strlen(...))); |
| 53 | +}); |
| 54 | + |
| 55 | + |
| 56 | +// Property context (same restrictions as Constant) |
| 57 | + |
| 58 | +test('Property context rejects stdClass', function () { |
| 59 | + $dumper = new Dumper; |
| 60 | + $dumper->context = DumpContext::Property; |
| 61 | + Assert::exception( |
| 62 | + fn() => $dumper->dump((object) ['a' => 1]), |
| 63 | + Nette\InvalidStateException::class, |
| 64 | + '%a% Property %a%', |
| 65 | + ); |
| 66 | +}); |
| 67 | + |
| 68 | + |
| 69 | +test('Property context rejects DateTime', function () { |
| 70 | + $dumper = new Dumper; |
| 71 | + $dumper->context = DumpContext::Property; |
| 72 | + Assert::exception( |
| 73 | + fn() => $dumper->dump(new DateTime('2024-01-01')), |
| 74 | + Nette\InvalidStateException::class, |
| 75 | + '%a% Property %a%', |
| 76 | + ); |
| 77 | +}); |
| 78 | + |
| 79 | + |
| 80 | +test('Property context rejects custom objects', function () { |
| 81 | + $dumper = new Dumper; |
| 82 | + $dumper->context = DumpContext::Property; |
| 83 | + Assert::exception( |
| 84 | + fn() => $dumper->dump(new ArrayObject), |
| 85 | + Nette\InvalidStateException::class, |
| 86 | + '%a% Property %a%', |
| 87 | + ); |
| 88 | +}); |
| 89 | + |
| 90 | + |
| 91 | +// Parameter context (allows new, casts) |
| 92 | + |
| 93 | +test('Parameter context allows stdClass', function () { |
| 94 | + $dumper = new Dumper; |
| 95 | + $dumper->context = DumpContext::Parameter; |
| 96 | + Assert::contains('(object)', $dumper->dump((object) ['a' => 1])); |
| 97 | +}); |
| 98 | + |
| 99 | + |
| 100 | +test('Parameter context allows DateTime', function () { |
| 101 | + $dumper = new Dumper; |
| 102 | + $dumper->context = DumpContext::Parameter; |
| 103 | + Assert::contains('new \DateTime', $dumper->dump(new DateTime('2024-01-01'))); |
| 104 | +}); |
| 105 | + |
| 106 | + |
| 107 | +test('Parameter context rejects custom objects', function () { |
| 108 | + $dumper = new Dumper; |
| 109 | + $dumper->context = DumpContext::Parameter; |
| 110 | + Assert::exception( |
| 111 | + fn() => $dumper->dump(new ArrayObject), |
| 112 | + Nette\InvalidStateException::class, |
| 113 | + '%a% ArrayObject %a% Parameter %a%', |
| 114 | + ); |
| 115 | +}); |
| 116 | + |
| 117 | + |
| 118 | +// Attribute context (same as Parameter) |
| 119 | + |
| 120 | +test('Attribute context allows stdClass', function () { |
| 121 | + $dumper = new Dumper; |
| 122 | + $dumper->context = DumpContext::Attribute; |
| 123 | + Assert::contains('(object)', $dumper->dump((object) ['a' => 1])); |
| 124 | +}); |
| 125 | + |
| 126 | + |
| 127 | +test('Attribute context allows DateTime', function () { |
| 128 | + $dumper = new Dumper; |
| 129 | + $dumper->context = DumpContext::Attribute; |
| 130 | + Assert::contains('new \DateTime', $dumper->dump(new DateTime('2024-01-01'))); |
| 131 | +}); |
| 132 | + |
| 133 | + |
| 134 | +test('Attribute context rejects custom objects', function () { |
| 135 | + $dumper = new Dumper; |
| 136 | + $dumper->context = DumpContext::Attribute; |
| 137 | + Assert::exception( |
| 138 | + fn() => $dumper->dump(new ArrayObject), |
| 139 | + Nette\InvalidStateException::class, |
| 140 | + '%a% ArrayObject %a% Attribute %a%', |
| 141 | + ); |
| 142 | +}); |
| 143 | + |
| 144 | + |
| 145 | +// Expression context (no restrictions, default) |
| 146 | + |
| 147 | +test('Expression context allows everything', function () { |
| 148 | + $dumper = new Dumper; |
| 149 | + Assert::same(DumpContext::Expression, $dumper->context); |
| 150 | + Assert::contains('(object)', $dumper->dump((object) ['a' => 1])); |
| 151 | + Assert::contains('new \DateTime', $dumper->dump(new DateTime('2024-01-01'))); |
| 152 | + Assert::contains('createObject', $dumper->dump(new ArrayObject)); |
| 153 | +}); |
| 154 | + |
| 155 | + |
| 156 | +// Propagation through arrays |
| 157 | + |
| 158 | +test('context propagates into array elements', function () { |
| 159 | + $dumper = new Dumper; |
| 160 | + $dumper->context = DumpContext::Constant; |
| 161 | + Assert::exception( |
| 162 | + fn() => $dumper->dump(['key' => (object) ['a' => 1]]), |
| 163 | + Nette\InvalidStateException::class, |
| 164 | + '%a% stdClass %a% Constant %a%', |
| 165 | + ); |
| 166 | +}); |
0 commit comments