12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103 |
- <?php
-
- require_once 'Swift/Mime/MimeEntity.php';
- require_once 'Swift/Tests/SwiftUnitTestCase.php';
- require_once 'Swift/Mime/ContentEncoder.php';
- require_once 'Swift/Mime/Header.php';
- require_once 'Swift/Mime/ParameterizedHeader.php';
- require_once 'Swift/KeyCache.php';
- require_once 'Swift/Mime/HeaderSet.php';
-
- abstract class Swift_Mime_AbstractMimeEntityTest
- extends Swift_Tests_SwiftUnitTestCase
- {
- public function testGetHeadersReturnsHeaderSet()
- {
- $headers = $this->_createHeaderSet();
- $entity = $this->_createEntity($headers, $this->_createEncoder(),
- $this->_createCache()
- );
- $this->assertSame($headers, $entity->getHeaders());
- }
-
- public function testContentTypeIsReturnedFromHeader()
- {
- $ctype = $this->_createHeader('Content-Type', 'image/jpeg-test');
- $headers = $this->_createHeaderSet(array('Content-Type' => $ctype));
- $entity = $this->_createEntity($headers, $this->_createEncoder(),
- $this->_createCache()
- );
- $this->assertEqual('image/jpeg-test', $entity->getContentType());
- }
-
- public function testContentTypeIsSetInHeader()
- {
- $ctype = $this->_createHeader('Content-Type', 'text/plain', array(), false);
- $headers = $this->_createHeaderSet(array('Content-Type' => $ctype));
- $this->_checking(Expectations::create()
- -> one($ctype)->setFieldBodyModel('image/jpeg')
- -> ignoring($ctype)
- );
- $entity = $this->_createEntity($headers, $this->_createEncoder(),
- $this->_createCache()
- );
- $entity->setContentType('image/jpeg');
- }
-
- public function testContentTypeHeaderIsAddedIfNoneSet()
- {
- $headers = $this->_createHeaderSet(array(), false);
- $this->_checking(Expectations::create()
- -> one($headers)->addParameterizedHeader('Content-Type', 'image/jpeg')
- -> ignoring($headers)
- );
- $entity = $this->_createEntity($headers, $this->_createEncoder(),
- $this->_createCache()
- );
- $entity->setContentType('image/jpeg');
- }
-
- public function testContentTypeCanBeSetViaSetBody()
- {
- $headers = $this->_createHeaderSet(array(), false);
- $this->_checking(Expectations::create()
- -> one($headers)->addParameterizedHeader('Content-Type', 'text/html')
- -> ignoring($headers)
- );
- $entity = $this->_createEntity($headers, $this->_createEncoder(),
- $this->_createCache()
- );
- $entity->setBody('<b>foo</b>', 'text/html');
- }
-
- public function testGetEncoderFromConstructor()
- {
- $encoder = $this->_createEncoder('base64');
- $entity = $this->_createEntity($this->_createHeaderSet(), $encoder,
- $this->_createCache()
- );
- $this->assertSame($encoder, $entity->getEncoder());
- }
-
- public function testSetAndGetEncoder()
- {
- $encoder = $this->_createEncoder('base64');
- $headers = $this->_createHeaderSet();
- $entity = $this->_createEntity($headers, $this->_createEncoder(),
- $this->_createCache()
- );
- $entity->setEncoder($encoder);
- $this->assertSame($encoder, $entity->getEncoder());
- }
-
- public function testSettingEncoderUpdatesTransferEncoding()
- {
- $encoder = $this->_createEncoder('base64');
- $encoding = $this->_createHeader(
- 'Content-Transfer-Encoding', '8bit', array(), false
- );
- $headers = $this->_createHeaderSet(array(
- 'Content-Transfer-Encoding' => $encoding
- ));
- $this->_checking(Expectations::create()
- -> one($encoding)->setFieldBodyModel('base64')
- -> ignoring($encoding)
- );
- $entity = $this->_createEntity($headers, $this->_createEncoder(),
- $this->_createCache()
- );
- $entity->setEncoder($encoder);
- }
-
- public function testSettingEncoderAddsEncodingHeaderIfNonePresent()
- {
- $headers = $this->_createHeaderSet(array(), false);
- $this->_checking(Expectations::create()
- -> one($headers)->addTextHeader('Content-Transfer-Encoding', 'something')
- -> ignoring($headers)
- );
- $entity = $this->_createEntity($headers, $this->_createEncoder(),
- $this->_createCache()
- );
- $entity->setEncoder($this->_createEncoder('something'));
- }
-
- public function testIdIsReturnedFromHeader()
- {
- /* -- RFC 2045, 7.
- In constructing a high-level user agent, it may be desirable to allow
- one body to make reference to another. Accordingly, bodies may be
- labelled using the "Content-ID" header field, which is syntactically
- identical to the "Message-ID" header field
- */
-
- $cid = $this->_createHeader('Content-ID', 'zip@button');
- $headers = $this->_createHeaderSet(array('Content-ID' => $cid));
- $entity = $this->_createEntity($headers, $this->_createEncoder(),
- $this->_createCache()
- );
- $this->assertEqual('zip@button', $entity->getId());
- }
-
- public function testIdIsSetInHeader()
- {
- $cid = $this->_createHeader('Content-ID', 'zip@button', array(), false);
- $headers = $this->_createHeaderSet(array('Content-ID' => $cid));
- $this->_checking(Expectations::create()
- -> one($cid)->setFieldBodyModel('foo@bar')
- -> ignoring($cid)
- );
- $entity = $this->_createEntity($headers, $this->_createEncoder(),
- $this->_createCache()
- );
- $entity->setId('foo@bar');
- }
-
- public function testIdIsAutoGenerated()
- {
- $entity = $this->_createEntity($this->_createHeaderSet(),
- $this->_createEncoder(), $this->_createCache()
- );
- $this->assertPattern('/^.*?@.*?$/D', $entity->getId());
- }
-
- public function testGenerateIdCreatesNewId()
- {
- $headers = $this->_createHeaderSet();
- $entity = $this->_createEntity($headers, $this->_createEncoder(),
- $this->_createCache()
- );
- $id1 = $entity->generateId();
- $id2 = $entity->generateId();
- $this->assertNotEqual($id1, $id2);
- }
-
- public function testGenerateIdSetsNewId()
- {
- $headers = $this->_createHeaderSet();
- $entity = $this->_createEntity($headers, $this->_createEncoder(),
- $this->_createCache()
- );
- $id = $entity->generateId();
- $this->assertEqual($id, $entity->getId());
- }
-
- public function testDescriptionIsReadFromHeader()
- {
- /* -- RFC 2045, 8.
- The ability to associate some descriptive information with a given
- body is often desirable. For example, it may be useful to mark an
- "image" body as "a picture of the Space Shuttle Endeavor." Such text
- may be placed in the Content-Description header field. This header
- field is always optional.
- */
-
- $desc = $this->_createHeader('Content-Description', 'something');
- $headers = $this->_createHeaderSet(array('Content-Description' => $desc));
- $entity = $this->_createEntity($headers, $this->_createEncoder(),
- $this->_createCache()
- );
- $this->assertEqual('something', $entity->getDescription());
- }
-
- public function testDescriptionIsSetInHeader()
- {
- $desc = $this->_createHeader('Content-Description', '', array(), false);
- $headers = $this->_createHeaderSet(array('Content-Description' => $desc));
- $this->_checking(Expectations::create()
- -> one($desc)->setFieldBodyModel('whatever')
- -> ignoring($desc)
- );
- $entity = $this->_createEntity($headers, $this->_createEncoder(),
- $this->_createCache()
- );
- $entity->setDescription('whatever');
- }
-
- public function testDescriptionHeaderIsAddedIfNotPresent()
- {
- $headers = $this->_createHeaderSet(array(), false);
- $this->_checking(Expectations::create()
- -> one($headers)->addTextHeader('Content-Description', 'whatever')
- -> ignoring($headers)
- );
- $entity = $this->_createEntity($headers, $this->_createEncoder(),
- $this->_createCache()
- );
- $entity->setDescription('whatever');
- }
-
- public function testSetAndGetMaxLineLength()
- {
- $entity = $this->_createEntity($this->_createHeaderSet(),
- $this->_createEncoder(), $this->_createCache()
- );
- $entity->setMaxLineLength(60);
- $this->assertEqual(60, $entity->getMaxLineLength());
- }
-
- public function testEncoderIsUsedForStringGeneration()
- {
- $encoder = $this->_createEncoder('base64', false);
- $this->_checking(Expectations::create()
- -> one($encoder)->encodeString('blah', optional())
- -> ignoring($encoder)
- );
- $entity = $this->_createEntity($this->_createHeaderSet(),
- $encoder, $this->_createCache()
- );
- $entity->setBody("blah");
- $entity->toString();
- }
-
- public function testMaxLineLengthIsProvidedWhenEncoding()
- {
- $encoder = $this->_createEncoder('base64', false);
- $this->_checking(Expectations::create()
- -> one($encoder)->encodeString('blah', 0, 65)
- -> ignoring($encoder)
- );
- $entity = $this->_createEntity($this->_createHeaderSet(),
- $encoder, $this->_createCache()
- );
- $entity->setBody("blah");
- $entity->setMaxLineLength(65);
- $entity->toString();
- }
-
- public function testHeadersAppearInString()
- {
- $headers = $this->_createHeaderSet(array(), false);
- $this->_checking(Expectations::create()
- -> ignoring($headers)->toString() -> returns(
- "Content-Type: text/plain; charset=utf-8\r\n" .
- "X-MyHeader: foobar\r\n"
- )
- -> ignoring($headers)
- );
- $entity = $this->_createEntity($headers, $this->_createEncoder(),
- $this->_createCache()
- );
- $this->assertEqual(
- "Content-Type: text/plain; charset=utf-8\r\n" .
- "X-MyHeader: foobar\r\n",
- $entity->toString()
- );
- }
-
- public function testSetAndGetBody()
- {
- $entity = $this->_createEntity($this->_createHeaderSet(),
- $this->_createEncoder(), $this->_createCache()
- );
- $entity->setBody("blah\r\nblah!");
- $this->assertEqual("blah\r\nblah!", $entity->getBody());
- }
-
- public function testBodyIsAppended()
- {
- $headers = $this->_createHeaderSet(array(), false);
- $this->_checking(Expectations::create()
- -> ignoring($headers)->toString() -> returns(
- "Content-Type: text/plain; charset=utf-8\r\n"
- )
- -> ignoring($headers)
- );
- $entity = $this->_createEntity($headers, $this->_createEncoder(),
- $this->_createCache()
- );
- $entity->setBody("blah\r\nblah!");
- $this->assertEqual(
- "Content-Type: text/plain; charset=utf-8\r\n" .
- "\r\n" .
- "blah\r\nblah!",
- $entity->toString()
- );
- }
-
- public function testGetBodyReturnsStringFromByteStream()
- {
- $os = $this->_createOutputStream("byte stream string");
- $entity = $this->_createEntity($this->_createHeaderSet(),
- $this->_createEncoder(), $this->_createCache()
- );
- $entity->setBody($os);
- $this->assertEqual("byte stream string", $entity->getBody());
- }
-
- public function testByteStreamBodyIsAppended()
- {
- $headers = $this->_createHeaderSet(array(), false);
- $os = $this->_createOutputStream("streamed");
- $this->_checking(Expectations::create()
- -> ignoring($headers)->toString() -> returns(
- "Content-Type: text/plain; charset=utf-8\r\n"
- )
- -> ignoring($headers)
- );
- $entity = $this->_createEntity($headers, $this->_createEncoder(),
- $this->_createCache()
- );
- $entity->setBody($os);
- $this->assertEqual(
- "Content-Type: text/plain; charset=utf-8\r\n" .
- "\r\n" .
- "streamed",
- $entity->toString()
- );
- }
-
- public function testBoundaryCanBeRetrieved()
- {
- /* -- RFC 2046, 5.1.1.
- boundary := 0*69<bchars> bcharsnospace
-
- bchars := bcharsnospace / " "
-
- bcharsnospace := DIGIT / ALPHA / "'" / "(" / ")" /
- "+" / "_" / "," / "-" / "." /
- "/" / ":" / "=" / "?"
- */
-
- $entity = $this->_createEntity($this->_createHeaderSet(),
- $this->_createEncoder(), $this->_createCache()
- );
- $this->assertPattern(
- '/^[a-zA-Z0-9\'\(\)\+_\-,\.\/:=\?\ ]{0,69}[a-zA-Z0-9\'\(\)\+_\-,\.\/:=\?]$/D',
- $entity->getBoundary()
- );
- }
-
- public function testBoundaryNeverChanges()
- {
- $entity = $this->_createEntity($this->_createHeaderSet(),
- $this->_createEncoder(), $this->_createCache()
- );
- $firstBoundary = $entity->getBoundary();
- for ($i = 0; $i < 10; $i++) {
- $this->assertEqual($firstBoundary, $entity->getBoundary());
- }
- }
-
- public function testBoundaryCanBeSet()
- {
- $entity = $this->_createEntity($this->_createHeaderSet(),
- $this->_createEncoder(), $this->_createCache()
- );
- $entity->setBoundary('foobar');
- $this->assertEqual('foobar', $entity->getBoundary());
- }
-
- public function testAddingChildrenGeneratesBoundaryInHeaders()
- {
- $child = $this->_createChild();
- $cType = $this->_createHeader('Content-Type', 'text/plain', array(), false);
- $this->_checking(Expectations::create()
- -> one($cType)->setParameter('boundary', any())
- -> ignoring($cType)
- );
-
- $entity = $this->_createEntity($this->_createHeaderSet(array(
- 'Content-Type' => $cType
- )),
- $this->_createEncoder(), $this->_createCache()
- );
- $entity->setChildren(array($child));
- }
-
- public function testChildrenOfLevelAttachmentAndLessCauseMultipartMixed()
- {
- for ($level = Swift_Mime_MimeEntity::LEVEL_MIXED;
- $level > Swift_Mime_MimeEntity::LEVEL_TOP; $level /= 2)
- {
- $child = $this->_createChild($level);
- $cType = $this->_createHeader(
- 'Content-Type', 'text/plain', array(), false
- );
- $this->_checking(Expectations::create()
- -> one($cType)->setFieldBodyModel('multipart/mixed')
- -> ignoring($cType)
- );
- $entity = $this->_createEntity($this->_createHeaderSet(array(
- 'Content-Type' => $cType)),
- $this->_createEncoder(), $this->_createCache()
- );
- $entity->setChildren(array($child));
- }
- }
-
- public function testChildrenOfLevelAlternativeAndLessCauseMultipartAlternative()
- {
- for ($level = Swift_Mime_MimeEntity::LEVEL_ALTERNATIVE;
- $level > Swift_Mime_MimeEntity::LEVEL_MIXED; $level /= 2)
- {
- $child = $this->_createChild($level);
- $cType = $this->_createHeader(
- 'Content-Type', 'text/plain', array(), false
- );
- $this->_checking(Expectations::create()
- -> one($cType)->setFieldBodyModel('multipart/alternative')
- -> ignoring($cType)
- );
- $entity = $this->_createEntity($this->_createHeaderSet(array(
- 'Content-Type' => $cType)),
- $this->_createEncoder(), $this->_createCache()
- );
- $entity->setChildren(array($child));
- }
- }
-
- public function testChildrenOfLevelRelatedAndLessCauseMultipartRelated()
- {
- for ($level = Swift_Mime_MimeEntity::LEVEL_RELATED;
- $level > Swift_Mime_MimeEntity::LEVEL_ALTERNATIVE; $level /= 2)
- {
- $child = $this->_createChild($level);
- $cType = $this->_createHeader(
- 'Content-Type', 'text/plain', array(), false
- );
- $this->_checking(Expectations::create()
- -> one($cType)->setFieldBodyModel('multipart/related')
- -> ignoring($cType)
- );
- $entity = $this->_createEntity($this->_createHeaderSet(array(
- 'Content-Type' => $cType)),
- $this->_createEncoder(), $this->_createCache()
- );
- $entity->setChildren(array($child));
- }
- }
-
- public function testHighestLevelChildDeterminesContentType()
- {
- $combinations = array(
- array('levels' => array(Swift_Mime_MimeEntity::LEVEL_MIXED,
- Swift_Mime_MimeEntity::LEVEL_ALTERNATIVE,
- Swift_Mime_MimeEntity::LEVEL_RELATED
- ),
- 'type' => 'multipart/mixed'
- ),
- array('levels' => array(Swift_Mime_MimeEntity::LEVEL_MIXED,
- Swift_Mime_MimeEntity::LEVEL_RELATED
- ),
- 'type' => 'multipart/mixed'
- ),
- array('levels' => array(Swift_Mime_MimeEntity::LEVEL_MIXED,
- Swift_Mime_MimeEntity::LEVEL_ALTERNATIVE
- ),
- 'type' => 'multipart/mixed'
- ),
- array('levels' => array(Swift_Mime_MimeEntity::LEVEL_ALTERNATIVE,
- Swift_Mime_MimeEntity::LEVEL_RELATED
- ),
- 'type' => 'multipart/alternative'
- )
- );
-
- foreach ($combinations as $combination) {
- $children = array();
- foreach ($combination['levels'] as $level) {
- $children[] = $this->_createChild($level);
- }
-
- $cType = $this->_createHeader(
- 'Content-Type', 'text/plain', array(), false
- );
- $this->_checking(Expectations::create()
- -> one($cType)->setFieldBodyModel($combination['type'])
- -> ignoring($cType)
- );
- $entity = $this->_createEntity($this->_createHeaderSet(array(
- 'Content-Type' => $cType)),
- $this->_createEncoder(), $this->_createCache()
- );
- $entity->setChildren($children);
- }
- }
-
- public function testChildrenAppearNestedInString()
- {
- /* -- RFC 2046, 5.1.1.
- (excerpt too verbose to paste here)
- */
-
- $headers = $this->_createHeaderSet(array(), false);
-
- $child1 = $this->_createChild(Swift_Mime_MimeEntity::LEVEL_ALTERNATIVE,
- "Content-Type: text/plain\r\n" .
- "\r\n" .
- "foobar"
- );
-
- $child2 = $this->_createChild(Swift_Mime_MimeEntity::LEVEL_ALTERNATIVE,
- "Content-Type: text/html\r\n" .
- "\r\n" .
- "<b>foobar</b>"
- );
-
- $this->_checking(Expectations::create()
- -> ignoring($headers)->toString() -> returns(
- "Content-Type: multipart/alternative; boundary=\"xxx\"\r\n"
- )
- -> ignoring($headers)
- );
-
- $entity = $this->_createEntity($headers, $this->_createEncoder(),
- $this->_createCache()
- );
- $entity->setBoundary('xxx');
- $entity->setChildren(array($child1, $child2));
-
- $this->assertEqual(
- "Content-Type: multipart/alternative; boundary=\"xxx\"\r\n" .
- "\r\n" .
- "\r\n--xxx\r\n" .
- "Content-Type: text/plain\r\n" .
- "\r\n" .
- "foobar\r\n" .
- "\r\n--xxx\r\n" .
- "Content-Type: text/html\r\n" .
- "\r\n" .
- "<b>foobar</b>\r\n" .
- "\r\n--xxx--\r\n",
- $entity->toString()
- );
- }
-
- public function testMixingLevelsIsHierarchical()
- {
- $headers = $this->_createHeaderSet(array(), false);
- $newHeaders = $this->_createHeaderSet(array(), false);
-
- $part = $this->_createChild(Swift_Mime_MimeEntity::LEVEL_ALTERNATIVE,
- "Content-Type: text/plain\r\n" .
- "\r\n" .
- "foobar"
- );
-
- $attachment = $this->_createChild(Swift_Mime_MimeEntity::LEVEL_MIXED,
- "Content-Type: application/octet-stream\r\n" .
- "\r\n" .
- "data"
- );
-
- $this->_checking(Expectations::create()
- -> ignoring($headers)->toString() -> returns(
- "Content-Type: multipart/mixed; boundary=\"xxx\"\r\n"
- )
- -> ignoring($headers)->newInstance() -> returns($newHeaders)
- -> ignoring($headers)
- -> ignoring($newHeaders)->toString() -> returns(
- "Content-Type: multipart/alternative; boundary=\"yyy\"\r\n"
- )
- -> ignoring($newHeaders)
- );
-
- $entity = $this->_createEntity($headers, $this->_createEncoder(),
- $this->_createCache()
- );
- $entity->setBoundary('xxx');
- $entity->setChildren(array($part, $attachment));
-
- $this->assertPattern(
- "~^" .
- "Content-Type: multipart/mixed; boundary=\"xxx\"\r\n" .
- "\r\n\r\n--xxx\r\n" .
- "Content-Type: multipart/alternative; boundary=\"yyy\"\r\n" .
- "\r\n\r\n--(.*?)\r\n" .
- "Content-Type: text/plain\r\n" .
- "\r\n" .
- "foobar" .
- "\r\n\r\n--\\1--\r\n" .
- "\r\n\r\n--xxx\r\n" .
- "Content-Type: application/octet-stream\r\n" .
- "\r\n" .
- "data" .
- "\r\n\r\n--xxx--\r\n" .
- "\$~",
- $entity->toString()
- );
- }
-
- public function testSettingEncoderNotifiesChildren()
- {
- $child = $this->_createChild(0, '', false);
- $encoder = $this->_createEncoder('base64');
-
- $this->_checking(Expectations::create()
- -> one($child)->encoderChanged($encoder)
- -> ignoring($child)
- );
-
- $entity = $this->_createEntity($this->_createHeaderSet(),
- $this->_createEncoder(), $this->_createCache()
- );
- $entity->setChildren(array($child));
- $entity->setEncoder($encoder);
- }
-
- public function testReceiptOfEncoderChangeNotifiesChildren()
- {
- $child = $this->_createChild(0, '', false);
- $encoder = $this->_createEncoder('base64');
-
- $this->_checking(Expectations::create()
- -> one($child)->encoderChanged($encoder)
- -> ignoring($child)
- );
-
- $entity = $this->_createEntity($this->_createHeaderSet(),
- $this->_createEncoder(), $this->_createCache()
- );
- $entity->setChildren(array($child));
- $entity->encoderChanged($encoder);
- }
-
- public function testReceiptOfCharsetChangeNotifiesChildren()
- {
- $child = $this->_createChild(0, '', false);
-
- $this->_checking(Expectations::create()
- -> one($child)->charsetChanged('windows-874')
- -> ignoring($child)
- );
-
- $entity = $this->_createEntity($this->_createHeaderSet(),
- $this->_createEncoder(), $this->_createCache()
- );
- $entity->setChildren(array($child));
- $entity->charsetChanged('windows-874');
- }
-
- public function testEntityIsWrittenToByteStream()
- {
- $entity = $this->_createEntity($this->_createHeaderSet(),
- $this->_createEncoder(), $this->_createCache()
- );
- $is = $this->_createInputStream(false);
- $this->_checking(Expectations::create()
- -> atLeast(1)->of($is)->write(any())
- -> ignoring($is)
- );
-
- $entity->toByteStream($is);
- }
-
- public function testEntityHeadersAreComittedToByteStream()
- {
- $entity = $this->_createEntity($this->_createHeaderSet(),
- $this->_createEncoder(), $this->_createCache()
- );
- $is = $this->_createInputStream(false);
- $this->_checking(Expectations::create()
- -> atLeast(1)->of($is)->commit()
- -> atLeast(1)->of($is)->write(any())
- -> ignoring($is)
- );
-
- $entity->toByteStream($is);
- }
-
- public function testOrderingTextBeforeHtml()
- {
- $htmlChild = $this->_createChild(Swift_Mime_MimeEntity::LEVEL_ALTERNATIVE,
- "Content-Type: text/html\r\n" .
- "\r\n" .
- "HTML PART",
- false
- );
- $textChild = $this->_createChild(Swift_Mime_MimeEntity::LEVEL_ALTERNATIVE,
- "Content-Type: text/plain\r\n" .
- "\r\n" .
- "TEXT PART",
- false
- );
- $headers = $this->_createHeaderSet(array(), false);
- $this->_checking(Expectations::create()
- -> ignoring($headers)->toString() -> returns(
- "Content-Type: multipart/alternative; boundary=\"xxx\"\r\n"
- )
- -> ignoring($headers)
- -> ignoring($htmlChild)->getContentType() -> returns('text/html')
- -> ignoring($htmlChild)
- -> ignoring($textChild)->getContentType() -> returns('text/plain')
- -> ignoring($textChild)
- );
- $entity = $this->_createEntity($headers, $this->_createEncoder(),
- $this->_createCache()
- );
- $entity->setBoundary('xxx');
- $entity->setChildren(array($htmlChild, $textChild));
-
- $this->assertEqual(
- "Content-Type: multipart/alternative; boundary=\"xxx\"\r\n" .
- "\r\n\r\n--xxx\r\n" .
- "Content-Type: text/plain\r\n" .
- "\r\n" .
- "TEXT PART" .
- "\r\n\r\n--xxx\r\n" .
- "Content-Type: text/html\r\n" .
- "\r\n" .
- "HTML PART" .
- "\r\n\r\n--xxx--\r\n",
- $entity->toString()
- );
- }
-
- public function testUnsettingChildrenRestoresContentType()
- {
- $cType = $this->_createHeader('Content-Type', 'text/plain', array(), false);
- $child = $this->_createChild(Swift_Mime_MimeEntity::LEVEL_ALTERNATIVE);
-
- $s = $this->_mockery()->sequence('Type setting');
- $this->_checking(Expectations::create()
- -> one($cType)->setFieldBodyModel('image/jpeg') -> inSequence($s)
- -> one($cType)->setFieldBodyModel('multipart/alternative') -> inSequence($s)
- -> one($cType)->setFieldBodyModel('image/jpeg') -> inSequence($s)
- -> ignoring($cType)
- );
-
- $entity = $this->_createEntity($this->_createHeaderSet(array(
- 'Content-Type' => $cType
- )),
- $this->_createEncoder(), $this->_createCache()
- );
-
- $entity->setContentType('image/jpeg');
- $entity->setChildren(array($child));
- $entity->setChildren(array());
- }
-
- public function testBodyIsReadFromCacheWhenUsingToStringIfPresent()
- {
- $headers = $this->_createHeaderSet(array(), false);
- $this->_checking(Expectations::create()
- -> ignoring($headers)->toString() -> returns(
- "Content-Type: text/plain; charset=utf-8\r\n"
- )
- -> ignoring($headers)
- );
-
- $cache = $this->_createCache(false);
- $this->_checking(Expectations::create()
- -> one($cache)->hasKey(any(), 'body') -> returns(true)
- -> one($cache)->getString(any(), 'body') -> returns("\r\ncache\r\ncache!")
- -> ignoring($cache)
- );
-
- $entity = $this->_createEntity($headers, $this->_createEncoder(),
- $cache
- );
-
- $entity->setBody("blah\r\nblah!");
- $this->assertEqual(
- "Content-Type: text/plain; charset=utf-8\r\n" .
- "\r\n" .
- "cache\r\ncache!",
- $entity->toString()
- );
- }
-
- public function testBodyIsAddedToCacheWhenUsingToString()
- {
- $headers = $this->_createHeaderSet(array(), false);
- $this->_checking(Expectations::create()
- -> ignoring($headers)->toString() -> returns(
- "Content-Type: text/plain; charset=utf-8\r\n"
- )
- -> ignoring($headers)
- );
-
- $cache = $this->_createCache(false);
- $this->_checking(Expectations::create()
- -> one($cache)->hasKey(any(), 'body') -> returns(false)
- -> one($cache)->setString(any(), 'body', "\r\nblah\r\nblah!", Swift_KeyCache::MODE_WRITE)
- -> ignoring($cache)
- );
-
- $entity = $this->_createEntity($headers, $this->_createEncoder(),
- $cache
- );
-
- $entity->setBody("blah\r\nblah!");
- $entity->toString();
- }
-
- public function testBodyIsClearedFromCacheIfNewBodySet()
- {
- $headers = $this->_createHeaderSet(array(), false);
- $this->_checking(Expectations::create()
- -> ignoring($headers)->toString() -> returns(
- "Content-Type: text/plain; charset=utf-8\r\n"
- )
- -> ignoring($headers)
- );
-
- $cache = $this->_createCache(false);
- $this->_checking(Expectations::create()
- -> one($cache)->clearKey(any(), 'body')
- -> ignoring($cache)
- );
-
- $entity = $this->_createEntity($headers, $this->_createEncoder(),
- $cache
- );
-
- $entity->setBody("blah\r\nblah!");
- $entity->toString();
-
- $entity->setBody("new\r\nnew!");
- }
-
- public function testBodyIsNotClearedFromCacheIfSameBodySet()
- {
- $headers = $this->_createHeaderSet(array(), false);
- $this->_checking(Expectations::create()
- -> ignoring($headers)->toString() -> returns(
- "Content-Type: text/plain; charset=utf-8\r\n"
- )
- -> ignoring($headers)
- );
-
- $cache = $this->_createCache(false);
- $this->_checking(Expectations::create()
- -> never($cache)->clearKey(any(), 'body')
- -> ignoring($cache)
- );
-
- $entity = $this->_createEntity($headers, $this->_createEncoder(),
- $cache
- );
-
- $entity->setBody("blah\r\nblah!");
- $entity->toString();
-
- $entity->setBody("blah\r\nblah!");
- }
-
- public function testBodyIsClearedFromCacheIfNewEncoderSet()
- {
- $headers = $this->_createHeaderSet(array(), false);
- $this->_checking(Expectations::create()
- -> ignoring($headers)->toString() -> returns(
- "Content-Type: text/plain; charset=utf-8\r\n"
- )
- -> ignoring($headers)
- );
-
- $cache = $this->_createCache(false);
- $this->_checking(Expectations::create()
- -> one($cache)->clearKey(any(), 'body')
- -> ignoring($cache)
- );
-
- $otherEncoder = $this->_createEncoder();
-
- $entity = $this->_createEntity($headers, $this->_createEncoder(),
- $cache
- );
-
- $entity->setBody("blah\r\nblah!");
- $entity->toString();
-
- $entity->setEncoder($otherEncoder);
- }
-
- public function testBodyIsReadFromCacheWhenUsingToByteStreamIfPresent()
- {
- $is = $this->_createInputStream();
- $cache = $this->_createCache(false);
-
- $this->_checking(Expectations::create()
- -> one($cache)->hasKey(any(), 'body') -> returns(true)
- -> one($cache)->exportToByteStream(any(), 'body', $is)
- -> ignoring($cache)
- );
-
- $entity = $this->_createEntity($this->_createHeaderSet(),
- $this->_createEncoder(), $cache
- );
- $entity->setBody('foo');
-
- $entity->toByteStream($is);
- }
-
- public function testBodyIsAddedToCacheWhenUsingToByteStream()
- {
- $is = $this->_createInputStream();
- $cache = $this->_createCache(false);
-
- $this->_checking(Expectations::create()
- -> one($cache)->hasKey(any(), 'body') -> returns(false)
- //The input stream should be fetched for writing
- // Proving that it's actually written to is possible, but extremely
- // fragile. Best let the acceptance tests cover this aspect
- -> one($cache)->getInputByteStream(any(), 'body')
- -> ignoring($cache)
- );
-
- $entity = $this->_createEntity($this->_createHeaderSet(),
- $this->_createEncoder(), $cache
- );
- $entity->setBody('foo');
-
- $entity->toByteStream($is);
- }
-
- public function testFluidInterface()
- {
- $entity = $this->_createEntity($this->_createHeaderSet(),
- $this->_createEncoder(), $this->_createCache()
- );
-
- $this->assertSame($entity,
- $entity
- ->setContentType('text/plain')
- ->setEncoder($this->_createEncoder())
- ->setId('foo@bar')
- ->setDescription('my description')
- ->setMaxLineLength(998)
- ->setBody('xx')
- ->setBoundary('xyz')
- ->setChildren(array())
- );
- }
-
- // -- Private helpers
-
- abstract protected function _createEntity($headers, $encoder, $cache);
-
- protected function _createChild($level = null, $string = '', $stub = true)
- {
- $child = $this->_mock('Swift_Mime_MimeEntity');
- if (isset($level)) {
- $this->_checking(Expectations::create()
- -> ignoring($child)->getNestingLevel() -> returns($level)
- );
- }
- $this->_checking(Expectations::create()
- -> ignoring($child)->toString() -> returns($string)
- );
- if ($stub) {
- $this->_checking(Expectations::create()
- -> ignoring($child)
- );
- }
-
- return $child;
- }
-
- protected function _createEncoder($name = 'quoted-printable', $stub = true)
- {
- $encoder = $this->_mock('Swift_Mime_ContentEncoder');
- $this->_checking(Expectations::create()
- -> ignoring($encoder)->getName() -> returns($name)
- );
-
- if ($stub) {
- $this->_checking(Expectations::create()
- -> ignoring($encoder)->encodeString(any(), optional())
- -> calls(array($this, 'returnStringFromEncoder'))
- -> ignoring($encoder)
- );
- }
-
- return $encoder;
- }
-
- protected function _createCache($stub = true)
- {
- $cache = $this->_mock('Swift_KeyCache');
-
- if ($stub) {
- $this->_checking(Expectations::create()
- -> ignoring($cache)
- );
- }
-
- return $cache;
- }
-
- protected function _createHeaderSet($headers = array(), $stub = true)
- {
- $set = $this->_mock('Swift_Mime_HeaderSet');
- foreach ($headers as $key => $header) {
- $this->_checking(Expectations::create()
- -> ignoring($set)->has($key) -> returns(true)
- -> ignoring($set)->get($key) -> returns($header)
- );
- }
- if ($stub) {
- $this->_checking(Expectations::create()
- -> ignoring($set)->newInstance() -> returns($set)
- -> ignoring($set)
- );
- }
-
- return $set;
- }
-
- protected function _createHeader($name, $model = null, $params = array(), $stub = true)
- {
- $header = $this->_mock('Swift_Mime_ParameterizedHeader');
- $this->_checking(Expectations::create()
- -> ignoring($header)->getFieldName() -> returns($name)
- -> ignoring($header)->getFieldBodyModel() -> returns($model)
- );
- foreach ($params as $key => $value) {
- $this->_checking(Expectations::create()
- -> ignoring($header)->getParameter($key) -> returns($value)
- );
- }
- if ($stub) {
- $this->_checking(Expectations::create()
- -> ignoring($header)
- );
- }
-
- return $header;
- }
-
- protected function _createOutputStream($data = null, $stub = true)
- {
- $os = $this->_mock('Swift_OutputByteStream');
- if (isset($data)) {
- $pos = $this->_mockery()->states('position')->startsAs('at beginning');
- $this->_checking(Expectations::create()
- -> ignoring($os)->read(optional()) -> returns($data)
- -> when($pos->isNot('at end')) -> then($pos->is('at end'))
-
- -> ignoring($os)->read(optional()) -> returns(false)
- );
- if ($stub) {
- $this->_checking(Expectations::create()
- -> ignoring($os)
- );
- }
- }
-
- return $os;
- }
-
- protected function _createInputStream($stub = true)
- {
- $is = $this->_mock('Swift_InputByteStream');
- if ($stub) {
- $this->_checking(Expectations::create()
- -> ignoring($is)
- );
- }
-
- return $is;
- }
-
- // -- Mock helpers
-
- public function returnStringFromEncoder(Yay_Invocation $invocation)
- {
- $args = $invocation->getArguments();
-
- return array_shift($args);
- }
- }
|