Robert Lu | 12f124c | 2018-01-25 23:19:41 +0800 | [diff] [blame] | 1 | <?php |
Volodymyr Panivko | 8e828c0 | 2024-02-19 11:34:48 +0100 | [diff] [blame] | 2 | |
Robert Lu | 12f124c | 2018-01-25 23:19:41 +0800 | [diff] [blame] | 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 | |
Volodymyr Panivko | 68139d1 | 2024-03-19 23:14:07 +0100 | [diff] [blame^] | 22 | namespace Test\Thrift\Integration; |
Robert Lu | 12f124c | 2018-01-25 23:19:41 +0800 | [diff] [blame] | 23 | |
| 24 | use PHPUnit\Framework\TestCase; |
| 25 | use Thrift\Exception\TProtocolException; |
| 26 | use Thrift\Protocol\TBinaryProtocol; |
| 27 | use Thrift\Transport\TMemoryBuffer; |
| 28 | |
| 29 | abstract class BaseValidatorTest extends TestCase |
| 30 | { |
Volodymyr Panivko | 68139d1 | 2024-03-19 23:14:07 +0100 | [diff] [blame^] | 31 | abstract public function getNsGlobal(); |
| 32 | |
Robert Lu | 12f124c | 2018-01-25 23:19:41 +0800 | [diff] [blame] | 33 | public function testEmptyStructValidator() |
| 34 | { |
Volodymyr Panivko | 68139d1 | 2024-03-19 23:14:07 +0100 | [diff] [blame^] | 35 | $this->assertNoReadValidator($this->getNsGlobal() . '\ThriftTest\EmptyStruct'); |
| 36 | $this->assertNoWriteValidator($this->getNsGlobal() . '\ThriftTest\EmptyStruct'); |
Robert Lu | 12f124c | 2018-01-25 23:19:41 +0800 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | public function testBonkValidator() |
| 40 | { |
Volodymyr Panivko | 68139d1 | 2024-03-19 23:14:07 +0100 | [diff] [blame^] | 41 | $this->assertNoReadValidator($this->getNsGlobal() . '\ThriftTest\Bonk'); |
| 42 | $this->assertHasWriteValidator($this->getNsGlobal() . '\ThriftTest\Bonk'); |
Robert Lu | 12f124c | 2018-01-25 23:19:41 +0800 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | public function testStructAValidator() |
| 46 | { |
Volodymyr Panivko | 68139d1 | 2024-03-19 23:14:07 +0100 | [diff] [blame^] | 47 | $this->assertHasReadValidator($this->getNsGlobal() . '\ThriftTest\StructA'); |
| 48 | $this->assertHasWriteValidator($this->getNsGlobal() . '\ThriftTest\StructA'); |
Robert Lu | 12f124c | 2018-01-25 23:19:41 +0800 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | public function testUnionOfStringsValidator() |
| 52 | { |
Volodymyr Panivko | 68139d1 | 2024-03-19 23:14:07 +0100 | [diff] [blame^] | 53 | $this->assertNoWriteValidator($this->getNsGlobal() . '\TestValidators\UnionOfStrings'); |
Robert Lu | 12f124c | 2018-01-25 23:19:41 +0800 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | public function testServiceResultValidator() |
| 57 | { |
Volodymyr Panivko | 68139d1 | 2024-03-19 23:14:07 +0100 | [diff] [blame^] | 58 | $this->assertNoReadValidator($this->getNsGlobal() . '\TestValidators\TestService_test_result'); |
| 59 | $this->assertNoWriteValidator($this->getNsGlobal() . '\TestValidators\TestService_test_result'); |
Robert Lu | 12f124c | 2018-01-25 23:19:41 +0800 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | public function testReadEmpty() |
| 63 | { |
Volodymyr Panivko | 68139d1 | 2024-03-19 23:14:07 +0100 | [diff] [blame^] | 64 | $className = $this->getNsGlobal() . '\ThriftTest\Bonk'; |
| 65 | $bonk = new $className(); |
Robert Lu | 12f124c | 2018-01-25 23:19:41 +0800 | [diff] [blame] | 66 | $transport = new TMemoryBuffer("\000"); |
| 67 | $protocol = new TBinaryProtocol($transport); |
| 68 | $bonk->read($protocol); |
Volodymyr Panivko | 8e828c0 | 2024-02-19 11:34:48 +0100 | [diff] [blame] | 69 | $this->assertTrue(true); |
Robert Lu | 12f124c | 2018-01-25 23:19:41 +0800 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | public function testWriteEmpty() |
| 73 | { |
Volodymyr Panivko | 68139d1 | 2024-03-19 23:14:07 +0100 | [diff] [blame^] | 74 | $className = $this->getNsGlobal() . '\ThriftTest\Bonk'; |
| 75 | $bonk = new $className(); |
Robert Lu | 12f124c | 2018-01-25 23:19:41 +0800 | [diff] [blame] | 76 | $transport = new TMemoryBuffer(); |
| 77 | $protocol = new TBinaryProtocol($transport); |
| 78 | try { |
| 79 | $bonk->write($protocol); |
| 80 | $this->fail('Bonk was able to write an empty object'); |
| 81 | } catch (TProtocolException $e) { |
Volodymyr Panivko | 8e828c0 | 2024-02-19 11:34:48 +0100 | [diff] [blame] | 82 | $this->expectExceptionMessage('Required field Bonk.message is unset!'); |
| 83 | throw $e; |
Robert Lu | 12f124c | 2018-01-25 23:19:41 +0800 | [diff] [blame] | 84 | } |
| 85 | } |
| 86 | |
| 87 | public function testWriteWithMissingRequired() |
| 88 | { |
| 89 | // Check that we are not able to write StructA with a missing required field |
Volodymyr Panivko | 68139d1 | 2024-03-19 23:14:07 +0100 | [diff] [blame^] | 90 | $className = $this->getNsGlobal() . '\ThriftTest\StructA'; |
| 91 | $structa = new $className(); |
Robert Lu | 12f124c | 2018-01-25 23:19:41 +0800 | [diff] [blame] | 92 | $transport = new TMemoryBuffer(); |
| 93 | $protocol = new TBinaryProtocol($transport); |
| 94 | |
| 95 | try { |
| 96 | $structa->write($protocol); |
| 97 | $this->fail('StructA was able to write an empty object'); |
| 98 | } catch (TProtocolException $e) { |
Volodymyr Panivko | 8e828c0 | 2024-02-19 11:34:48 +0100 | [diff] [blame] | 99 | $this->expectExceptionMessage('Required field StructA.s is unset!'); |
| 100 | throw $e; |
Robert Lu | 12f124c | 2018-01-25 23:19:41 +0800 | [diff] [blame] | 101 | } |
| 102 | } |
| 103 | |
| 104 | public function testReadStructA() |
| 105 | { |
| 106 | $transport = new TMemoryBuffer(base64_decode('CwABAAAAA2FiYwA=')); |
| 107 | $protocol = new TBinaryProtocol($transport); |
Volodymyr Panivko | 68139d1 | 2024-03-19 23:14:07 +0100 | [diff] [blame^] | 108 | $className = $this->getNsGlobal() . '\ThriftTest\StructA'; |
| 109 | $structa = new $className(); |
Robert Lu | 12f124c | 2018-01-25 23:19:41 +0800 | [diff] [blame] | 110 | $structa->read($protocol); |
| 111 | $this->assertEquals("abc", $structa->s); |
| 112 | } |
| 113 | |
| 114 | public function testWriteStructA() |
| 115 | { |
| 116 | $transport = new TMemoryBuffer(); |
| 117 | $protocol = new TBinaryProtocol($transport); |
Volodymyr Panivko | 68139d1 | 2024-03-19 23:14:07 +0100 | [diff] [blame^] | 118 | $className = $this->getNsGlobal() . '\ThriftTest\StructA'; |
| 119 | $structa = new $className(); |
Robert Lu | 12f124c | 2018-01-25 23:19:41 +0800 | [diff] [blame] | 120 | $structa->s = "abc"; |
| 121 | $structa->write($protocol); |
| 122 | $writeResult = base64_encode($transport->getBuffer()); |
| 123 | $this->assertEquals('CwABAAAAA2FiYwA=', $writeResult); |
| 124 | } |
| 125 | |
| 126 | protected static function assertHasReadValidator($class) |
| 127 | { |
| 128 | if (!static::hasReadValidator($class)) { |
| 129 | static::fail($class . ' class should have a read validator'); |
Volodymyr Panivko | 8e828c0 | 2024-02-19 11:34:48 +0100 | [diff] [blame] | 130 | } else { |
| 131 | static::assertTrue(true); |
Robert Lu | 12f124c | 2018-01-25 23:19:41 +0800 | [diff] [blame] | 132 | } |
| 133 | } |
| 134 | |
| 135 | protected static function assertNoReadValidator($class) |
| 136 | { |
| 137 | if (static::hasReadValidator($class)) { |
| 138 | static::fail($class . ' class should not have a write validator'); |
Volodymyr Panivko | 8e828c0 | 2024-02-19 11:34:48 +0100 | [diff] [blame] | 139 | } else { |
| 140 | static::assertTrue(true); |
Robert Lu | 12f124c | 2018-01-25 23:19:41 +0800 | [diff] [blame] | 141 | } |
| 142 | } |
| 143 | |
| 144 | protected static function assertHasWriteValidator($class) |
| 145 | { |
| 146 | if (!static::hasWriteValidator($class)) { |
| 147 | static::fail($class . ' class should have a write validator'); |
Volodymyr Panivko | 8e828c0 | 2024-02-19 11:34:48 +0100 | [diff] [blame] | 148 | } else { |
| 149 | static::assertTrue(true); |
Robert Lu | 12f124c | 2018-01-25 23:19:41 +0800 | [diff] [blame] | 150 | } |
| 151 | } |
| 152 | |
| 153 | protected static function assertNoWriteValidator($class) |
| 154 | { |
| 155 | if (static::hasWriteValidator($class)) { |
| 156 | static::fail($class . ' class should not have a write validator'); |
Volodymyr Panivko | 8e828c0 | 2024-02-19 11:34:48 +0100 | [diff] [blame] | 157 | } else { |
| 158 | static::assertTrue(true); |
Robert Lu | 12f124c | 2018-01-25 23:19:41 +0800 | [diff] [blame] | 159 | } |
| 160 | } |
| 161 | |
| 162 | private static function hasReadValidator($class) |
| 163 | { |
| 164 | $rc = new \ReflectionClass($class); |
| 165 | |
| 166 | return $rc->hasMethod('_validateForRead'); |
| 167 | } |
| 168 | |
| 169 | private static function hasWriteValidator($class) |
| 170 | { |
| 171 | $rc = new \ReflectionClass($class); |
| 172 | |
| 173 | return $rc->hasMethod('_validateForWrite'); |
| 174 | } |
| 175 | } |