| 1 |
<?php |
| 2 |
|
| 3 |
/* |
| 4 |
* Licensed to the Apache Software Foundation (ASF) under one |
| 5 |
* or more contributor license agreements. See the NOTICE file |
| 6 |
* distributed with this work for additional information |
| 7 |
* regarding copyright ownership. The ASF licenses this file |
| 8 |
* to you under the Apache License, Version 2.0 (the |
| 9 |
* "License"); you may not use this file except in compliance |
| 10 |
* with the License. You may obtain a copy of the License at |
| 11 |
* |
| 12 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 13 |
* |
| 14 |
* Unless required by applicable law or agreed to in writing, |
| 15 |
* software distributed under the License is distributed on an |
| 16 |
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 17 |
* KIND, either express or implied. See the License for the |
| 18 |
* specific language governing permissions and limitations |
| 19 |
* under the License. |
| 20 |
*/ |
| 21 |
|
| 22 |
namespace Test\Thrift\Unit\Lib\ClassLoader; |
| 23 |
|
| 24 |
use phpmock\phpunit\PHPMock; |
| 25 |
use PHPUnit\Framework\TestCase; |
| 26 |
use Thrift\ClassLoader\ThriftClassLoader; |
| 27 |
|
| 28 |
class ThriftClassLoaderTest extends TestCase |
| 29 |
{ |
| 30 |
use PHPMock; |
| 31 |
|
| 32 |
/** |
| 33 |
* @dataProvider registerNamespaceDataProvider |
| 34 |
*/ |
| 35 |
public function testRegisterNamespace( |
| 36 |
$namespaces, |
| 37 |
$class, |
| 38 |
$isClassExist = true, |
| 39 |
$useApcu = false, |
| 40 |
$apcuPrefix = null |
| 41 |
) { |
| 42 |
$this->getFunctionMock('Thrift\ClassLoader', 'apcu_fetch') |
| 43 |
->expects($useApcu ? $this->once() : $this->never()) |
| 44 |
->with($apcuPrefix . $class) |
| 45 |
->willReturn(false); |
| 46 |
|
| 47 |
$this->getFunctionMock('Thrift\ClassLoader', 'apcu_store') |
| 48 |
->expects($useApcu ? $this->once() : $this->never()) |
| 49 |
->with($apcuPrefix . $class, $this->anything()) |
| 50 |
->willReturn(true); |
| 51 |
|
| 52 |
$loader = new ThriftClassLoader($useApcu, $apcuPrefix); |
| 53 |
foreach ($namespaces as $namespace => $paths) { |
| 54 |
$loader->registerNamespace($namespace, $paths); |
| 55 |
} |
| 56 |
$loader->register(); |
| 57 |
$loader->loadClass($class); |
| 58 |
if ($isClassExist) { |
| 59 |
$this->assertTrue(class_exists($class, false), "->loadClass() loads '$class'"); |
| 60 |
} else { |
| 61 |
$this->assertFalse(class_exists($class, false), "->loadClass() loads '$class'"); |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
public function registerNamespaceDataProvider() |
| 66 |
{ |
| 67 |
yield 'default' => [ |
| 68 |
'namespaces' => [ |
| 69 |
'A' => __DIR__ . '/Fixtures', |
| 70 |
], |
| 71 |
'class' => 'A\TestClass', |
| 72 |
]; |
| 73 |
yield 'missedClass' => [ |
| 74 |
'namespaces' => [ |
| 75 |
'A' => __DIR__ . '/Fixtures', |
| 76 |
], |
| 77 |
'class' => 'A\MissedClass', |
| 78 |
'isClassExist' => false, |
| 79 |
]; |
| 80 |
yield 'pathAsArray' => [ |
| 81 |
'namespaces' => [ |
| 82 |
'B' => [__DIR__ . '/Fixtures'], |
| 83 |
], |
| 84 |
'class' => 'B\TestClass', |
| 85 |
]; |
| 86 |
yield 'loadClassWithSlash' => [ |
| 87 |
'namespaces' => [ |
| 88 |
'C' => __DIR__ . '/Fixtures', |
| 89 |
], |
| 90 |
'class' => '\C\TestClass', |
| 91 |
]; |
| 92 |
yield 'severalNamespaces' => [ |
| 93 |
'namespaces' => [ |
| 94 |
'D' => __DIR__ . '/Fixtures', |
| 95 |
'E' => __DIR__ . '/Fixtures', |
| 96 |
], |
| 97 |
'class' => '\E\TestClass', |
| 98 |
]; |
| 99 |
yield 'useApcu' => [ |
| 100 |
'namespaces' => [ |
| 101 |
'D' => __DIR__ . '/Fixtures', |
| 102 |
'E' => __DIR__ . '/Fixtures', |
| 103 |
], |
| 104 |
'class' => '\E\TestClass', |
| 105 |
'isClassExist' => true, |
| 106 |
'useApcu' => true, |
| 107 |
'apcuPrefix' => 'APCU_PREFIX', |
| 108 |
]; |
| 109 |
} |
| 110 |
} |