Debian Codegraph @

lib/php/test/Integration/Lib/ClassLoader/ThriftClassLoaderTest.php

raw
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\Integration\Lib\ClassLoader;
23
24 use phpmock\phpunit\PHPMock;
25 use PHPUnit\Framework\TestCase;
26 use Thrift\ClassLoader\ThriftClassLoader;
27
28 /***
29 * This test depends on running the compiler against the ./Resources/ThriftTest.thrift file:
30 * lib/php/test$ ../../../compiler/cpp/thrift --gen php:classmap,server,rest,nsglobal="Classmap" -r --out ./Resources/packages/phpcm ./Resources/ThriftTest.thrift
31 */
32 class ThriftClassLoaderTest extends TestCase
33 {
34 use PHPMock;
35
36 /**
37 * @dataProvider registerDefinitionDataProvider
38 */
39 public function testRegisterDefinition(
40 $definitions,
41 $class,
42 $checkInterfaceExist = false,
43 $useApcu = false,
44 $apcuPrefix = null
45 ) {
46 $this->getFunctionMock('Thrift\ClassLoader', 'apcu_fetch')
47 ->expects($useApcu ? $this->any() : $this->never())
48 ->with($apcuPrefix . $class)
49 ->willReturn(false);
50
51 $this->getFunctionMock('Thrift\ClassLoader', 'apcu_store')
52 ->expects($useApcu ? $this->any() : $this->never())
53 ->with($apcuPrefix . $class, $this->anything())
54 ->willReturn(true);
55
56 $loader = new ThriftClassLoader($useApcu, $apcuPrefix);
57 foreach ($definitions as $namespace => $paths) {
58 $loader->registerDefinition($namespace, $paths);
59 }
60 $loader->register();
61
62 $loader->loadClass($class);
63 if ($checkInterfaceExist) {
64 $this->assertTrue(interface_exists($class, false), "->loadClass() loads '$class'");
65 } else {
66 $this->assertTrue(class_exists($class, false), "->loadClass() loads '$class'");
67 }
68 }
69
70 public function registerDefinitionDataProvider()
71 {
72 yield 'loadType' => [
73 'definitions' => [
74 'Classmap' => __DIR__ . '/../../../Resources/packages/phpcm',
75 ],
76 'class' => 'Classmap\ThriftTest\Xtruct',
77 ];
78 yield 'loadInterface' => [
79 'definitions' => [
80 'Classmap' => __DIR__ . '/../../../Resources/packages/phpcm',
81 ],
82 'class' => '\Classmap\ThriftTest\ThriftTestIf',
83 'checkInterfaceExist' => true,
84 ];
85 yield 'loadClient' => [
86 'definitions' => [
87 'Classmap' => __DIR__ . '/../../../Resources/packages/phpcm',
88 ],
89 'class' => '\Classmap\ThriftTest\ThriftTestClient',
90 ];
91 yield 'loadProcessor' => [
92 'definitions' => [
93 'Classmap' => __DIR__ . '/../../../Resources/packages/phpcm',
94 ],
95 'class' => '\Classmap\ThriftTest\ThriftTestProcessor',
96 ];
97 yield 'loadRest' => [
98 'definitions' => [
99 'Classmap' => __DIR__ . '/../../../Resources/packages/phpcm',
100 ],
101 'class' => '\Classmap\ThriftTest\ThriftTestRest',
102 ];
103 yield 'load_args' => [
104 'definitions' => [
105 'Classmap' => __DIR__ . '/../../../Resources/packages/phpcm',
106 ],
107 'class' => '\Classmap\ThriftTest\ThriftTest_testVoid_args',
108 ];
109 yield 'load_result' => [
110 'definitions' => [
111 'Classmap' => __DIR__ . '/../../../Resources/packages/phpcm',
112 ],
113 'class' => '\Classmap\ThriftTest\ThriftTest_testVoid_result',
114 ];
115 yield 'pathAsArray' => [
116 'definitions' => [
117 'Classmap' => [__DIR__ . '/../../../Resources/packages/phpcm'],
118 ],
119 'class' => 'Classmap\ThriftTest\Xtruct',
120 ];
121 yield 'severalDefinitions' => [
122 'definitions' => [
123 'Classmap\ThriftTest' => [__DIR__ . '/../../../Resources/packages/phpcm'],
124 'Classmap\TestValidators' => [__DIR__ . '/../../../Resources/packages/phpcm'],
125 ],
126 'class' => '\Classmap\TestValidators\TestServiceClient',
127 ];
128 yield 'useApcu' => [
129 'definitions' => [
130 'Classmap\ThriftTest' => [__DIR__ . '/../../../Resources/packages/phpcm'],
131 'Classmap\TestValidators' => [__DIR__ . '/../../../Resources/packages/phpcm'],
132 ],
133 'class' => '\Classmap\TestValidators\TestServiceClient',
134 'checkInterfaceExist' => false,
135 'useApcu' => true,
136 'apcuPrefix' => 'APCU_PREFIX',
137 ];
138 }
139 }