|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <[email protected]> |
| 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 Symfony\Component\Cache\Tests\Traits; |
| 13 | + |
| 14 | +use PHPUnit\Framework\TestCase; |
| 15 | +use Symfony\Component\VarExporter\LazyProxyTrait; |
| 16 | +use Symfony\Component\VarExporter\ProxyHelper; |
| 17 | + |
| 18 | +/** |
| 19 | + * @requires extension redis |
| 20 | + */ |
| 21 | +class RedisProxiesTest extends TestCase |
| 22 | +{ |
| 23 | + /** |
| 24 | + * @testWith ["Redis"] |
| 25 | + * ["RedisCluster"] |
| 26 | + */ |
| 27 | + public function testRedis5Proxy($class) |
| 28 | + { |
| 29 | + $proxy = file_get_contents(\dirname(__DIR__)."/Traits/{$class}5Proxy.php"); |
| 30 | + $proxy = substr($proxy, 0, 8 + strpos($proxy, "\n ];")); |
| 31 | + $methods = []; |
| 32 | + |
| 33 | + foreach ((new \ReflectionClass($class))->getMethods() as $method) { |
| 34 | + if ('reset' === $method->name || method_exists(LazyProxyTrait::class, $method->name)) { |
| 35 | + continue; |
| 36 | + } |
| 37 | + $return = $method->getReturnType() instanceof \ReflectionNamedType && 'void' === (string) $method->getReturnType() ? '' : 'return '; |
| 38 | + $methods[] = "\n ".ProxyHelper::exportSignature($method, false)."\n".<<<EOPHP |
| 39 | + { |
| 40 | + {$return}\$this->lazyObjectReal->{$method->name}(...\\func_get_args()); |
| 41 | + } |
| 42 | +
|
| 43 | + EOPHP; |
| 44 | + } |
| 45 | + |
| 46 | + uksort($methods, 'strnatcmp'); |
| 47 | + $proxy .= implode('', $methods)."}\n"; |
| 48 | + |
| 49 | + $this->assertStringEqualsFile(\dirname(__DIR__)."/Traits/{$class}5Proxy.php", $proxy); |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * @testWith ["Redis", "redis"] |
| 54 | + * ["RedisCluster", "redis_cluster"] |
| 55 | + */ |
| 56 | + public function testRedis6Proxy($class, $stub) |
| 57 | + { |
| 58 | + $stub = file_get_contents("https://raw.githubusercontent.com/phpredis/phpredis/develop/{$stub}.stub.php"); |
| 59 | + $stub = preg_replace('/^class /m', 'return; \0', $stub); |
| 60 | + $stub = preg_replace('/^return; class ([a-zA-Z]++)/m', 'interface \1StubInterface', $stub, 1); |
| 61 | + eval(substr($stub, 5)); |
| 62 | + |
| 63 | + $proxy = file_get_contents(\dirname(__DIR__)."/Traits/{$class}6Proxy.php"); |
| 64 | + $proxy = substr($proxy, 0, 8 + strpos($proxy, "\n ];")); |
| 65 | + $methods = []; |
| 66 | + |
| 67 | + foreach ((new \ReflectionClass($class.'StubInterface'))->getMethods() as $method) { |
| 68 | + if ('reset' === $method->name || method_exists(LazyProxyTrait::class, $method->name)) { |
| 69 | + continue; |
| 70 | + } |
| 71 | + $return = $method->getReturnType() instanceof \ReflectionNamedType && 'void' === (string) $method->getReturnType() ? '' : 'return '; |
| 72 | + $methods[] = "\n ".ProxyHelper::exportSignature($method, false)."\n".<<<EOPHP |
| 73 | + { |
| 74 | + {$return}\$this->lazyObjectReal->{$method->name}(...\\func_get_args()); |
| 75 | + } |
| 76 | +
|
| 77 | + EOPHP; |
| 78 | + } |
| 79 | + |
| 80 | + uksort($methods, 'strnatcmp'); |
| 81 | + $proxy .= implode('', $methods)."}\n"; |
| 82 | + |
| 83 | + $this->assertStringEqualsFile(\dirname(__DIR__)."/Traits/{$class}6Proxy.php", $proxy); |
| 84 | + } |
| 85 | +} |
0 commit comments