1: <?php
2:
3: /**
4: * Jyxo PHP Library
5: *
6: * LICENSE
7: *
8: * This source file is subject to the new BSD license that is bundled
9: * with this package in the file license.txt.
10: * It is also available through the world-wide-web at this URL:
11: * https://github.com/jyxo/php/blob/master/license.txt
12: */
13:
14: /**
15: * Tests IMAP server availability.
16: *
17: * @category Jyxo
18: * @package Jyxo_Beholder
19: * @copyright Copyright (c) 2005-2011 Jyxo, s.r.o.
20: * @license https://github.com/jyxo/php/blob/master/license.txt
21: * @author Jaroslav HanslĂk
22: */
23: class Jyxo_Beholder_TestCase_Imap extends Jyxo_Beholder_TestCase
24: {
25: /**
26: * Host.
27: *
28: * @var string
29: */
30: private $host;
31:
32: /**
33: * Username.
34: *
35: * @var string
36: */
37: private $user;
38:
39: /**
40: * Password.
41: *
42: * @var string
43: */
44: private $password;
45:
46: /**
47: * Port.
48: *
49: * @var integer
50: */
51: private $port;
52:
53: /**
54: * Validate certificates.
55: *
56: * @var boolean
57: */
58: private $validateCert;
59:
60: /**
61: * Constructor.
62: *
63: * @param string $description Test description
64: * @param string $host Server hostname
65: * @param string $user Username
66: * @param string $password Password
67: * @param integer $port Port
68: * @param boolean $validateCert Validate certificates
69: */
70: public function __construct($description, $host = 'localhost', $user = '', $password = '', $port = 143, $validateCert = true)
71: {
72: parent::__construct($description);
73:
74: $this->host = (string) $host;
75: $this->user = (string) $user;
76: $this->password = (string) $password;
77: $this->port = (int) $port;
78: $this->validateCert = (bool) $validateCert;
79: }
80:
81: /**
82: * Performs the test.
83: *
84: * @return Jyxo_Beholder_Result
85: */
86: public function run()
87: {
88: // The imap extension is required
89: if (!extension_loaded('imap')) {
90: return new Jyxo_Beholder_Result(Jyxo_Beholder_Result::NOT_APPLICABLE, 'Extension imap missing');
91: }
92:
93: // Label for status
94: $description = sprintf('%s@%s:%s', $this->user, $this->host, $this->port);
95:
96: $imap = imap_open('{' . $this->host . ':' . $this->port . '/' . (!$this->validateCert ? 'no' : '') . 'validate-cert}', $this->user, $this->password, OP_HALFOPEN, 1);
97: if (false === $imap) {
98: return new Jyxo_Beholder_Result(Jyxo_Beholder_Result::FAILURE, sprintf('Connection error %s', $description));
99: }
100: imap_close($imap);
101:
102: return new Jyxo_Beholder_Result(Jyxo_Beholder_Result::SUCCESS, $description);
103: }
104: }
105: