WrapperTypeSettersTest.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. <?php
  2. require_once('test_base.php');
  3. require_once('test_util.php');
  4. use Foo\TestWrapperSetters;
  5. use Google\Protobuf\BoolValue;
  6. use Google\Protobuf\BytesValue;
  7. use Google\Protobuf\DoubleValue;
  8. use Google\Protobuf\FloatValue;
  9. use Google\Protobuf\Int32Value;
  10. use Google\Protobuf\Int64Value;
  11. use Google\Protobuf\StringValue;
  12. use Google\Protobuf\UInt32Value;
  13. use Google\Protobuf\UInt64Value;
  14. class WrapperTypeSettersTest extends TestBase
  15. {
  16. /**
  17. * @dataProvider gettersAndSettersDataProvider
  18. */
  19. public function testGettersAndSetters(
  20. $class,
  21. $wrapperClass,
  22. $setter,
  23. $valueSetter,
  24. $getter,
  25. $valueGetter,
  26. $sequence
  27. ) {
  28. $oldSetterMsg = new $class();
  29. $newSetterMsg = new $class();
  30. foreach ($sequence as list($value, $expectedValue)) {
  31. // Manually wrap the value to pass to the old setter
  32. $wrappedValue = is_null($value) ? $value : new $wrapperClass(['value' => $value]);
  33. // Set values using new and old setters
  34. $oldSetterMsg->$setter($wrappedValue);
  35. $newSetterMsg->$valueSetter($value);
  36. // Get expected values old getter
  37. $expectedValue = $oldSetterMsg->$getter();
  38. // Check that old getter returns the same value after using the
  39. // new setter
  40. $actualValue = $newSetterMsg->$getter();
  41. $this->assertEquals($expectedValue, $actualValue);
  42. // Check that new getter returns the unwrapped value from
  43. // $expectedValue
  44. $actualValueNewGetter = $newSetterMsg->$valueGetter();
  45. if (is_null($expectedValue)) {
  46. $this->assertNull($actualValueNewGetter);
  47. } else {
  48. $this->assertEquals($expectedValue->getValue(), $actualValueNewGetter);
  49. }
  50. }
  51. }
  52. public function gettersAndSettersDataProvider()
  53. {
  54. return [
  55. [TestWrapperSetters::class, DoubleValue::class, "setDoubleValue", "setDoubleValueUnwrapped", "getDoubleValue", "getDoubleValueUnwrapped", [
  56. [1.1, new DoubleValue(["value" => 1.1])],
  57. [2.2, new DoubleValue(["value" => 2.2])],
  58. [null, null],
  59. [0, new DoubleValue()],
  60. ]],
  61. [TestWrapperSetters::class, FloatValue::class, "setFloatValue", "setFloatValueUnwrapped", "getFloatValue", "getFloatValueUnwrapped", [
  62. [1.1, new FloatValue(["value" => 1.1])],
  63. [2.2, new FloatValue(["value" => 2.2])],
  64. [null, null],
  65. [0, new FloatValue()],
  66. ]],
  67. [TestWrapperSetters::class, Int64Value::class, "setInt64Value", "setInt64ValueUnwrapped", "getInt64Value", "getInt64ValueUnwrapped", [
  68. [123, new Int64Value(["value" => 123])],
  69. [-789, new Int64Value(["value" => -789])],
  70. [null, null],
  71. [0, new Int64Value()],
  72. [5.5, new Int64Value(["value" => 5])], // Test conversion from float to int
  73. ]],
  74. [TestWrapperSetters::class, UInt64Value::class, "setUInt64Value", "setUInt64ValueUnwrapped", "getUInt64Value", "getUInt64ValueUnwrapped", [
  75. [123, new UInt64Value(["value" => 123])],
  76. [789, new UInt64Value(["value" => 789])],
  77. [null, null],
  78. [0, new UInt64Value()],
  79. [5.5, new UInt64Value(["value" => 5])], // Test conversion from float to int
  80. [-7, new UInt64Value(["value" => -7])], // Test conversion from -ve to +ve
  81. ]],
  82. [TestWrapperSetters::class, Int32Value::class, "setInt32Value", "setInt32ValueUnwrapped", "getInt32Value", "getInt32ValueUnwrapped", [
  83. [123, new Int32Value(["value" => 123])],
  84. [-789, new Int32Value(["value" => -789])],
  85. [null, null],
  86. [0, new Int32Value()],
  87. [5.5, new Int32Value(["value" => 5])], // Test conversion from float to int
  88. ]],
  89. [TestWrapperSetters::class, UInt32Value::class, "setUInt32Value", "setUInt32ValueUnwrapped", "getUInt32Value", "getUInt32ValueUnwrapped", [
  90. [123, new UInt32Value(["value" => 123])],
  91. [789, new UInt32Value(["value" => 789])],
  92. [null, null],
  93. [0, new UInt32Value()],
  94. [5.5, new UInt32Value(["value" => 5])], // Test conversion from float to int
  95. [-7, new UInt32Value(["value" => -7])], // Test conversion from -ve to +ve
  96. ]],
  97. [TestWrapperSetters::class, BoolValue::class, "setBoolValue", "setBoolValueUnwrapped", "getBoolValue", "getBoolValueUnwrapped", [
  98. [true, new BoolValue(["value" => true])],
  99. [false, new BoolValue(["value" => false])],
  100. [null, null],
  101. ]],
  102. [TestWrapperSetters::class, StringValue::class, "setStringValue", "setStringValueUnwrapped", "getStringValue", "getStringValueUnwrapped", [
  103. ["asdf", new StringValue(["value" => "asdf"])],
  104. ["", new StringValue(["value" => ""])],
  105. [null, null],
  106. ["", new StringValue()],
  107. [5, new StringValue(["value" => "5"])], // Test conversion from number to string
  108. [5.5, new StringValue(["value" => "5.5"])], // Test conversion from number to string
  109. [-7, new StringValue(["value" => "-7"])], // Test conversion from number to string
  110. [-7.5, new StringValue(["value" => "-7.5"])], // Test conversion from number to string
  111. ]],
  112. [TestWrapperSetters::class, BytesValue::class, "setBytesValue", "setBytesValueUnwrapped", "getBytesValue", "getBytesValueUnwrapped", [
  113. ["asdf", new BytesValue(["value" => "asdf"])],
  114. ["", new BytesValue(["value" => ""])],
  115. [null, null],
  116. ["", new BytesValue()],
  117. [5, new BytesValue(["value" => "5"])], // Test conversion from number to bytes
  118. [5.5, new BytesValue(["value" => "5.5"])], // Test conversion from number to bytes
  119. [-7, new BytesValue(["value" => "-7"])], // Test conversion from number to bytes
  120. [-7.5, new BytesValue(["value" => "-7.5"])], // Test conversion from number to bytes
  121. ]],
  122. [TestWrapperSetters::class, DoubleValue::class, "setDoubleValueOneof", "setDoubleValueOneofUnwrapped", "getDoubleValueOneof", "getDoubleValueOneofUnwrapped", [
  123. [1.1, new DoubleValue(["value" => 1.1])],
  124. [2.2, new DoubleValue(["value" => 2.2])],
  125. [null, null],
  126. [0, new DoubleValue()],
  127. ]],
  128. [TestWrapperSetters::class, StringValue::class, "setStringValueOneof", "setStringValueOneofUnwrapped", "getStringValueOneof", "getStringValueOneofUnwrapped", [
  129. ["asdf", new StringValue(["value" => "asdf"])],
  130. ["", new StringValue(["value" => ""])],
  131. [null, null],
  132. ["", new StringValue()],
  133. [5, new StringValue(["value" => "5"])], // Test conversion from number to string
  134. [5.5, new StringValue(["value" => "5.5"])], // Test conversion from number to string
  135. [-7, new StringValue(["value" => "-7"])], // Test conversion from number to string
  136. [-7.5, new StringValue(["value" => "-7.5"])], // Test conversion from number to string
  137. ]],
  138. ];
  139. }
  140. /**
  141. * @dataProvider invalidSettersDataProvider
  142. */
  143. public function testInvalidSetters($class, $setter, $value)
  144. {
  145. $this->expectException(Exception::class);
  146. (new $class())->$setter($value);
  147. }
  148. public function invalidSettersDataProvider()
  149. {
  150. return [
  151. [TestWrapperSetters::class, "setDoubleValueUnwrapped", "abc"],
  152. [TestWrapperSetters::class, "setDoubleValueUnwrapped", []],
  153. [TestWrapperSetters::class, "setDoubleValueUnwrapped", new stdClass()],
  154. [TestWrapperSetters::class, "setDoubleValueUnwrapped", new DoubleValue()],
  155. [TestWrapperSetters::class, "setFloatValueUnwrapped", "abc"],
  156. [TestWrapperSetters::class, "setFloatValueUnwrapped", []],
  157. [TestWrapperSetters::class, "setFloatValueUnwrapped", new stdClass()],
  158. [TestWrapperSetters::class, "setFloatValueUnwrapped", new FloatValue()],
  159. [TestWrapperSetters::class, "setInt64ValueUnwrapped", "abc"],
  160. [TestWrapperSetters::class, "setInt64ValueUnwrapped", []],
  161. [TestWrapperSetters::class, "setInt64ValueUnwrapped", new stdClass()],
  162. [TestWrapperSetters::class, "setInt64ValueUnwrapped", new Int64Value()],
  163. [TestWrapperSetters::class, "setUInt64ValueUnwrapped", "abc"],
  164. [TestWrapperSetters::class, "setUInt64ValueUnwrapped", []],
  165. [TestWrapperSetters::class, "setUInt64ValueUnwrapped", new stdClass()],
  166. [TestWrapperSetters::class, "setUInt64ValueUnwrapped", new UInt64Value()],
  167. [TestWrapperSetters::class, "setInt32ValueUnwrapped", "abc"],
  168. [TestWrapperSetters::class, "setInt32ValueUnwrapped", []],
  169. [TestWrapperSetters::class, "setInt32ValueUnwrapped", new stdClass()],
  170. [TestWrapperSetters::class, "setInt32ValueUnwrapped", new Int32Value()],
  171. [TestWrapperSetters::class, "setUInt32ValueUnwrapped", "abc"],
  172. [TestWrapperSetters::class, "setUInt32ValueUnwrapped", []],
  173. [TestWrapperSetters::class, "setUInt32ValueUnwrapped", new stdClass()],
  174. [TestWrapperSetters::class, "setUInt32ValueUnwrapped", new UInt32Value()],
  175. [TestWrapperSetters::class, "setBoolValueUnwrapped", []],
  176. [TestWrapperSetters::class, "setBoolValueUnwrapped", new stdClass()],
  177. [TestWrapperSetters::class, "setBoolValueUnwrapped", new BoolValue()],
  178. [TestWrapperSetters::class, "setStringValueUnwrapped", []],
  179. [TestWrapperSetters::class, "setStringValueUnwrapped", new stdClass()],
  180. [TestWrapperSetters::class, "setStringValueUnwrapped", new StringValue()],
  181. [TestWrapperSetters::class, "setBytesValueUnwrapped", []],
  182. [TestWrapperSetters::class, "setBytesValueUnwrapped", new stdClass()],
  183. [TestWrapperSetters::class, "setBytesValueUnwrapped", new BytesValue()],
  184. ];
  185. }
  186. /**
  187. * @dataProvider constructorWithWrapperTypeDataProvider
  188. */
  189. public function testConstructorWithWrapperType($class, $wrapperClass, $wrapperField, $getter, $value)
  190. {
  191. $actualInstance = new $class([$wrapperField => $value]);
  192. $expectedInstance = new $class([$wrapperField => new $wrapperClass(['value' => $value])]);
  193. $this->assertEquals($expectedInstance->$getter()->getValue(), $actualInstance->$getter()->getValue());
  194. }
  195. public function constructorWithWrapperTypeDataProvider()
  196. {
  197. return [
  198. [TestWrapperSetters::class, DoubleValue::class, 'double_value', 'getDoubleValue', 1.1],
  199. [TestWrapperSetters::class, FloatValue::class, 'float_value', 'getFloatValue', 2.2],
  200. [TestWrapperSetters::class, Int64Value::class, 'int64_value', 'getInt64Value', 3],
  201. [TestWrapperSetters::class, UInt64Value::class, 'uint64_value', 'getUInt64Value', 4],
  202. [TestWrapperSetters::class, Int32Value::class, 'int32_value', 'getInt32Value', 5],
  203. [TestWrapperSetters::class, UInt32Value::class, 'uint32_value', 'getUInt32Value', 6],
  204. [TestWrapperSetters::class, BoolValue::class, 'bool_value', 'getBoolValue', true],
  205. [TestWrapperSetters::class, StringValue::class, 'string_value', 'getStringValue', "eight"],
  206. [TestWrapperSetters::class, BytesValue::class, 'bytes_value', 'getBytesValue', "nine"],
  207. ];
  208. }
  209. /**
  210. * @dataProvider constructorWithRepeatedWrapperTypeDataProvider
  211. */
  212. public function testConstructorWithRepeatedWrapperType($wrapperField, $getter, $value)
  213. {
  214. $actualInstance = new TestWrapperSetters([$wrapperField => $value]);
  215. foreach ($actualInstance->$getter() as $key => $actualWrapperValue) {
  216. $actualInnerValue = $actualWrapperValue->getValue();
  217. $expectedElement = $value[$key];
  218. if (is_object($expectedElement) && is_a($expectedElement, '\Google\Protobuf\StringValue')) {
  219. $expectedInnerValue = $expectedElement->getValue();
  220. } else {
  221. $expectedInnerValue = $expectedElement;
  222. }
  223. $this->assertEquals($expectedInnerValue, $actualInnerValue);
  224. }
  225. $this->assertTrue(true);
  226. }
  227. public function constructorWithRepeatedWrapperTypeDataProvider()
  228. {
  229. $sv7 = new StringValue(['value' => 'seven']);
  230. $sv8 = new StringValue(['value' => 'eight']);
  231. $testWrapperSetters = new TestWrapperSetters();
  232. $testWrapperSetters->setRepeatedStringValue([$sv7, $sv8]);
  233. $repeatedField = $testWrapperSetters->getRepeatedStringValue();
  234. return [
  235. ['repeated_string_value', 'getRepeatedStringValue', []],
  236. ['repeated_string_value', 'getRepeatedStringValue', [$sv7]],
  237. ['repeated_string_value', 'getRepeatedStringValue', [$sv7, $sv8]],
  238. ['repeated_string_value', 'getRepeatedStringValue', ['seven']],
  239. ['repeated_string_value', 'getRepeatedStringValue', [7]],
  240. ['repeated_string_value', 'getRepeatedStringValue', [7.7]],
  241. ['repeated_string_value', 'getRepeatedStringValue', ['seven', 'eight']],
  242. ['repeated_string_value', 'getRepeatedStringValue', [$sv7, 'eight']],
  243. ['repeated_string_value', 'getRepeatedStringValue', ['seven', $sv8]],
  244. ['repeated_string_value', 'getRepeatedStringValue', $repeatedField],
  245. ];
  246. }
  247. /**
  248. * @dataProvider constructorWithMapWrapperTypeDataProvider
  249. */
  250. public function testConstructorWithMapWrapperType($wrapperField, $getter, $value)
  251. {
  252. $actualInstance = new TestWrapperSetters([$wrapperField => $value]);
  253. foreach ($actualInstance->$getter() as $key => $actualWrapperValue) {
  254. $actualInnerValue = $actualWrapperValue->getValue();
  255. $expectedElement = $value[$key];
  256. if (is_object($expectedElement) && is_a($expectedElement, '\Google\Protobuf\StringValue')) {
  257. $expectedInnerValue = $expectedElement->getValue();
  258. } elseif (is_object($expectedElement) && is_a($expectedElement, '\Google\Protobuf\Internal\MapEntry')) {
  259. $expectedInnerValue = $expectedElement->getValue()->getValue();
  260. } else {
  261. $expectedInnerValue = $expectedElement;
  262. }
  263. $this->assertEquals($expectedInnerValue, $actualInnerValue);
  264. }
  265. $this->assertTrue(true);
  266. }
  267. public function constructorWithMapWrapperTypeDataProvider()
  268. {
  269. $sv7 = new StringValue(['value' => 'seven']);
  270. $sv8 = new StringValue(['value' => 'eight']);
  271. $testWrapperSetters = new TestWrapperSetters();
  272. $testWrapperSetters->setMapStringValue(['key' => $sv7, 'key2' => $sv8]);
  273. $mapField = $testWrapperSetters->getMapStringValue();
  274. return [
  275. ['map_string_value', 'getMapStringValue', []],
  276. ['map_string_value', 'getMapStringValue', ['key' => $sv7]],
  277. ['map_string_value', 'getMapStringValue', ['key' => $sv7, 'key2' => $sv8]],
  278. ['map_string_value', 'getMapStringValue', ['key' => 'seven']],
  279. ['map_string_value', 'getMapStringValue', ['key' => 7]],
  280. ['map_string_value', 'getMapStringValue', ['key' => 7.7]],
  281. ['map_string_value', 'getMapStringValue', ['key' => 'seven', 'key2' => 'eight']],
  282. ['map_string_value', 'getMapStringValue', ['key' => $sv7, 'key2' => 'eight']],
  283. ['map_string_value', 'getMapStringValue', ['key' => 'seven', 'key2' => $sv8]],
  284. ['map_string_value', 'getMapStringValue', $mapField],
  285. ];
  286. }
  287. }