blob: 8e6a6d9174320386c1028202717d4490b9ac97e1 [file] [log] [blame]
Robert Lu12f124c2018-01-25 23:19:41 +08001<?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.
Robert Lu12f124c2018-01-25 23:19:41 +080020 */
21
Volodymyr Panivko8e828c02024-02-19 11:34:48 +010022namespace Test\Thrift\Unit;
Robert Lu12f124c2018-01-25 23:19:41 +080023
24use PHPUnit\Framework\TestCase;
Volodymyr Panivko8e828c02024-02-19 11:34:48 +010025use Test\Thrift\Fixtures\Fixtures;
26use Test\Thrift\Fixtures\TSimpleJSONProtocolFixtures;
27use Thrift\ClassLoader\ThriftClassLoader;
Robert Lu12f124c2018-01-25 23:19:41 +080028use Thrift\Protocol\TSimpleJSONProtocol;
29use Thrift\Transport\TMemoryBuffer;
30
Robert Lu12f124c2018-01-25 23:19:41 +080031/***
Volodymyr Panivko8e828c02024-02-19 11:34:48 +010032 * This test suite depends on running the compiler against the ./Resources/ThriftTest.thrift file:
33 * lib/php/test$ ../../../compiler/cpp/thrift --gen php -r --out ./Resources/packages/php ./Resources/ThriftTest.thrift
Robert Lu12f124c2018-01-25 23:19:41 +080034 */
35class TSimpleJSONProtocolTest extends TestCase
36{
37 private $transport;
38 private $protocol;
39
vladimir.panivkof6927022024-02-24 17:12:10 +010040 public static function setUpBeforeClass(): void
Robert Lu12f124c2018-01-25 23:19:41 +080041 {
Volodymyr Panivko8e828c02024-02-19 11:34:48 +010042 $loader = new ThriftClassLoader();
43 $loader->registerNamespace('ThriftTest', __DIR__ . '/../Resources/packages/php');
44 $loader->registerDefinition('ThriftTest', __DIR__ . '/../Resources/packages/php');
45 $loader->register();
Robert Lu12f124c2018-01-25 23:19:41 +080046
47 Fixtures::populateTestArgs();
48 TSimpleJSONProtocolFixtures::populateTestArgsSimpleJSON();
49 }
50
vladimir.panivkof6927022024-02-24 17:12:10 +010051 public function setUp(): void
Robert Lu12f124c2018-01-25 23:19:41 +080052 {
53 $this->transport = new TMemoryBuffer();
54 $this->protocol = new TSimpleJSONProtocol($this->transport);
55 $this->transport->open();
56 }
57
58 /**
59 * WRITE TESTS
60 */
61 public function testVoidWrite()
62 {
63 $args = new \ThriftTest\ThriftTest_testVoid_args();
64 $args->write($this->protocol);
65
66 $actual = $this->transport->read(Fixtures::$bufsize);
67 $expected = TSimpleJSONProtocolFixtures::$testArgsJSON['testVoid'];
68
69 $this->assertEquals($expected, $actual);
70 }
71
72 public function testString1Write()
73 {
74 $args = new \ThriftTest\ThriftTest_testString_args();
75 $args->thing = Fixtures::$testArgs['testString1'];
76 $args->write($this->protocol);
77
78 $actual = $this->transport->read(Fixtures::$bufsize);
79 $expected = TSimpleJSONProtocolFixtures::$testArgsJSON['testString1'];
80
81 $this->assertEquals($expected, $actual);
82 }
83
84 public function testString2Write()
85 {
86 $args = new \ThriftTest\ThriftTest_testString_args();
87 $args->thing = Fixtures::$testArgs['testString2'];
88 $args->write($this->protocol);
89
90 $actual = $this->transport->read(Fixtures::$bufsize);
91 $expected = TSimpleJSONProtocolFixtures::$testArgsJSON['testString2'];
92
93 $this->assertEquals($expected, $actual);
94 }
95
96 public function testDoubleWrite()
97 {
98 $args = new \ThriftTest\ThriftTest_testDouble_args();
99 $args->thing = Fixtures::$testArgs['testDouble'];
100 $args->write($this->protocol);
101
102 $actual = $this->transport->read(Fixtures::$bufsize);
103 $expected = TSimpleJSONProtocolFixtures::$testArgsJSON['testDouble'];
104
105 $this->assertEquals($expected, $actual);
106 }
107
108 public function testByteWrite()
109 {
110 $args = new \ThriftTest\ThriftTest_testByte_args();
111 $args->thing = Fixtures::$testArgs['testByte'];
112 $args->write($this->protocol);
113
114 $actual = $this->transport->read(Fixtures::$bufsize);
115 $expected = TSimpleJSONProtocolFixtures::$testArgsJSON['testByte'];
116
117 $this->assertEquals($expected, $actual);
118 }
119
120 public function testI32Write()
121 {
122 $args = new \ThriftTest\ThriftTest_testI32_args();
123 $args->thing = Fixtures::$testArgs['testI32'];
124 $args->write($this->protocol);
125
126 $actual = $this->transport->read(Fixtures::$bufsize);
127 $expected = TSimpleJSONProtocolFixtures::$testArgsJSON['testI32'];
128
129 $this->assertEquals($expected, $actual);
130 }
131
132 public function testI64Write()
133 {
134 $args = new \ThriftTest\ThriftTest_testI64_args();
135 $args->thing = Fixtures::$testArgs['testI64'];
136 $args->write($this->protocol);
137
138 $actual = $this->transport->read(Fixtures::$bufsize);
139 $expected = TSimpleJSONProtocolFixtures::$testArgsJSON['testI64'];
140
141 $this->assertEquals($expected, $actual);
142 }
143
144 public function testStructWrite()
145 {
146 $args = new \ThriftTest\ThriftTest_testStruct_args();
147 $args->thing = Fixtures::$testArgs['testStruct'];
148
149 $args->write($this->protocol);
150
151 $actual = $this->transport->read(Fixtures::$bufsize);
152 $expected = TSimpleJSONProtocolFixtures::$testArgsJSON['testStruct'];
153
154 $this->assertEquals($expected, $actual);
155 }
156
157 public function testNestWrite()
158 {
159 $args = new \ThriftTest\ThriftTest_testNest_args();
160 $args->thing = Fixtures::$testArgs['testNest'];
161
162 $args->write($this->protocol);
163
164 $actual = $this->transport->read(Fixtures::$bufsize);
165 $expected = TSimpleJSONProtocolFixtures::$testArgsJSON['testNest'];
166
167 $this->assertEquals($expected, $actual);
168 }
169
170 public function testMapWrite()
171 {
172 $args = new \ThriftTest\ThriftTest_testMap_args();
173 $args->thing = Fixtures::$testArgs['testMap'];
174
175 $args->write($this->protocol);
176
177 $actual = $this->transport->read(Fixtures::$bufsize);
178 $expected = TSimpleJSONProtocolFixtures::$testArgsJSON['testMap'];
179
180 $this->assertEquals($expected, $actual);
181 }
182
183 public function testStringMapWrite()
184 {
185 $args = new \ThriftTest\ThriftTest_testStringMap_args();
186 $args->thing = Fixtures::$testArgs['testStringMap'];
187
188 $args->write($this->protocol);
189
190 $actual = $this->transport->read(Fixtures::$bufsize);
191 $expected = TSimpleJSONProtocolFixtures::$testArgsJSON['testStringMap'];
192
193 $this->assertEquals($expected, $actual);
194 }
195
196 public function testSetWrite()
197 {
198 $args = new \ThriftTest\ThriftTest_testSet_args();
199 $args->thing = Fixtures::$testArgs['testSet'];
200
201 $args->write($this->protocol);
202
203 $actual = $this->transport->read(Fixtures::$bufsize);
204 $expected = TSimpleJSONProtocolFixtures::$testArgsJSON['testSet'];
205
206 $this->assertEquals($expected, $actual);
207 }
208
209 public function testListWrite()
210 {
211 $args = new \ThriftTest\ThriftTest_testList_args();
212 $args->thing = Fixtures::$testArgs['testList'];
213
214 $args->write($this->protocol);
215
216 $actual = $this->transport->read(Fixtures::$bufsize);
217 $expected = TSimpleJSONProtocolFixtures::$testArgsJSON['testList'];
218
219 $this->assertEquals($expected, $actual);
220 }
221
222 public function testEnumWrite()
223 {
224 $args = new \ThriftTest\ThriftTest_testEnum_args();
225 $args->thing = Fixtures::$testArgs['testEnum'];
226
227 $args->write($this->protocol);
228
229 $actual = $this->transport->read(Fixtures::$bufsize);
230 $expected = TSimpleJSONProtocolFixtures::$testArgsJSON['testEnum'];
231
232 $this->assertEquals($expected, $actual);
233 }
234
235 public function testTypedefWrite()
236 {
237 $args = new \ThriftTest\ThriftTest_testTypedef_args();
238 $args->thing = Fixtures::$testArgs['testTypedef'];
239
240 $args->write($this->protocol);
241
242 $actual = $this->transport->read(Fixtures::$bufsize);
243 $expected = TSimpleJSONProtocolFixtures::$testArgsJSON['testTypedef'];
244
245 $this->assertEquals($expected, $actual);
246 }
247}