diff --git a/src/WireMock/Client/Authentication/Authenticator.php b/src/WireMock/Client/Authentication/Authenticator.php new file mode 100644 index 0000000..ece2a4f --- /dev/null +++ b/src/WireMock/Client/Authentication/Authenticator.php @@ -0,0 +1,18 @@ + $headers + * @return list + */ + public function modifyHeaders(array $headers): array; + + /** + * @param non-empty-string $url + * @return non-empty-string + */ + public function modifyUrl(string $url): string; +} \ No newline at end of file diff --git a/src/WireMock/Client/Authentication/NullAuthenticator.php b/src/WireMock/Client/Authentication/NullAuthenticator.php new file mode 100644 index 0000000..d0390c4 --- /dev/null +++ b/src/WireMock/Client/Authentication/NullAuthenticator.php @@ -0,0 +1,16 @@ +token = $token; + } + + public function modifyHeaders(array $headers): array + { + return array_merge($headers, [sprintf('Authorization: Token %s', $this->token)]); + } + + public function modifyUrl(string $url): string + { + return $url; + } +} \ No newline at end of file diff --git a/src/WireMock/Client/Curl.php b/src/WireMock/Client/Curl.php index cd0c077..f9688b7 100644 --- a/src/WireMock/Client/Curl.php +++ b/src/WireMock/Client/Curl.php @@ -2,8 +2,21 @@ namespace WireMock\Client; +use WireMock\Client\Authentication\Authenticator; +use WireMock\Client\Authentication\NullAuthenticator; + class Curl { + /** + * @var Authenticator + */ + private $authenticator; + + public function __construct(?Authenticator $authenticator = null) + { + $this->authenticator = $authenticator ?? new NullAuthenticator(); + } + /** * @param string $url * @return string The response body @@ -51,7 +64,7 @@ public function delete(string $url): string */ private function makeCurlRequest(string $method, string $url, ?string $json = null) { - $ch = curl_init($url); + $ch = curl_init($this->authenticator->modifyUrl($url)); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method); if ($json !== null) { curl_setopt($ch, CURLOPT_POSTFIELDS, $json); @@ -60,10 +73,12 @@ private function makeCurlRequest(string $method, string $url, ?string $json = nu $contentLength = 0; } curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); - curl_setopt($ch, CURLOPT_HTTPHEADER, array( + + $defaultHeaders = [ 'Content-Type: application/json', - "Content-Length: $contentLength", - )); + sprintf('Content-Length: %s', $contentLength), + ]; + curl_setopt($ch, CURLOPT_HTTPHEADER, $this->authenticator->modifyHeaders($defaultHeaders)); $result = curl_exec($ch); diff --git a/src/WireMock/Client/HttpWait.php b/src/WireMock/Client/HttpWait.php index 761b3d7..6f313f3 100644 --- a/src/WireMock/Client/HttpWait.php +++ b/src/WireMock/Client/HttpWait.php @@ -4,6 +4,16 @@ class HttpWait { + /** + * @var Curl + */ + private $curl; + + public function __construct(?Curl $curl = null) + { + $this->curl = $curl ?? new Curl(); + } + public function waitForServerToGive200($url, $timeoutSecs = 10, $debug = true) { $debugTrace = array(); @@ -11,26 +21,18 @@ public function waitForServerToGive200($url, $timeoutSecs = 10, $debug = true) $serverStarted = false; while (microtime(true) - $startTime < $timeoutSecs) { try { - $headers = @get_headers($url, 1); - } catch (\Exception $e) { - $debugTrace[] = "$url not yet up. Error getting headers: " . $e->getMessage(); - continue; - } - - if (isset($headers) && isset($headers[0]) && strpos($headers[0], '200 OK') !== false) { + $this->curl->get($url); $serverStarted = true; break; - } else { - if (!isset($headers)) { - $debugTrace[] = "$url not yet up. \$headers not set"; - } else if (!isset($headers[0])) { - $debugTrace[] = "$url not yet up. \$headers[0] not set"; - } else { - $debugTrace[] = "$url not yet up. \$headers[0] was {$headers[0]}"; - } + } catch (\Exception $e) { + $debugTrace[] = "$url not yet up. " . $e->getMessage(); + + usleep(100000); + + continue; } - usleep(100000); } + if (!$serverStarted) { $time = microtime(true) - $startTime; if ($debug) { diff --git a/src/WireMock/Client/LogicalOperatorMatchingStrategy.php b/src/WireMock/Client/LogicalOperatorMatchingStrategy.php index eef4c5a..e646ef0 100644 --- a/src/WireMock/Client/LogicalOperatorMatchingStrategy.php +++ b/src/WireMock/Client/LogicalOperatorMatchingStrategy.php @@ -30,4 +30,4 @@ public static function orAll(...$matchers): LogicalOperatorMatchingStrategy { return new self("or", $matchers); } -} \ No newline at end of file +} diff --git a/src/WireMock/Client/WireMock.php b/src/WireMock/Client/WireMock.php index c4713c2..9ad4f38 100644 --- a/src/WireMock/Client/WireMock.php +++ b/src/WireMock/Client/WireMock.php @@ -24,6 +24,8 @@ class WireMock private $hostname; /** @var int */ private $port; + /** @var string */ + private $scheme; /** @var HttpWait */ private $httpWait; /** @var Curl */ @@ -45,13 +47,15 @@ public function __construct( Curl $curl, Serializer $serializer, $hostname = 'localhost', - $port = 8080 + $port = 8080, + $scheme = 'http' ) { - $this->hostname = $hostname; - $this->port = $port; $this->httpWait = $httpWait; $this->curl = $curl; $this->serializer = $serializer; + $this->hostname = $hostname; + $this->port = $port; + $this->scheme = $scheme; } public function isAlive($timeoutSecs = 10, $debug = true) @@ -98,11 +102,11 @@ public function importStubs($stubImportsOrBuilder) /** * @param RequestPatternBuilder|CountMatchingStrategy|integer $requestPatternBuilderOrCount - * @param RequestPatternBuilder|null $requestPatternBuilder + * @param ?RequestPatternBuilder $requestPatternBuilder * @throws ClientException * @throws VerificationException */ - public function verify($requestPatternBuilderOrCount, RequestPatternBuilder $requestPatternBuilder = null) + public function verify($requestPatternBuilderOrCount, ?RequestPatternBuilder $requestPatternBuilder = null) { if ($requestPatternBuilderOrCount instanceof CountMatchingStrategy) { $patternBuilder = $requestPatternBuilder; @@ -506,9 +510,9 @@ private function doDelete(string $path, ?string $resultType = null) } } - private function _makeUrl($path) + private function _makeUrl($path): string { - return "http://$this->hostname:$this->port/$path"; + return sprintf('%s://%s:%d/%s', $this->scheme, $this->hostname, $this->port, $path); } /** diff --git a/src/WireMock/Serde/PropNaming/ReferencingPropertyNamingStrategy.php b/src/WireMock/Serde/PropNaming/ReferencingPropertyNamingStrategy.php index 1cf7c32..2d849d1 100644 --- a/src/WireMock/Serde/PropNaming/ReferencingPropertyNamingStrategy.php +++ b/src/WireMock/Serde/PropNaming/ReferencingPropertyNamingStrategy.php @@ -4,6 +4,7 @@ use ReflectionException; use ReflectionMethod; +use WireMock\Serde\ReflectionUtils; use WireMock\Serde\SerializationException; use WireMock\Serde\StaticFactoryMethodValidator; @@ -47,8 +48,8 @@ function getSerializedName(array $data): string */ function getPossibleSerializedNames(): array { - $refMethod = new ReflectionMethod($this->possibleNamesGenerator); + $refMethod = ReflectionUtils::reflectMethod($this->possibleNamesGenerator); $refMethod->setAccessible(true); return $refMethod->invoke(null); } -} \ No newline at end of file +} diff --git a/src/WireMock/Serde/ReflectionUtils.php b/src/WireMock/Serde/ReflectionUtils.php new file mode 100644 index 0000000..2a25239 --- /dev/null +++ b/src/WireMock/Serde/ReflectionUtils.php @@ -0,0 +1,20 @@ += 80300) { + $refMethod = ReflectionMethod::createFromMethodName($methodName); + } else { + $refMethod = new ReflectionMethod($methodName); + } + + return $refMethod; + } +} diff --git a/src/WireMock/Serde/SerdeClassDiscriminationInfo.php b/src/WireMock/Serde/SerdeClassDiscriminationInfo.php index 6ccea38..823069c 100644 --- a/src/WireMock/Serde/SerdeClassDiscriminationInfo.php +++ b/src/WireMock/Serde/SerdeClassDiscriminationInfo.php @@ -36,7 +36,7 @@ public function __construct(string $discriminatorFactoryName, array $possibleSer */ public function getDiscriminator(): ?ClassDiscriminator { - $refMethod = new ReflectionMethod($this->discriminatorFactoryName); + $refMethod = ReflectionUtils::reflectMethod($this->discriminatorFactoryName); $refMethod->setAccessible(true); return $refMethod->invoke(null); } @@ -51,4 +51,4 @@ public function getTypeByFullyQualifiedClassName(string $fqn): SerdeTypeClass } return $this->possibleSerdeTypes[$fqn]; } -} \ No newline at end of file +} diff --git a/src/WireMock/Serde/SerdeProp.php b/src/WireMock/Serde/SerdeProp.php index d6b2ec8..2c5477c 100644 --- a/src/WireMock/Serde/SerdeProp.php +++ b/src/WireMock/Serde/SerdeProp.php @@ -38,7 +38,7 @@ public function __construct( string $name, string $owningClassName, SerdeType $serdeType, - PropertyNamingStrategy $propertyNamingStrategy = null, + ?PropertyNamingStrategy $propertyNamingStrategy = null, bool $unwrapped = false, bool $catchAll = false ) @@ -105,4 +105,4 @@ public function getPotentiallyNullableSerdeTypeClassOrThrow(): SerdeTypeClass { } throw new SerializationException("Cannot get SerdeTypeClass for prop $this->name because it is of type " . $this->serdeType->displayName()); } -} \ No newline at end of file +} diff --git a/src/WireMock/Serde/StaticFactoryMethodValidator.php b/src/WireMock/Serde/StaticFactoryMethodValidator.php index 6e39102..6659e5c 100644 --- a/src/WireMock/Serde/StaticFactoryMethodValidator.php +++ b/src/WireMock/Serde/StaticFactoryMethodValidator.php @@ -12,7 +12,7 @@ class StaticFactoryMethodValidator * @throws SerializationException */ public static function validate($fqMethodName) { - $refMethod = new ReflectionMethod($fqMethodName); + $refMethod = ReflectionUtils::reflectMethod($fqMethodName); if (!$refMethod->isStatic()) { throw new SerializationException("$fqMethodName must be a static method but is not"); } @@ -21,4 +21,4 @@ public static function validate($fqMethodName) { throw new SerializationException("$fqMethodName must take no required args, but requires $numRequiredParams"); } } -} \ No newline at end of file +} diff --git a/test/WireMock/Integration/CloudIntegrationTest.php b/test/WireMock/Integration/CloudIntegrationTest.php new file mode 100644 index 0000000..96746c9 --- /dev/null +++ b/test/WireMock/Integration/CloudIntegrationTest.php @@ -0,0 +1,27 @@ +isAlive()); + } +}