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