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