summaryrefslogtreecommitdiff
path: root/server/vendor/php-opencloud/common/tests/integration/TestCase.php
diff options
context:
space:
mode:
authorroot <root@kabir-PC>2016-03-23 11:31:51 +0100
committerroot <root@kabir-PC>2016-03-23 11:31:51 +0100
commita26989103d70fb0dd3ff6834de107cae246778c3 (patch)
tree0f243c83b790ffb57f19261fc2a509131f6776ce /server/vendor/php-opencloud/common/tests/integration/TestCase.php
parent1342db60283cb61a1c3810993575d35b9fb33ac0 (diff)
parent6e78d76f887d1149ea85bfb06db7ee7ad7435f5a (diff)
Merge branch 'develop' of https://github.com/manzerbredes/istic-openstack into develop
Diffstat (limited to 'server/vendor/php-opencloud/common/tests/integration/TestCase.php')
-rw-r--r--server/vendor/php-opencloud/common/tests/integration/TestCase.php115
1 files changed, 115 insertions, 0 deletions
diff --git a/server/vendor/php-opencloud/common/tests/integration/TestCase.php b/server/vendor/php-opencloud/common/tests/integration/TestCase.php
new file mode 100644
index 0000000..687760a
--- /dev/null
+++ b/server/vendor/php-opencloud/common/tests/integration/TestCase.php
@@ -0,0 +1,115 @@
+<?php
+
+namespace OpenCloud\integration;
+
+use Psr\Log\LoggerInterface;
+
+abstract class TestCase extends \PHPUnit_Framework_TestCase implements TestInterface
+{
+ private $logger;
+ private $startPoint;
+ private $lastPoint;
+ private $sampleManager;
+
+ public function __construct(LoggerInterface $logger, SampleManagerInterface $sampleManager)
+ {
+ $this->logger = $logger;
+ $this->sampleManager = $sampleManager;
+ }
+
+ public function teardown()
+ {
+ $this->sampleManager->deletePaths();
+ }
+
+ public function runOneTest($name)
+ {
+ if (!method_exists($this, $name)) {
+ throw new \InvalidArgumentException(sprintf("%s method does not exist", $name));
+ }
+
+ $this->startTimer();
+ $this->$name();
+ $this->outputTimeTaken();
+ }
+
+ protected function startTimer()
+ {
+ $this->startPoint = $this->lastPoint = microtime(true);
+ }
+
+ private function wrapColor($message, $colorPrefix)
+ {
+ return sprintf("%s%s", $colorPrefix, $message) . "\033[0m\033[1;0m";
+ }
+
+ protected function logStep($message, array $context = [])
+ {
+ $duration = microtime(true) - $this->lastPoint;
+
+ $stepTimeTaken = sprintf('(%s)', $this->formatSecDifference($duration));
+
+ if ($duration >= 10) {
+ $color = "\033[0m\033[1;31m"; // red
+ } elseif ($duration >= 2) {
+ $color = "\033[0m\033[1;33m"; // yellow
+ } else {
+ $color = "\033[0m\033[1;32m"; // green
+ }
+
+ $message = '{timeTaken} ' . $message;
+ $context['{timeTaken}'] = $this->wrapColor($stepTimeTaken, $color);
+
+ $this->logger->info($message, $context);
+
+ $this->lastPoint = microtime(true);
+ }
+
+ protected function randomStr($length = 5)
+ {
+ $chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
+ $charsLen = strlen($chars);
+
+ $randomString = '';
+ for ($i = 0; $i < $length; $i++) {
+ $randomString .= $chars[rand(0, $charsLen - 1)];
+ }
+
+ return 'phptest_' . $randomString;
+ }
+
+ private function formatMinDifference($duration)
+ {
+ $output = '';
+
+ if (($minutes = floor($duration / 60)) > 0) {
+ $output .= $minutes . 'min' . (($minutes > 1) ? 's' : '');
+ }
+
+ if (($seconds = number_format(fmod($duration, 60), 2)) > 0) {
+ if ($minutes > 0) {
+ $output .= ' ';
+ }
+ $output .= $seconds . 's';
+ }
+
+ return $output;
+ }
+
+ private function formatSecDifference($duration)
+ {
+ return number_format($duration, 2) . 's';
+ }
+
+ protected function outputTimeTaken()
+ {
+ $output = $this->formatMinDifference(microtime(true) - $this->startPoint);
+
+ $this->logger->info('Finished all tests! Time taken: {output}.', ['{output}' => $output]);
+ }
+
+ protected function sampleFile(array $replacements, $path)
+ {
+ return $this->sampleManager->write($path, $replacements);
+ }
+}