|
| 1 | +<?php |
| 2 | +// Copyright 2004-present Facebook. All Rights Reserved. |
| 3 | +// |
| 4 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +// you may not use this file except in compliance with the License. |
| 6 | +// You may obtain a copy of the License at |
| 7 | +// |
| 8 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +// |
| 10 | +// Unless required by applicable law or agreed to in writing, software |
| 11 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +// See the License for the specific language governing permissions and |
| 14 | +// limitations under the License. |
| 15 | + |
| 16 | +namespace Facebook\WebDriver\Remote; |
| 17 | + |
| 18 | +use phpmock\phpunit\PHPMock; |
| 19 | + |
| 20 | +class HttpCommandExecutorTest extends \PHPUnit_Framework_TestCase |
| 21 | +{ |
| 22 | + use PHPMock; |
| 23 | + /** @var HttpCommandExecutor */ |
| 24 | + private $executor; |
| 25 | + |
| 26 | + public function setUp() |
| 27 | + { |
| 28 | + $this->executor = new HttpCommandExecutor('http://localhost:4444'); |
| 29 | + } |
| 30 | + |
| 31 | + /** |
| 32 | + * @dataProvider commandProvider |
| 33 | + * @param int $command |
| 34 | + * @param array $params |
| 35 | + * @param string $expectedUrl |
| 36 | + * @param string $expectedPostData |
| 37 | + */ |
| 38 | + public function testShouldSendRequestToAssembledUrl($command, array $params, $expectedUrl, $expectedPostData) |
| 39 | + { |
| 40 | + $command = new WebDriverCommand('foo-123', $command, $params); |
| 41 | + |
| 42 | + $curlSetoptMock = $this->getFunctionMock(__NAMESPACE__, 'curl_setopt'); |
| 43 | + $curlSetoptMock->expects($this->at(0)) |
| 44 | + ->with($this->anything(), CURLOPT_URL, $expectedUrl); |
| 45 | + |
| 46 | + $curlSetoptMock->expects($this->at(2)) |
| 47 | + ->with($this->anything(), CURLOPT_POSTFIELDS, $expectedPostData); |
| 48 | + |
| 49 | + $curlExecMock = $this->getFunctionMock(__NAMESPACE__, 'curl_exec'); |
| 50 | + $curlExecMock->expects($this->once()) |
| 51 | + ->willReturn('{}'); |
| 52 | + |
| 53 | + $this->executor->execute($command); |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * @return array[] |
| 58 | + */ |
| 59 | + public function commandProvider() |
| 60 | + { |
| 61 | + return [ |
| 62 | + 'POST command having :id placeholder in url' => [ |
| 63 | + DriverCommand::SEND_KEYS_TO_ELEMENT, |
| 64 | + ['value' => 'submitted-value', ':id' => '1337'], |
| 65 | + 'http://localhost:4444/session/foo-123/element/1337/value', |
| 66 | + '{"value":"submitted-value"}', |
| 67 | + ], |
| 68 | + 'POST command without :id placeholder in url' => [ |
| 69 | + DriverCommand::TOUCH_UP, |
| 70 | + ['x' => 3, 'y' => 6], |
| 71 | + 'http://localhost:4444/session/foo-123/touch/up', |
| 72 | + '{"x":3,"y":6}', |
| 73 | + ], |
| 74 | + 'Extra useless placeholder parameter should be removed' => [ |
| 75 | + DriverCommand::TOUCH_UP, |
| 76 | + ['x' => 3, 'y' => 6, ':useless' => 'foo'], |
| 77 | + 'http://localhost:4444/session/foo-123/touch/up', |
| 78 | + '{"x":3,"y":6}', |
| 79 | + ], |
| 80 | + 'DELETE command' => [ |
| 81 | + DriverCommand::DELETE_COOKIE, |
| 82 | + [':name' => 'cookie-name'], |
| 83 | + 'http://localhost:4444/session/foo-123/cookie/cookie-name', |
| 84 | + null, |
| 85 | + ], |
| 86 | + 'GET command without session in URL' => [ |
| 87 | + DriverCommand::GET_ALL_SESSIONS, |
| 88 | + [], |
| 89 | + 'http://localhost:4444/sessions', |
| 90 | + null, |
| 91 | + ], |
| 92 | + ]; |
| 93 | + } |
| 94 | +} |
0 commit comments