Debian Codegraph @

lib/php/test/Integration/BaseValidatorTest.php

raw
1 <?php
2
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
22 namespace Test\Thrift\Integration;
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 abstract public function getNsGlobal();
32
33 public function testEmptyStructValidator()
34 {
35 $this->assertNoReadValidator($this->getNsGlobal() . '\ThriftTest\EmptyStruct');
36 $this->assertNoWriteValidator($this->getNsGlobal() . '\ThriftTest\EmptyStruct');
37 }
38
39 public function testBonkValidator()
40 {
41 $this->assertNoReadValidator($this->getNsGlobal() . '\ThriftTest\Bonk');
42 $this->assertHasWriteValidator($this->getNsGlobal() . '\ThriftTest\Bonk');
43 }
44
45 public function testStructAValidator()
46 {
47 $this->assertHasReadValidator($this->getNsGlobal() . '\ThriftTest\StructA');
48 $this->assertHasWriteValidator($this->getNsGlobal() . '\ThriftTest\StructA');
49 }
50
51 public function testUnionOfStringsValidator()
52 {
53 $this->assertNoWriteValidator($this->getNsGlobal() . '\TestValidators\UnionOfStrings');
54 }
55
56 public function testServiceResultValidator()
57 {
58 $this->assertNoReadValidator($this->getNsGlobal() . '\TestValidators\TestService_test_result');
59 $this->assertNoWriteValidator($this->getNsGlobal() . '\TestValidators\TestService_test_result');
60 }
61
62 public function testReadEmpty()
63 {
64 $className = $this->getNsGlobal() . '\ThriftTest\Bonk';
65 $bonk = new $className();
66 $transport = new TMemoryBuffer("\000");
67 $protocol = new TBinaryProtocol($transport);
68 $bonk->read($protocol);
69 $this->assertTrue(true);
70 }
71
72 public function testWriteEmpty()
73 {
74 $className = $this->getNsGlobal() . '\ThriftTest\Bonk';
75 $bonk = new $className();
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) {
82 $this->expectExceptionMessage('Required field Bonk.message is unset!');
83 throw $e;
84 }
85 }
86
87 public function testWriteWithMissingRequired()
88 {
89 // Check that we are not able to write StructA with a missing required field
90 $className = $this->getNsGlobal() . '\ThriftTest\StructA';
91 $structa = new $className();
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) {
99 $this->expectExceptionMessage('Required field StructA.s is unset!');
100 throw $e;
101 }
102 }
103
104 public function testReadStructA()
105 {
106 $transport = new TMemoryBuffer(base64_decode('CwABAAAAA2FiYwA='));
107 $protocol = new TBinaryProtocol($transport);
108 $className = $this->getNsGlobal() . '\ThriftTest\StructA';
109 $structa = new $className();
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);
118 $className = $this->getNsGlobal() . '\ThriftTest\StructA';
119 $structa = new $className();
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');
130 } else {
131 static::assertTrue(true);
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');
139 } else {
140 static::assertTrue(true);
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');
148 } else {
149 static::assertTrue(true);
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');
157 } else {
158 static::assertTrue(true);
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 }