GeneratedTest.php
3 years ago
MessageAwareArrayComparator.php
3 years ago
MessageAwareExporter.php
3 years ago
MockBidiStreamingCall.php
3 years ago
MockClientStreamingCall.php
3 years ago
MockGrpcTransport.php
3 years ago
MockRequest.php
3 years ago
MockRequestBody.php
3 years ago
MockResponse.php
3 years ago
MockServerStreamingCall.php
3 years ago
MockStatus.php
3 years ago
MockStubTrait.php
3 years ago
MockTransport.php
3 years ago
MockUnaryCall.php
3 years ago
ProtobufGPBEmptyComparator.php
3 years ago
ProtobufMessageComparator.php
3 years ago
ReceivedRequest.php
3 years ago
SerializationTrait.php
3 years ago
mocks.proto
3 years ago
GeneratedTest.php
103 lines
| 1 | <?php |
| 2 | /* |
| 3 | * Copyright 2017 Google LLC |
| 4 | * All rights reserved. |
| 5 | * |
| 6 | * Redistribution and use in source and binary forms, with or without |
| 7 | * modification, are permitted provided that the following conditions are |
| 8 | * met: |
| 9 | * |
| 10 | * * Redistributions of source code must retain the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer. |
| 12 | * * Redistributions in binary form must reproduce the above |
| 13 | * copyright notice, this list of conditions and the following disclaimer |
| 14 | * in the documentation and/or other materials provided with the |
| 15 | * distribution. |
| 16 | * * Neither the name of Google Inc. nor the names of its |
| 17 | * contributors may be used to endorse or promote products derived from |
| 18 | * this software without specific prior written permission. |
| 19 | * |
| 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 21 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 22 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 23 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 24 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 25 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 26 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 27 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 28 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 29 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 30 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 | */ |
| 32 | namespace Google\ApiCore\Testing; |
| 33 | |
| 34 | use Google\ApiCore\Serializer; |
| 35 | use Google\Protobuf\DescriptorPool; |
| 36 | use Google\Protobuf\Internal\Message; |
| 37 | use Google\Protobuf\Internal\RepeatedField; |
| 38 | use PHPUnit\Framework\TestCase; |
| 39 | |
| 40 | /** |
| 41 | * @internal |
| 42 | */ |
| 43 | abstract class GeneratedTest extends TestCase |
| 44 | { |
| 45 | /** |
| 46 | * @param mixed $expected |
| 47 | * @param mixed $actual |
| 48 | */ |
| 49 | public function assertProtobufEquals(&$expected, &$actual) |
| 50 | { |
| 51 | if ($expected === $actual) { |
| 52 | // This is not needed but reduces the number of "This test did not perform any assertions" messages |
| 53 | $this->assertSame($expected, $actual); |
| 54 | |
| 55 | return; |
| 56 | } |
| 57 | |
| 58 | if (is_array($expected) || $expected instanceof RepeatedField) { |
| 59 | if (is_array($expected) === is_array($actual)) { |
| 60 | $this->assertEquals($expected, $actual); |
| 61 | } |
| 62 | |
| 63 | $this->assertCount(count($expected), $actual); |
| 64 | |
| 65 | $expectedValues = $this->getValues($expected); |
| 66 | $actualValues = $this->getValues($actual); |
| 67 | |
| 68 | for ($i = 0; $i < count($expectedValues); $i++) { |
| 69 | $expectedElement = $expectedValues[$i]; |
| 70 | $actualElement = $actualValues[$i]; |
| 71 | $this->assertProtobufEquals($expectedElement, $actualElement); |
| 72 | } |
| 73 | } else { |
| 74 | $this->assertEquals($expected, $actual); |
| 75 | if ($expected instanceof Message) { |
| 76 | $pool = DescriptorPool::getGeneratedPool(); |
| 77 | $descriptor = $pool->getDescriptorByClassName(get_class($expected)); |
| 78 | |
| 79 | $fieldCount = $descriptor->getFieldCount(); |
| 80 | for ($i = 0; $i < $fieldCount; $i++) { |
| 81 | $field = $descriptor->getField($i); |
| 82 | $getter = Serializer::getGetter($field->getName()); |
| 83 | $expectedFieldValue = $expected->$getter(); |
| 84 | $actualFieldValue = $actual->$getter(); |
| 85 | $this->assertProtobufEquals($expectedFieldValue, $actualFieldValue); |
| 86 | } |
| 87 | } |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * @param iterable $field |
| 93 | */ |
| 94 | private function getValues($field) |
| 95 | { |
| 96 | return array_values( |
| 97 | is_array($field) |
| 98 | ? $field |
| 99 | : iterator_to_array($field) |
| 100 | ); |
| 101 | } |
| 102 | } |
| 103 |