1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10: 11: 12:
13:
14: namespace Jyxo\Beholder\TestCase;
15:
16: 17: 18: 19: 20: 21: 22: 23: 24:
25: class Webdav extends \Jyxo\Beholder\TestCase
26: {
27: 28: 29: 30: 31:
32: private $server;
33:
34: 35: 36: 37: 38:
39: private $dir;
40:
41: 42: 43: 44: 45:
46: private $options;
47:
48: 49: 50: 51: 52: 53: 54: 55:
56: public function __construct($description, $server, $dir = '', array $options = array())
57: {
58: parent::__construct($description);
59:
60: $this->server = (string) $server;
61: $this->dir = (string) $dir;
62: $this->options = $options;
63: }
64:
65: 66: 67: 68: 69:
70: public function run()
71: {
72:
73: if (!extension_loaded('http')) {
74: return new \Jyxo\Beholder\Result(\Jyxo\Beholder\Result::NOT_APPLICABLE, 'Extension http missing');
75: }
76:
77:
78: if (!class_exists('\Jyxo\Webdav\Client')) {
79: return new \Jyxo\Beholder\Result(\Jyxo\Beholder\Result::NOT_APPLICABLE, 'Class \Jyxo\Webdav\Client missing');
80: }
81:
82: $random = md5(uniqid(time(), true));
83: $dir = trim($this->dir, '/');
84: if (!empty($dir)) {
85: $dir = '/' . $dir;
86: }
87: $path = $dir . '/beholder-' . $random . '.txt';
88: $content = $random;
89:
90:
91: $serverUrl = $this->server;
92: if ('http://' !== substr($this->server, 0, 7)) {
93: $serverUrl = 'http://' . $serverUrl;
94: }
95: $parsed = parse_url($serverUrl);
96: $ip = $parsed['host'];
97: $port = !empty($parsed['port']) ? $parsed['port'] : 80;
98: $description = (false !== filter_var($ip, FILTER_VALIDATE_IP) ? gethostbyaddr($ip) : $ip) . ':' . $port . $dir;
99:
100: try {
101: $webdav = new \Jyxo\Webdav\Client(array($serverUrl));
102: foreach ($this->options as $name => $value) {
103: $webdav->setOption($name, $value);
104: }
105:
106:
107: if (!$webdav->put($path, $content)) {
108: return new \Jyxo\Beholder\Result(\Jyxo\Beholder\Result::FAILURE, sprintf('Write error %s', $description));
109: }
110:
111:
112: $readContent = $webdav->get($path);
113: if (strlen($readContent) !== strlen($content)) {
114: return new \Jyxo\Beholder\Result(\Jyxo\Beholder\Result::FAILURE, sprintf('Read error %s', $description));
115: }
116:
117:
118: if (!$webdav->unlink($path)) {
119: return new \Jyxo\Beholder\Result(\Jyxo\Beholder\Result::FAILURE, sprintf('Delete error %s', $description));
120: }
121:
122: } catch (\Jyxo\Webdav\FileNotExistException $e) {
123: return new \Jyxo\Beholder\Result(\Jyxo\Beholder\Result::FAILURE, sprintf('Read error %s', $description));
124: } catch (\Jyxo\Webdav\Exception $e) {
125: return new \Jyxo\Beholder\Result(\Jyxo\Beholder\Result::FAILURE, sprintf('Error %s', $description));
126: }
127:
128:
129: return new \Jyxo\Beholder\Result(\Jyxo\Beholder\Result::SUCCESS, $description);
130: }
131: }
132: