|
| 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\Unit\Lib\Base; |
| 23 | + |
| 24 | +use PHPUnit\Framework\TestCase; |
| 25 | +use Test\Thrift\Unit\Lib\Base\Fixture\ComplexStruct; |
| 26 | +use Test\Thrift\Unit\Lib\Base\Fixture\NestedStruct; |
| 27 | +use Thrift\Protocol\TBinaryProtocol; |
| 28 | +use Thrift\Transport\TMemoryBuffer; |
| 29 | +use Thrift\Type\TType; |
| 30 | + |
| 31 | +class TBaseTest extends TestCase |
| 32 | +{ |
| 33 | + public function testConstructorHydratesKnownFieldsFromSpec(): void |
| 34 | + { |
| 35 | + $struct = new ComplexStruct( |
| 36 | + ComplexStruct::$_TSPEC, |
| 37 | + [ |
| 38 | + 'flag' => true, |
| 39 | + 'name' => 'hydrated', |
| 40 | + ] |
| 41 | + ); |
| 42 | + |
| 43 | + $this->assertTrue($struct->flag); |
| 44 | + $this->assertSame('hydrated', $struct->name); |
| 45 | + $this->assertNull($struct->child); |
| 46 | + } |
| 47 | + |
| 48 | + public function testWakeupPreservesExistingState(): void |
| 49 | + { |
| 50 | + $struct = $this->createComplexStruct(); |
| 51 | + |
| 52 | + /** @var ComplexStruct $restored */ |
| 53 | + $restored = unserialize(serialize($struct)); |
| 54 | + |
| 55 | + $this->assertEquals($struct, $restored); |
| 56 | + } |
| 57 | + |
| 58 | + public function testReadAndWriteRoundTripNestedContainers(): void |
| 59 | + { |
| 60 | + $restored = $this->roundTrip($this->createComplexStruct()); |
| 61 | + |
| 62 | + $this->assertTrue($restored->flag); |
| 63 | + $this->assertSame('root', $restored->name); |
| 64 | + $this->assertInstanceOf(NestedStruct::class, $restored->child); |
| 65 | + $this->assertSame('child', $restored->child->value); |
| 66 | + $this->assertSame(['alpha' => 1, 'beta' => 2], $restored->mapField); |
| 67 | + $this->assertCount(2, $restored->listField); |
| 68 | + $this->assertSame('first', $restored->listField[0]->value); |
| 69 | + $this->assertSame('second', $restored->listField[1]->value); |
| 70 | + $this->assertSame([10 => true, 20 => true], $restored->setField); |
| 71 | + $this->assertSame([1 => [3, 4], 2 => [5]], $restored->mapOfLists); |
| 72 | + $this->assertNull($restored->optionalField); |
| 73 | + } |
| 74 | + |
| 75 | + public function testReadSkipsUnknownAndUnexpectedFields(): void |
| 76 | + { |
| 77 | + $transport = new TMemoryBuffer(); |
| 78 | + $protocol = new TBinaryProtocol($transport); |
| 79 | + |
| 80 | + $protocol->writeStructBegin('ComplexStruct'); |
| 81 | + |
| 82 | + $protocol->writeFieldBegin('flag', TType::STRING, 1); |
| 83 | + $protocol->writeString('ignored'); |
| 84 | + $protocol->writeFieldEnd(); |
| 85 | + |
| 86 | + $protocol->writeFieldBegin('unknown', TType::I32, 99); |
| 87 | + $protocol->writeI32(123); |
| 88 | + $protocol->writeFieldEnd(); |
| 89 | + |
| 90 | + $protocol->writeFieldBegin('name', TType::STRING, 2); |
| 91 | + $protocol->writeString('kept'); |
| 92 | + $protocol->writeFieldEnd(); |
| 93 | + |
| 94 | + $protocol->writeFieldStop(); |
| 95 | + $protocol->writeStructEnd(); |
| 96 | + |
| 97 | + $struct = new ComplexStruct(); |
| 98 | + $struct->read(new TBinaryProtocol($transport)); |
| 99 | + |
| 100 | + $this->assertNull($struct->flag); |
| 101 | + $this->assertSame('kept', $struct->name); |
| 102 | + $this->assertNull($struct->child); |
| 103 | + } |
| 104 | + |
| 105 | + private function roundTrip(ComplexStruct $struct): ComplexStruct |
| 106 | + { |
| 107 | + $transport = new TMemoryBuffer(); |
| 108 | + $writer = new TBinaryProtocol($transport); |
| 109 | + $struct->write($writer); |
| 110 | + |
| 111 | + $copy = new ComplexStruct(); |
| 112 | + $copy->read(new TBinaryProtocol($transport)); |
| 113 | + |
| 114 | + return $copy; |
| 115 | + } |
| 116 | + |
| 117 | + private function createComplexStruct(): ComplexStruct |
| 118 | + { |
| 119 | + $child = new NestedStruct(NestedStruct::$_TSPEC, ['value' => 'child']); |
| 120 | + $first = new NestedStruct(NestedStruct::$_TSPEC, ['value' => 'first']); |
| 121 | + $second = new NestedStruct(NestedStruct::$_TSPEC, ['value' => 'second']); |
| 122 | + |
| 123 | + return new ComplexStruct( |
| 124 | + ComplexStruct::$_TSPEC, |
| 125 | + [ |
| 126 | + 'flag' => true, |
| 127 | + 'name' => 'root', |
| 128 | + 'child' => $child, |
| 129 | + 'mapField' => ['alpha' => 1, 'beta' => 2], |
| 130 | + 'listField' => [$first, $second], |
| 131 | + 'setField' => [10 => true, 20 => true], |
| 132 | + 'mapOfLists' => [1 => [3, 4], 2 => [5]], |
| 133 | + ] |
| 134 | + ); |
| 135 | + } |
| 136 | +} |
0 commit comments