private function waitForExpectedResponse($expectation, $timeout)
    {
        $response           = null;
        $lastLoggedResponse = null;
        $buffer             = '';
        $start              = time();
        stream_set_blocking($this->pipes[1], false);

        while (true) {
            if (time() - $start >= $timeout) {
                throw new ProcessTimeoutException();
            }

            if (feof($this->pipes[1])) {
                throw new UnexpectedEOFException();
            }

            if (!$this->isRunning()) {
                throw new ProcessTerminatedException();
            }

            $buffer .= fread($this->pipes[1], 4096);
            $response = static::trimAnswer($buffer);

            if ($response !== '' && $response !== $lastLoggedResponse) {
                $lastLoggedResponse = $response;
                $this->logger->info("Expected '{$expectation}', got '{$response}'");
            }

            if (fnmatch($expectation, $response)) {
                return;
            }
        }
    }