blob: ae9d5844d2e236f31fc1f3771877d7fce1921547 [file] [log] [blame]
Robert Lu12f124c2018-01-25 23:19:41 +08001<?php
Volodymyr Panivko8e828c02024-02-19 11:34:48 +01002
Robert Lu12f124c2018-01-25 23:19:41 +08003/*
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 Panivko68139d12024-03-19 23:14:07 +010022namespace Test\Thrift\Integration;
Robert Lu12f124c2018-01-25 23:19:41 +080023
24use PHPUnit\Framework\TestCase;
25use Thrift\Exception\TProtocolException;
26use Thrift\Protocol\TBinaryProtocol;
27use Thrift\Transport\TMemoryBuffer;
28
29abstract class BaseValidatorTest extends TestCase
30{
Volodymyr Panivko68139d12024-03-19 23:14:07 +010031 abstract public function getNsGlobal();
32
Robert Lu12f124c2018-01-25 23:19:41 +080033 public function testEmptyStructValidator()
34 {
Volodymyr Panivko68139d12024-03-19 23:14:07 +010035 $this->assertNoReadValidator($this->getNsGlobal() . '\ThriftTest\EmptyStruct');
36 $this->assertNoWriteValidator($this->getNsGlobal() . '\ThriftTest\EmptyStruct');
Robert Lu12f124c2018-01-25 23:19:41 +080037 }
38
39 public function testBonkValidator()
40 {
Volodymyr Panivko68139d12024-03-19 23:14:07 +010041 $this->assertNoReadValidator($this->getNsGlobal() . '\ThriftTest\Bonk');
42 $this->assertHasWriteValidator($this->getNsGlobal() . '\ThriftTest\Bonk');
Robert Lu12f124c2018-01-25 23:19:41 +080043 }
44
45 public function testStructAValidator()
46 {
Volodymyr Panivko68139d12024-03-19 23:14:07 +010047 $this->assertHasReadValidator($this->getNsGlobal() . '\ThriftTest\StructA');
48 $this->assertHasWriteValidator($this->getNsGlobal() . '\ThriftTest\StructA');
Robert Lu12f124c2018-01-25 23:19:41 +080049 }
50
51 public function testUnionOfStringsValidator()
52 {
Volodymyr Panivko68139d12024-03-19 23:14:07 +010053 $this->assertNoWriteValidator($this->getNsGlobal() . '\TestValidators\UnionOfStrings');
Robert Lu12f124c2018-01-25 23:19:41 +080054 }
55
56 public function testServiceResultValidator()
57 {
Volodymyr Panivko68139d12024-03-19 23:14:07 +010058 $this->assertNoReadValidator($this->getNsGlobal() . '\TestValidators\TestService_test_result');
59 $this->assertNoWriteValidator($this->getNsGlobal() . '\TestValidators\TestService_test_result');
Robert Lu12f124c2018-01-25 23:19:41 +080060 }
61
62 public function testReadEmpty()
63 {
Volodymyr Panivko68139d12024-03-19 23:14:07 +010064 $className = $this->getNsGlobal() . '\ThriftTest\Bonk';
65 $bonk = new $className();
Robert Lu12f124c2018-01-25 23:19:41 +080066 $transport = new TMemoryBuffer("\000");
67 $protocol = new TBinaryProtocol($transport);
68 $bonk->read($protocol);
Volodymyr Panivko8e828c02024-02-19 11:34:48 +010069 $this->assertTrue(true);
Robert Lu12f124c2018-01-25 23:19:41 +080070 }
71
72 public function testWriteEmpty()
73 {
Volodymyr Panivko68139d12024-03-19 23:14:07 +010074 $className = $this->getNsGlobal() . '\ThriftTest\Bonk';
75 $bonk = new $className();
Robert Lu12f124c2018-01-25 23:19:41 +080076 $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 Panivko8e828c02024-02-19 11:34:48 +010082 $this->expectExceptionMessage('Required field Bonk.message is unset!');
83 throw $e;
Robert Lu12f124c2018-01-25 23:19:41 +080084 }
85 }
86
87 public function testWriteWithMissingRequired()
88 {
89 // Check that we are not able to write StructA with a missing required field
Volodymyr Panivko68139d12024-03-19 23:14:07 +010090 $className = $this->getNsGlobal() . '\ThriftTest\StructA';
91 $structa = new $className();
Robert Lu12f124c2018-01-25 23:19:41 +080092 $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 Panivko8e828c02024-02-19 11:34:48 +010099 $this->expectExceptionMessage('Required field StructA.s is unset!');
100 throw $e;
Robert Lu12f124c2018-01-25 23:19:41 +0800101 }
102 }
103
104 public function testReadStructA()
105 {
106 $transport = new TMemoryBuffer(base64_decode('CwABAAAAA2FiYwA='));
107 $protocol = new TBinaryProtocol($transport);
Volodymyr Panivko68139d12024-03-19 23:14:07 +0100108 $className = $this->getNsGlobal() . '\ThriftTest\StructA';
109 $structa = new $className();
Robert Lu12f124c2018-01-25 23:19:41 +0800110 $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 Panivko68139d12024-03-19 23:14:07 +0100118 $className = $this->getNsGlobal() . '\ThriftTest\StructA';
119 $structa = new $className();
Robert Lu12f124c2018-01-25 23:19:41 +0800120 $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 Panivko8e828c02024-02-19 11:34:48 +0100130 } else {
131 static::assertTrue(true);
Robert Lu12f124c2018-01-25 23:19:41 +0800132 }
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 Panivko8e828c02024-02-19 11:34:48 +0100139 } else {
140 static::assertTrue(true);
Robert Lu12f124c2018-01-25 23:19:41 +0800141 }
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 Panivko8e828c02024-02-19 11:34:48 +0100148 } else {
149 static::assertTrue(true);
Robert Lu12f124c2018-01-25 23:19:41 +0800150 }
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 Panivko8e828c02024-02-19 11:34:48 +0100157 } else {
158 static::assertTrue(true);
Robert Lu12f124c2018-01-25 23:19:41 +0800159 }
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}