blob: 84c01f896def7f53349474b2493f83e857872270 [file] [log] [blame]
Jens Geyer577f4072014-07-23 19:04:12 +02001<?php
2/*
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License. You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing,
14 * software distributed under the License is distributed on an
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 * KIND, either express or implied. See the License for the
17 * specific language governing permissions and limitations
18 * under the License.
19 */
20
Robert Lubfba3702017-11-03 12:27:31 +080021namespace test\Thrift;
Jens Geyer577f4072014-07-23 19:04:12 +020022
Robert Lubfba3702017-11-03 12:27:31 +080023require_once __DIR__.'/../../../vendor/autoload.php';
Jens Geyer577f4072014-07-23 19:04:12 +020024
25use Thrift\ClassLoader\ThriftClassLoader;
26use Thrift\Exception\TProtocolException;
27use Thrift\Protocol\TBinaryProtocol;
28use Thrift\Transport\TMemoryBuffer;
29
30$oop_mode = (isset($argv[1]) && $argv[1] === '-oop');
31$GEN_DIR = $oop_mode ? 'phpvo' : 'phpv';
32
33$loader = new ThriftClassLoader();
Robert Lubfba3702017-11-03 12:27:31 +080034$loader->registerDefinition('ThriftTest', __DIR__ . '/packages/' . $GEN_DIR);
35$loader->registerDefinition('TestValidators', __DIR__ . '/packages/' . $GEN_DIR);
Jens Geyer577f4072014-07-23 19:04:12 +020036$loader->register();
37
38// Would be nice to have PHPUnit here, but for now just hack it.
39
Roger Thomas6fb59232014-11-04 10:09:23 +000040set_exception_handler(function ($e) {
Jens Geyer577f4072014-07-23 19:04:12 +020041 my_assert(false, "Unexpected exception caught: " . $e->getMessage());
42});
43
Roger Thomas6fb59232014-11-04 10:09:23 +000044set_error_handler(function ($errno, $errmsg) {
Jens Geyer577f4072014-07-23 19:04:12 +020045 my_assert(false, "Unexpected PHP error: " . $errmsg);
46});
47
48// Empty structs should not have validators
49assert_has_no_read_validator('ThriftTest\EmptyStruct');
50assert_has_no_write_validator('ThriftTest\EmptyStruct');
51
52// Bonk has only opt_in_req_out fields
53{
54 assert_has_no_read_validator('ThriftTest\Bonk');
55 assert_has_a_write_validator('ThriftTest\Bonk');
56 {
57 // Check that we can read an empty object
58 $bonk = new \ThriftTest\Bonk();
59 $transport = new TMemoryBuffer("\000");
60 $protocol = new TBinaryProtocol($transport);
61 $bonk->read($protocol);
62 }
63 {
64 // ...but not write an empty object
65 $bonk = new \ThriftTest\Bonk();
66 $transport = new TMemoryBuffer();
67 $protocol = new TBinaryProtocol($transport);
Roger Thomas6fb59232014-11-04 10:09:23 +000068 assert_protocol_exception_thrown(function () use ($bonk, $protocol) { $bonk->write($protocol); },
Jens Geyer577f4072014-07-23 19:04:12 +020069 'Bonk was able to write an empty object');
70 }
71}
72
73// StructA has a single required field
74{
75 assert_has_a_read_validator('ThriftTest\StructA');
76 assert_has_a_write_validator('ThriftTest\StructA');
77 {
78 // Check that we are not able to write StructA with a missing required field
79 $structa = new \ThriftTest\StructA();
80 $transport = new TMemoryBuffer();
81 $protocol = new TBinaryProtocol($transport);
Roger Thomas6fb59232014-11-04 10:09:23 +000082 assert_protocol_exception_thrown(function () use ($structa, $protocol) { $structa->write($protocol); },
Jens Geyer577f4072014-07-23 19:04:12 +020083 'StructA was able to write an empty object');
84 }
85 {
86 // Check that we are able to read and write a message with a good StructA
87 $transport = new TMemoryBuffer(base64_decode('CwABAAAAA2FiYwA='));
88 $protocol = new TBinaryProtocol($transport);
89 $structa = new \ThriftTest\StructA();
90 $structa->read($protocol);
91 $structa->write($protocol);
92 }
93}
94
95// Unions should not get write validators
96assert_has_no_write_validator('TestValidators\UnionOfStrings');
97
Roger Meierdb8751b2014-09-01 21:58:07 +020098// Service _result classes should not get any validators
99assert_has_no_read_validator('TestValidators\TestService_test_result');
100assert_has_no_write_validator('TestValidators\TestService_test_result');
101
Roger Thomas6fb59232014-11-04 10:09:23 +0000102function assert_has_a_read_validator($class)
103{
Jens Geyer577f4072014-07-23 19:04:12 +0200104 my_assert(has_read_validator_method($class),
105 $class . ' class should have a read validator');
106}
107
Roger Thomas6fb59232014-11-04 10:09:23 +0000108function assert_has_no_read_validator($class)
109{
Jens Geyer577f4072014-07-23 19:04:12 +0200110 my_assert(!has_read_validator_method($class),
111 $class . ' class should not have a read validator');
112}
113
Roger Thomas6fb59232014-11-04 10:09:23 +0000114function assert_has_a_write_validator($class)
115{
Jens Geyer577f4072014-07-23 19:04:12 +0200116 my_assert(has_write_validator_method($class),
117 $class . ' class should have a write validator');
118}
119
Roger Thomas6fb59232014-11-04 10:09:23 +0000120function assert_has_no_write_validator($class)
121{
Jens Geyer577f4072014-07-23 19:04:12 +0200122 my_assert(!has_write_validator_method($class),
123 $class . ' class should not have a write validator');
124}
125
Roger Thomas6fb59232014-11-04 10:09:23 +0000126function assert_protocol_exception_thrown($callable, $message)
127{
Jens Geyer577f4072014-07-23 19:04:12 +0200128 try {
129 call_user_func($callable);
130 my_assert(false, $message);
131 } catch (TProtocolException $e) {
132 }
133}
134
Roger Thomas6fb59232014-11-04 10:09:23 +0000135function has_write_validator_method($class)
136{
Jens Geyer577f4072014-07-23 19:04:12 +0200137 $rc = new \ReflectionClass($class);
Roger Thomas6fb59232014-11-04 10:09:23 +0000138
Jens Geyer577f4072014-07-23 19:04:12 +0200139 return $rc->hasMethod('_validateForWrite');
140}
141
Roger Thomas6fb59232014-11-04 10:09:23 +0000142function has_read_validator_method($class)
143{
Jens Geyer577f4072014-07-23 19:04:12 +0200144 $rc = new \ReflectionClass($class);
Roger Thomas6fb59232014-11-04 10:09:23 +0000145
Jens Geyer577f4072014-07-23 19:04:12 +0200146 return $rc->hasMethod('_validateForRead');
147}
148
Roger Thomas6fb59232014-11-04 10:09:23 +0000149function my_assert($something, $message)
150{
Jens Geyer577f4072014-07-23 19:04:12 +0200151 if (!$something) {
152 fwrite(STDERR, basename(__FILE__) . " FAILED: $message\n");
153 exit(1);
154 }
155}