Overview

Namespaces

  • Crunchmail
    • Collections
    • Entities
    • Exception
    • PHPUnit
    • Resources

Classes

  • Crunchmail\Client
  • Crunchmail\Collections\GenericCollection
  • Crunchmail\Entities\AttachmentEntity
  • Crunchmail\Entities\ContactEntity
  • Crunchmail\Entities\ContactListEntity
  • Crunchmail\Entities\ContactQueueEntity
  • Crunchmail\Entities\DomainEntity
  • Crunchmail\Entities\GenericEntity
  • Crunchmail\Entities\MessageEntity
  • Crunchmail\Entities\RecipientEntity
  • Crunchmail\PHPUnit\IsEntityConstraint
  • Crunchmail\PHPUnit\IsGenericCollectionConstraint
  • Crunchmail\PHPUnit\IsGenericEntityConstraint
  • Crunchmail\PHPUnit\IsGenericResourceConstraint
  • Crunchmail\PHPUnit\IsResourceConstraint
  • Crunchmail\PHPUnit\TestCase
  • Crunchmail\Resources\DomainsResource
  • Crunchmail\Resources\GenericResource
  • Crunchmail\Resources\PreviewSendResource

Exceptions

  • Crunchmail\Exception\ApiException
  • Overview
  • Namespace
  • Class
  1: <?php
  2: /**
  3:  * Base test class
  4:  *
  5:  * @author    Yannick Huerre <dev@sheoak.fr>
  6:  * @copyright 2015 (c) Oasiswork
  7:  * @license   https://opensource.org/licenses/MIT MIT
  8:  *
  9:  * @todo find a way to add request to an already created mocked client
 10:  */
 11: 
 12: namespace Crunchmail\PHPUnit;
 13: 
 14: use Crunchmail\Client;
 15: 
 16: use GuzzleHttp\Handler\MockHandler;
 17: use GuzzleHttp\HandlerStack;
 18: use GuzzleHttp\Psr7\Response;
 19: use GuzzleHttp\Psr7\Request;
 20: use GuzzleHttp\Middleware;
 21: 
 22: /**
 23:  * Test class
 24:  */
 25: abstract class TestCase extends \PHPUnit_Framework_TestCase
 26: {
 27:     private $container   = [];
 28:     private $bodyHistory = [];
 29: 
 30:     /**
 31:      * Shortcut to get a quick mocked client
 32:      *
 33:      * @return \Crunchmail\Client
 34:      */
 35:     public function quickMock()
 36:     {
 37:         $handler = call_user_func_array([$this, 'mockHandler'], func_get_args());
 38:         return $this->mockClient($handler);
 39:     }
 40: 
 41:     /**
 42:      * Create a mocked client
 43:      *
 44:      * @return void
 45:      */
 46:     public function mockClient($handler)
 47:     {
 48:         $client = new Client([
 49:             'base_uri'  => '',
 50:             'token_uri' => '',
 51:             'auth'      => ['api', 'key'],
 52:             'handler'   => $handler
 53:         ]);
 54:         return $client;
 55:     }
 56: 
 57:     /**
 58:      * Create a mocked handler
 59:      *
 60:      * @return void
 61:      *
 62:      * @SuppressWarnings(PHPMD)
 63:      */
 64:     public function mockHandler()
 65:     {
 66:         $responses = [];
 67: 
 68:         // TODO: enhance this with middleware history?
 69:         $this->bodyHistory = [];
 70: 
 71:         foreach (func_get_args() as $r)
 72:         {
 73:             list($tpl, $code) = $r;
 74: 
 75:             $body = $this->getTemplate($tpl);
 76:             $this->bodyHistory[] = $body;
 77: 
 78:             $responses[] = new MockHandler([ new Response($code, [], $body) ]);
 79:         }
 80: 
 81:         // Create a mock handler and queue responses.
 82:         $handler = new MockHandler($responses);
 83:         $stack   = HandlerStack::create($handler);
 84: 
 85:         // keep history
 86:         $this->container = [];
 87:         $history = Middleware::history($this->container);
 88: 
 89:         // Add the history middleware to the handler stack.
 90:         $stack->push($history);
 91: 
 92:         return $stack;
 93:     }
 94: 
 95:     public function getTemplate($tpl)
 96:     {
 97:         $dir = __DIR__ . '/../../tests/responses/';
 98:         $path = $dir . $tpl;
 99: 
100:         // automatic json extension
101:         if (! preg_match('#\.[a-z]+$#', $tpl))
102:         {
103:             $path .= '.json';
104:         }
105:         return file_get_contents($path);
106:     }
107: 
108:     public function getStdTemplate($tpl)
109:     {
110:         return json_decode($this->getTemplate($tpl));
111:     }
112: 
113:     /**
114:      * Return the $ind body sent in the history
115:      *
116:      * @param int $ind indice
117:      *
118:      * @return void
119:      */
120:     public function getSentBody($ind)
121:     {
122:         return json_decode($this->bodyHistory[$ind]);
123:     }
124: 
125:     public function getHistory()
126:     {
127:         return $this->container;
128:     }
129: 
130:     public function methodsProvider()
131:     {
132:         $data = [];
133:         foreach (Client::$methods as $m)
134:         {
135:             $data[] = [$m];
136:         }
137:         return $data;
138:     }
139: 
140:     public function getHistoryRequest($ind)
141:     {
142:         $history = $this->getHistory();
143:         return $history[$ind]['request'];
144:     }
145: 
146:     /**
147:      * @SuppressWarnings(PHPMD)
148:      */
149:     public function getHistoryContent($ind, $decode = true)
150:     {
151:         $req = $this->getHistoryRequest($ind);
152:         $content = $req->getBody()->getContents();
153:         return $decode ? json_decode($content) : $content;
154:     }
155: 
156:     /* ---------------------------------------------------------------------
157:      * New assertions
158:      * --------------------------------------------------------------------- */
159: 
160:     // @codeCoverageIgnoreStart
161: 
162:     public static function assertGenericEntity($other)
163:     {
164:         self::assertThat($other, self::isGenericEntity());
165:     }
166: 
167:     public static function assertGenericResource($other)
168:     {
169:         self::assertThat($other, self::isGenericResource());
170:     }
171: 
172:     public static function assertGenericCollection($other)
173:     {
174:         self::assertThat($other, self::isGenericCollection());
175:     }
176: 
177:     public static function assertEntity($type, $other)
178:     {
179:         self::assertThat($other, self::isEntity($type));
180:     }
181: 
182:     public static function assertResource($type, $other)
183:     {
184:         self::assertThat($other, self::isResource($type));
185:     }
186: 
187:     /* ---------------------------------------------------------------------
188:      * New constraints
189:      * --------------------------------------------------------------------- */
190:     public static function isGenericResource()
191:     {
192:         return new \Crunchmail\PHPUnit\IsGenericResourceConstraint();
193:     }
194: 
195:     public static function isGenericCollection()
196:     {
197:         return new \Crunchmail\PHPUnit\IsGenericCollectionConstraint();
198:     }
199: 
200:     public static function isGenericEntity()
201:     {
202:         return new \Crunchmail\PHPUnit\IsGenericEntityConstraint();
203:     }
204: 
205:     public static function isEntity($type)
206:     {
207:         return new \Crunchmail\PHPUnit\IsEntityConstraint($type);
208:     }
209: 
210:     public static function isResource($type)
211:     {
212:         return new \Crunchmail\PHPUnit\IsResourceConstraint($type);
213:     }
214: 
215: 
216:     // @codeCoverageIgnoreEnd
217: }
218: 
API documentation generated by ApiGen