|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * Test: Nette\Routing\Route public API methods |
| 5 | + */ |
| 6 | + |
| 7 | +declare(strict_types=1); |
| 8 | + |
| 9 | +use Nette\Routing\Route; |
| 10 | +use Tester\Assert; |
| 11 | + |
| 12 | + |
| 13 | +require __DIR__ . '/../bootstrap.php'; |
| 14 | + |
| 15 | + |
| 16 | +test('getMask() returns original mask string', function () { |
| 17 | + $route = new Route('<presenter>/<action>/<id>'); |
| 18 | + Assert::same('<presenter>/<action>/<id>', $route->getMask()); |
| 19 | + |
| 20 | + $route = new Route('//example.com/<path>'); |
| 21 | + Assert::same('//example.com/<path>', $route->getMask()); |
| 22 | + |
| 23 | + $route = new Route('index[.html]'); |
| 24 | + Assert::same('index[.html]', $route->getMask()); |
| 25 | +}); |
| 26 | + |
| 27 | + |
| 28 | +test('getMask() with complex patterns', function () { |
| 29 | + $mask = '<presenter>/<action>[/<id \d+>][/<slug [-a-z]+>]'; |
| 30 | + $route = new Route($mask); |
| 31 | + Assert::same($mask, $route->getMask()); |
| 32 | +}); |
| 33 | + |
| 34 | + |
| 35 | +test('getMask() with query parameters', function () { |
| 36 | + $mask = '<presenter> ? id=<id> & cat=<categoryId>'; |
| 37 | + $route = new Route($mask); |
| 38 | + Assert::same($mask, $route->getMask()); |
| 39 | +}); |
| 40 | + |
| 41 | + |
| 42 | +test('getMask() with host patterns', function () { |
| 43 | + $mask = '//%domain%/<presenter>/<action>'; |
| 44 | + $route = new Route($mask); |
| 45 | + Assert::same($mask, $route->getMask()); |
| 46 | +}); |
| 47 | + |
| 48 | + |
| 49 | +test('getDefaults() returns all parameters with default values', function () { |
| 50 | + $route = new Route('<presenter>/<action>', [ |
| 51 | + 'action' => 'default', |
| 52 | + 'id' => null, |
| 53 | + ]); |
| 54 | + |
| 55 | + Assert::same(['action' => 'default', 'id' => null], $route->getDefaults()); |
| 56 | +}); |
| 57 | + |
| 58 | + |
| 59 | +test('getDefaults() with various default types - converts to strings', function () { |
| 60 | + $route = new Route('<presenter>/<action>/<page>', [ |
| 61 | + 'action' => 'view', |
| 62 | + 'page' => 1, |
| 63 | + 'lang' => 'en', |
| 64 | + 'debug' => false, |
| 65 | + ]); |
| 66 | + |
| 67 | + Assert::same([ |
| 68 | + 'action' => 'view', |
| 69 | + 'page' => '1', |
| 70 | + 'lang' => 'en', |
| 71 | + 'debug' => '0', |
| 72 | + ], $route->getDefaults()); |
| 73 | +}); |
| 74 | + |
| 75 | + |
| 76 | +test('getDefaults() excludes parameters without defaults', function () { |
| 77 | + $route = new Route('<presenter>/<action>', [ |
| 78 | + 'action' => 'default', |
| 79 | + ]); |
| 80 | + |
| 81 | + $defaults = $route->getDefaults(); |
| 82 | + Assert::same(['action' => 'default'], $defaults); |
| 83 | + Assert::false(array_key_exists('presenter', $defaults)); |
| 84 | +}); |
| 85 | + |
| 86 | + |
| 87 | +test('getConstantParameters() returns only fixed constant values', function () { |
| 88 | + $route = new Route('api/<version>', [ |
| 89 | + 'module' => 'Api', |
| 90 | + 'version' => 'v1', |
| 91 | + ]); |
| 92 | + |
| 93 | + Assert::same(['module' => 'Api'], $route->getConstantParameters()); |
| 94 | +}); |
| 95 | + |
| 96 | + |
| 97 | +test('getConstantParameters() with multiple constants', function () { |
| 98 | + $route = new Route('blog/<slug>', [ |
| 99 | + 'presenter' => 'Blog', |
| 100 | + 'action' => 'detail', |
| 101 | + 'module' => 'Front', |
| 102 | + ]); |
| 103 | + |
| 104 | + Assert::same([ |
| 105 | + 'presenter' => 'Blog', |
| 106 | + 'action' => 'detail', |
| 107 | + 'module' => 'Front', |
| 108 | + ], $route->getConstantParameters()); |
| 109 | +}); |
| 110 | + |
| 111 | + |
| 112 | +test('getConstantParameters() excludes variable parameters', function () { |
| 113 | + $route = new Route('<presenter>/<action=default>/<id>', [ |
| 114 | + 'action' => 'default', |
| 115 | + 'module' => 'Front', |
| 116 | + ]); |
| 117 | + |
| 118 | + $constants = $route->getConstantParameters(); |
| 119 | + Assert::same(['module' => 'Front'], $constants); |
| 120 | + Assert::false(array_key_exists('action', $constants)); |
| 121 | + Assert::false(array_key_exists('presenter', $constants)); |
| 122 | + Assert::false(array_key_exists('id', $constants)); |
| 123 | +}); |
| 124 | + |
| 125 | + |
| 126 | +test('getConstantParameters() with empty result', function () { |
| 127 | + $route = new Route('<presenter>/<action>', [ |
| 128 | + 'action' => 'default', |
| 129 | + ]); |
| 130 | + |
| 131 | + Assert::same([], $route->getConstantParameters()); |
| 132 | +}); |
| 133 | + |
| 134 | + |
| 135 | +test('API methods work together consistently', function () { |
| 136 | + $route = new Route('<presenter>/<action=default>/<id>', [ |
| 137 | + 'action' => 'default', |
| 138 | + 'module' => 'Admin', |
| 139 | + 'secure' => true, |
| 140 | + ]); |
| 141 | + |
| 142 | + Assert::same('<presenter>/<action=default>/<id>', $route->getMask()); |
| 143 | + |
| 144 | + Assert::same([ |
| 145 | + 'action' => 'default', |
| 146 | + 'module' => 'Admin', |
| 147 | + 'secure' => '1', |
| 148 | + ], $route->getDefaults()); |
| 149 | + |
| 150 | + Assert::same([ |
| 151 | + 'module' => 'Admin', |
| 152 | + 'secure' => '1', |
| 153 | + ], $route->getConstantParameters()); |
| 154 | +}); |
0 commit comments