1: <?php
2: 3: 4:
5: class Ingo_Script_Imap_Mock extends Ingo_Script_Imap_Api
6: {
7: 8: 9:
10: protected $_fixtures = array();
11:
12: 13: 14:
15: protected $_folders = array();
16:
17: 18: 19:
20: public function loadFixtures($dir)
21: {
22: $this->_fixtures = array();
23:
24: $dh = opendir($dir);
25: while (($dent = readdir($dh)) !== false) {
26: if (!in_array($dent, array('.', '..'))) {
27: $this->_fixtures[$dent] = Horde_Mime_Headers::parseHeaders(file_get_contents($dir . '/' . $dent));
28: }
29: }
30: closedir($dh);
31:
32: $i = 0;
33: foreach (array_keys($this->_fixtures) as $key) {
34: $this->_folders['INBOX'][] = array('uid' => ++$i,
35: 'fixture' => $key,
36: 'deleted' => false);
37: }
38: }
39:
40: 41: 42:
43: public function hasMessage($fixture, $folder = 'INBOX')
44: {
45: if (empty($this->_folders[$folder])) {
46: return false;
47: }
48: foreach ($this->_folders[$folder] as $message) {
49: if ($message['fixture'] == $fixture) {
50: return !$message['deleted'];
51: }
52: }
53: return false;
54: }
55:
56: 57: 58: 59: 60:
61: public function search($query)
62: {
63: $result = array();
64: foreach ($this->_folders['INBOX'] as $message) {
65: if ($message['deleted']) {
66: continue;
67: }
68: if ($query->matches($this->_fixtures[$message['fixture']])) {
69: $result[] = $message['uid'];
70: }
71: }
72: return $result;
73: }
74:
75: 76: 77:
78: public function deleteMessages($indices)
79: {
80: foreach (array_keys($this->_folders['INBOX']) as $i) {
81: if (in_array($this->_folders['INBOX'][$i]['uid'], $indices)) {
82: unset($this->_folders['INBOX'][$i]);
83: }
84: }
85:
86:
87: $this->_folders['INBOX'] = array_merge($this->_folders['INBOX'], array());
88: }
89:
90: 91: 92:
93: public function moveMessages($indices, $folder)
94: {
95: foreach (array_keys($this->_folders['INBOX']) as $i) {
96: if (in_array($this->_folders['INBOX'][$i]['uid'], $indices)) {
97: $this->_folders[$folder][] = $this->_folders['INBOX'][$i];
98: }
99: }
100: return $this->deleteMessages($indices);
101: }
102:
103: 104: 105:
106: public function fetchEnvelope($indices)
107: {
108: $result = array();
109:
110: foreach ($indices as $uid) {
111: foreach (array_keys($this->_folders['INBOX']) as $i) {
112: if ($this->_folders['INBOX'][$i]['uid'] == $uid) {
113: $fetch = new Horde_Imap_Client_Data_Fetch();
114: $fetch->setEnvelope(array(
115: 'from' => $this->_fixtures[$this->_folders['INBOX'][$i]['fixture']]->getValue('from')
116: ));
117: $fetch->setUid = $uid;
118: $result[$uid] = $fetch;
119: }
120: }
121: }
122:
123: return $result;
124: }
125:
126: }
127: