blob: 4404e72ce98a7a05ead767d599f04af30ecdd381 [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 Panivko8e828c02024-02-19 11:34:48 +010022namespace Test\Thrift\Unit;
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{
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 Panivko8e828c02024-02-19 11:34:48 +010066 $this->assertTrue(true);
Robert Lu12f124c2018-01-25 23:19:41 +080067 }
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 Panivko8e828c02024-02-19 11:34:48 +010078 $this->expectExceptionMessage('Required field Bonk.message is unset!');
79 throw $e;
Robert Lu12f124c2018-01-25 23:19:41 +080080 }
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 Panivko8e828c02024-02-19 11:34:48 +010094 $this->expectExceptionMessage('Required field StructA.s is unset!');
95 throw $e;
Robert Lu12f124c2018-01-25 23:19:41 +080096 }
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 Panivko8e828c02024-02-19 11:34:48 +0100123 } else {
124 static::assertTrue(true);
Robert Lu12f124c2018-01-25 23:19:41 +0800125 }
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 Panivko8e828c02024-02-19 11:34:48 +0100132 } else {
133 static::assertTrue(true);
Robert Lu12f124c2018-01-25 23:19:41 +0800134 }
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 Panivko8e828c02024-02-19 11:34:48 +0100141 } else {
142 static::assertTrue(true);
Robert Lu12f124c2018-01-25 23:19:41 +0800143 }
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 Panivko8e828c02024-02-19 11:34:48 +0100150 } else {
151 static::assertTrue(true);
Robert Lu12f124c2018-01-25 23:19:41 +0800152 }
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}