Jens Geyer | 577f407 | 2014-07-23 19:04:12 +0200 | [diff] [blame] | 1 | <?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 Lu | bfba370 | 2017-11-03 12:27:31 +0800 | [diff] [blame^] | 21 | namespace test\Thrift; |
Jens Geyer | 577f407 | 2014-07-23 19:04:12 +0200 | [diff] [blame] | 22 | |
Robert Lu | bfba370 | 2017-11-03 12:27:31 +0800 | [diff] [blame^] | 23 | require_once __DIR__.'/../../../vendor/autoload.php'; |
Jens Geyer | 577f407 | 2014-07-23 19:04:12 +0200 | [diff] [blame] | 24 | |
| 25 | use Thrift\ClassLoader\ThriftClassLoader; |
| 26 | use Thrift\Exception\TProtocolException; |
| 27 | use Thrift\Protocol\TBinaryProtocol; |
| 28 | use 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 Lu | bfba370 | 2017-11-03 12:27:31 +0800 | [diff] [blame^] | 34 | $loader->registerDefinition('ThriftTest', __DIR__ . '/packages/' . $GEN_DIR); |
| 35 | $loader->registerDefinition('TestValidators', __DIR__ . '/packages/' . $GEN_DIR); |
Jens Geyer | 577f407 | 2014-07-23 19:04:12 +0200 | [diff] [blame] | 36 | $loader->register(); |
| 37 | |
| 38 | // Would be nice to have PHPUnit here, but for now just hack it. |
| 39 | |
Roger Thomas | 6fb5923 | 2014-11-04 10:09:23 +0000 | [diff] [blame] | 40 | set_exception_handler(function ($e) { |
Jens Geyer | 577f407 | 2014-07-23 19:04:12 +0200 | [diff] [blame] | 41 | my_assert(false, "Unexpected exception caught: " . $e->getMessage()); |
| 42 | }); |
| 43 | |
Roger Thomas | 6fb5923 | 2014-11-04 10:09:23 +0000 | [diff] [blame] | 44 | set_error_handler(function ($errno, $errmsg) { |
Jens Geyer | 577f407 | 2014-07-23 19:04:12 +0200 | [diff] [blame] | 45 | my_assert(false, "Unexpected PHP error: " . $errmsg); |
| 46 | }); |
| 47 | |
| 48 | // Empty structs should not have validators |
| 49 | assert_has_no_read_validator('ThriftTest\EmptyStruct'); |
| 50 | assert_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 Thomas | 6fb5923 | 2014-11-04 10:09:23 +0000 | [diff] [blame] | 68 | assert_protocol_exception_thrown(function () use ($bonk, $protocol) { $bonk->write($protocol); }, |
Jens Geyer | 577f407 | 2014-07-23 19:04:12 +0200 | [diff] [blame] | 69 | '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 Thomas | 6fb5923 | 2014-11-04 10:09:23 +0000 | [diff] [blame] | 82 | assert_protocol_exception_thrown(function () use ($structa, $protocol) { $structa->write($protocol); }, |
Jens Geyer | 577f407 | 2014-07-23 19:04:12 +0200 | [diff] [blame] | 83 | '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 |
| 96 | assert_has_no_write_validator('TestValidators\UnionOfStrings'); |
| 97 | |
Roger Meier | db8751b | 2014-09-01 21:58:07 +0200 | [diff] [blame] | 98 | // Service _result classes should not get any validators |
| 99 | assert_has_no_read_validator('TestValidators\TestService_test_result'); |
| 100 | assert_has_no_write_validator('TestValidators\TestService_test_result'); |
| 101 | |
Roger Thomas | 6fb5923 | 2014-11-04 10:09:23 +0000 | [diff] [blame] | 102 | function assert_has_a_read_validator($class) |
| 103 | { |
Jens Geyer | 577f407 | 2014-07-23 19:04:12 +0200 | [diff] [blame] | 104 | my_assert(has_read_validator_method($class), |
| 105 | $class . ' class should have a read validator'); |
| 106 | } |
| 107 | |
Roger Thomas | 6fb5923 | 2014-11-04 10:09:23 +0000 | [diff] [blame] | 108 | function assert_has_no_read_validator($class) |
| 109 | { |
Jens Geyer | 577f407 | 2014-07-23 19:04:12 +0200 | [diff] [blame] | 110 | my_assert(!has_read_validator_method($class), |
| 111 | $class . ' class should not have a read validator'); |
| 112 | } |
| 113 | |
Roger Thomas | 6fb5923 | 2014-11-04 10:09:23 +0000 | [diff] [blame] | 114 | function assert_has_a_write_validator($class) |
| 115 | { |
Jens Geyer | 577f407 | 2014-07-23 19:04:12 +0200 | [diff] [blame] | 116 | my_assert(has_write_validator_method($class), |
| 117 | $class . ' class should have a write validator'); |
| 118 | } |
| 119 | |
Roger Thomas | 6fb5923 | 2014-11-04 10:09:23 +0000 | [diff] [blame] | 120 | function assert_has_no_write_validator($class) |
| 121 | { |
Jens Geyer | 577f407 | 2014-07-23 19:04:12 +0200 | [diff] [blame] | 122 | my_assert(!has_write_validator_method($class), |
| 123 | $class . ' class should not have a write validator'); |
| 124 | } |
| 125 | |
Roger Thomas | 6fb5923 | 2014-11-04 10:09:23 +0000 | [diff] [blame] | 126 | function assert_protocol_exception_thrown($callable, $message) |
| 127 | { |
Jens Geyer | 577f407 | 2014-07-23 19:04:12 +0200 | [diff] [blame] | 128 | try { |
| 129 | call_user_func($callable); |
| 130 | my_assert(false, $message); |
| 131 | } catch (TProtocolException $e) { |
| 132 | } |
| 133 | } |
| 134 | |
Roger Thomas | 6fb5923 | 2014-11-04 10:09:23 +0000 | [diff] [blame] | 135 | function has_write_validator_method($class) |
| 136 | { |
Jens Geyer | 577f407 | 2014-07-23 19:04:12 +0200 | [diff] [blame] | 137 | $rc = new \ReflectionClass($class); |
Roger Thomas | 6fb5923 | 2014-11-04 10:09:23 +0000 | [diff] [blame] | 138 | |
Jens Geyer | 577f407 | 2014-07-23 19:04:12 +0200 | [diff] [blame] | 139 | return $rc->hasMethod('_validateForWrite'); |
| 140 | } |
| 141 | |
Roger Thomas | 6fb5923 | 2014-11-04 10:09:23 +0000 | [diff] [blame] | 142 | function has_read_validator_method($class) |
| 143 | { |
Jens Geyer | 577f407 | 2014-07-23 19:04:12 +0200 | [diff] [blame] | 144 | $rc = new \ReflectionClass($class); |
Roger Thomas | 6fb5923 | 2014-11-04 10:09:23 +0000 | [diff] [blame] | 145 | |
Jens Geyer | 577f407 | 2014-07-23 19:04:12 +0200 | [diff] [blame] | 146 | return $rc->hasMethod('_validateForRead'); |
| 147 | } |
| 148 | |
Roger Thomas | 6fb5923 | 2014-11-04 10:09:23 +0000 | [diff] [blame] | 149 | function my_assert($something, $message) |
| 150 | { |
Jens Geyer | 577f407 | 2014-07-23 19:04:12 +0200 | [diff] [blame] | 151 | if (!$something) { |
| 152 | fwrite(STDERR, basename(__FILE__) . " FAILED: $message\n"); |
| 153 | exit(1); |
| 154 | } |
| 155 | } |