summaryrefslogtreecommitdiff
path: root/server/vendor/php-opencloud/common/tests/integration/TestCase.php
blob: cb813813bed38ea638ce3857bf627a6249b13d4c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
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);
    }
}