1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111 |
- <?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);
- }
-
- }
|