|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * Tests for the XML-RPC demo.addTwoNumbers method. |
| 5 | + * |
| 6 | + * @group xmlrpc |
| 7 | + * |
| 8 | + * @covers wp_xmlrpc_server::addTwoNumbers |
| 9 | + */ |
| 10 | +class Tests_XMLRPC_demo_addTwoNumbers extends WP_XMLRPC_UnitTestCase { |
| 11 | + |
| 12 | + /** |
| 13 | + * Tests that addTwoNumbers returns the correct sum for valid integer inputs. |
| 14 | + * |
| 15 | + * @dataProvider data_valid_integers |
| 16 | + * |
| 17 | + * @param int $a First number. |
| 18 | + * @param int $b Second number. |
| 19 | + * @param int $expected Expected sum. |
| 20 | + */ |
| 21 | + public function test_add_two_numbers_with_valid_integers( $a, $b, $expected ) { |
| 22 | + $result = $this->myxmlrpcserver->addTwoNumbers( array( $a, $b ) ); |
| 23 | + |
| 24 | + $this->assertNotIXRError( $result ); |
| 25 | + $this->assertSame( $expected, $result ); |
| 26 | + } |
| 27 | + |
| 28 | + /** |
| 29 | + * Data provider for test_add_two_numbers_with_valid_integers. |
| 30 | + * |
| 31 | + * @return array<string, int[]> |
| 32 | + */ |
| 33 | + public function data_valid_integers(): array { |
| 34 | + return array( |
| 35 | + 'two positive integers' => array( 3, 5, 8 ), |
| 36 | + 'positive and negative integer' => array( 10, -3, 7 ), |
| 37 | + 'two negative integers' => array( -5, -7, -12 ), |
| 38 | + 'zero and positive integer' => array( 0, 42, 42 ), |
| 39 | + 'zero and negative integer' => array( 0, -42, -42 ), |
| 40 | + 'two zeros' => array( 0, 0, 0 ), |
| 41 | + 'large integers' => array( 1000000, 2000000, 3000000 ), |
| 42 | + ); |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * Tests that addTwoNumbers returns an error when invalid types are passed. |
| 47 | + * |
| 48 | + * @dataProvider data_invalid_arguments |
| 49 | + * |
| 50 | + * @param mixed $a First argument. |
| 51 | + * @param mixed $b Second argument. |
| 52 | + * @param string $message Description of the test case. |
| 53 | + */ |
| 54 | + public function test_add_two_numbers_with_invalid_arguments( $a, $b, $message ) { |
| 55 | + $result = $this->myxmlrpcserver->addTwoNumbers( array( $a, $b ) ); |
| 56 | + |
| 57 | + $this->assertIsNotInt( $result ); |
| 58 | + $this->assertSame( 400, $result->code, $message ); |
| 59 | + $this->assertSame( |
| 60 | + 'Invalid arguments passed to this XML-RPC method. Requires two integers.', |
| 61 | + $result->message, |
| 62 | + $message |
| 63 | + ); |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * Data provider for test_add_two_numbers_with_invalid_arguments. |
| 68 | + * |
| 69 | + * @return array<string, array<mixed>> |
| 70 | + */ |
| 71 | + public function data_invalid_arguments(): array { |
| 72 | + return array( |
| 73 | + 'first argument is string' => array( 'abc', 5, 'Should fail when first argument is a string.' ), |
| 74 | + 'second argument is string' => array( 3, 'abc', 'Should fail when second argument is a string.' ), |
| 75 | + 'both arguments are strings' => array( 'foo', 'bar', 'Should fail when both arguments are strings.' ), |
| 76 | + 'first argument is float' => array( 3.14, 5, 'Should fail when first argument is a float.' ), |
| 77 | + 'second argument is float' => array( 3, 5.5, 'Should fail when second argument is a float.' ), |
| 78 | + 'both arguments are floats' => array( 1.1, 2.2, 'Should fail when both arguments are floats.' ), |
| 79 | + 'first argument is null' => array( null, 5, 'Should fail when first argument is null.' ), |
| 80 | + 'second argument is null' => array( 3, null, 'Should fail when second argument is null.' ), |
| 81 | + 'first argument is boolean' => array( true, 5, 'Should fail when first argument is boolean.' ), |
| 82 | + 'second argument is boolean' => array( 3, false, 'Should fail when second argument is boolean.' ), |
| 83 | + 'first argument is array' => array( array( 1 ), 5, 'Should fail when first argument is an array.' ), |
| 84 | + 'second argument is array' => array( 3, array( 2 ), 'Should fail when second argument is an array.' ), |
| 85 | + 'numeric string as first argument' => array( '3', 5, 'Should fail when first argument is a numeric string.' ), |
| 86 | + 'numeric string as second argument' => array( 3, '5', 'Should fail when second argument is a numeric string.' ), |
| 87 | + ); |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * Tests that addTwoNumbers returns an error when no arguments are passed. |
| 92 | + * |
| 93 | + * When zero XML-RPC params are provided, IXR_Server passes an empty array to the method. |
| 94 | + */ |
| 95 | + public function test_add_two_numbers_with_no_arguments() { |
| 96 | + $result = $this->myxmlrpcserver->addTwoNumbers( array() ); |
| 97 | + |
| 98 | + $this->assertIsNotInt( $result ); |
| 99 | + $this->assertSame( 400, $result->code ); |
| 100 | + $this->assertSame( |
| 101 | + 'Invalid arguments passed to this XML-RPC method. Requires two integers.', |
| 102 | + $result->message |
| 103 | + ); |
| 104 | + } |
| 105 | + |
| 106 | + /** |
| 107 | + * Tests that addTwoNumbers returns an error when only one argument is passed. |
| 108 | + * |
| 109 | + * When exactly one XML-RPC param is provided, IXR_Server unwraps it from the array |
| 110 | + * and passes the single value directly to the method (not wrapped in an array). |
| 111 | + * |
| 112 | + * @see IXR_Server::call() lines 95-98 |
| 113 | + */ |
| 114 | + public function test_add_two_numbers_with_one_argument() { |
| 115 | + // IXR_Server passes single param directly, not as array. |
| 116 | + $result = $this->myxmlrpcserver->addTwoNumbers( 5 ); |
| 117 | + |
| 118 | + $this->assertIsNotInt( $result ); |
| 119 | + $this->assertSame( 400, $result->code ); |
| 120 | + $this->assertSame( |
| 121 | + 'Invalid arguments passed to this XML-RPC method. Requires two integers.', |
| 122 | + $result->message |
| 123 | + ); |
| 124 | + } |
| 125 | + |
| 126 | + /** |
| 127 | + * Tests that addTwoNumbers returns an error when too many arguments are passed. |
| 128 | + * |
| 129 | + * @dataProvider data_too_many_arguments |
| 130 | + * |
| 131 | + * @param array $args Arguments to pass to addTwoNumbers. |
| 132 | + * @param string $message Description of the test case. |
| 133 | + */ |
| 134 | + public function test_add_two_numbers_with_too_many_arguments( $args, $message ) { |
| 135 | + $result = $this->myxmlrpcserver->addTwoNumbers( $args ); |
| 136 | + |
| 137 | + $this->assertIsNotInt( $result ); |
| 138 | + $this->assertSame( 400, $result->code, $message ); |
| 139 | + $this->assertSame( |
| 140 | + 'Invalid arguments passed to this XML-RPC method. Requires two integers.', |
| 141 | + $result->message, |
| 142 | + $message |
| 143 | + ); |
| 144 | + } |
| 145 | + |
| 146 | + /** |
| 147 | + * Data provider for test_add_two_numbers_with_too_many_arguments. |
| 148 | + * |
| 149 | + * @return array<string, array<mixed>> |
| 150 | + */ |
| 151 | + public function data_too_many_arguments(): array { |
| 152 | + return array( |
| 153 | + 'three arguments' => array( array( 3, 5, 100 ), 'Should fail with three arguments.' ), |
| 154 | + 'four arguments' => array( array( 10, 20, 30, 40 ), 'Should fail with four arguments.' ), |
| 155 | + ); |
| 156 | + } |
| 157 | +} |
0 commit comments