├── .gitignore ├── .travis.yml ├── LICENSE ├── README.md ├── composer.json ├── phpunit.xml.dist ├── src └── Dflydev │ └── Canal │ ├── Analyzer │ └── Analyzer.php │ ├── Detector │ ├── ApacheMimeTypesExtensionDetector.php │ ├── CompositeDetector.php │ ├── DefaultDetectorFactory.php │ └── DetectorInterface.php │ ├── InternetMediaType │ ├── Adapter │ │ └── Webignition │ │ │ ├── InternetMediaType.php │ │ │ └── InternetMediaTypeParser.php │ ├── DefaultInternetMediaTypeParserFactory.php │ ├── InternetMediaTypeFactory.php │ ├── InternetMediaTypeInterface.php │ └── InternetMediaTypeParserInterface.php │ └── Metadata │ └── Metadata.php └── tests └── Dflydev └── Canal ├── Analyzer └── AnalyzerTest.php ├── Detector ├── ApacheMimeTypesExtensionDetectorTest.php ├── CompositeDetectorTest.php └── DefaultDetectorFactoryTest.php ├── InternetMediaType └── InternetMediaTypeFactoryTest.php └── Metadata └── MetadataTest.php /.gitignore: -------------------------------------------------------------------------------- 1 | composer.lock 2 | vendor 3 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: php 2 | 3 | php: 4 | - 5.3.3 5 | - 5.3 6 | - 5.4 7 | - 5.5 8 | 9 | before_script: 10 | - composer install --prefer-source --dev 11 | 12 | script: phpunit --coverage-text --verbose 13 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2013 Dragonfly Development Inc. 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy 4 | of this software and associated documentation files (the "Software"), to deal 5 | in the Software without restriction, including without limitation the rights 6 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | copies of the Software, and to permit persons to whom the Software is furnished 8 | to do so, subject to the following conditions: 9 | 10 | The above copyright notice and this permission notice shall be included in all 11 | copies or substantial portions of the Software. 12 | 13 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 | THE SOFTWARE. 20 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Canal 2 | ===== 3 | 4 | Content analysis for the purpose of determining [Internet media types][1]. 5 | 6 | 7 | Requirements 8 | ------------ 9 | 10 | * PHP 5.3+ 11 | 12 | 13 | Installation 14 | ------------ 15 | 16 | Through [Composer][2] as [dflydev/canal][3]. 17 | 18 | 19 | Usage 20 | ----- 21 | 22 | ```php 23 | detectFromFilename('/path/to/whatever.png'); 30 | 31 | // See the media type as a string 32 | print $internetMediaType->asString()."\n\n"; 33 | 34 | // See the media type's type 35 | print $internetMediaType->getType()."\n\n"; 36 | 37 | // See the media type's subtype 38 | print $internetMediaType->getSubtype()."\n\n"; 39 | 40 | // image/png 41 | // 42 | // image 43 | // 44 | // png 45 | ``` 46 | 47 | 48 | License 49 | ------- 50 | 51 | MIT, see LICENSE. 52 | 53 | 54 | Not Invented Here 55 | ----------------- 56 | 57 | This work was heavily influenced by [Apache Tika][4] and [Ferret][5]. 58 | 59 | 60 | [1]: http://en.wikipedia.org/wiki/Internet_media_type 61 | [2]: http://getcomposer.org 62 | [3]: https://packagist.org/packages/dflydev/canal 63 | [4]: http://tika.apache.org 64 | [5]: https://github.com/versionable/Ferret -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "dflydev/canal", 3 | "description": "Content analysis for the purpose of determining Internet media types.", 4 | "keywords": ["mime", "content", "type", "detection"], 5 | "license": "MIT", 6 | "authors": [ 7 | { 8 | "name": "Dragonfly Development Inc.", 9 | "email": "info@dflydev.com", 10 | "homepage": "http://dflydev.com" 11 | }, 12 | { 13 | "name": "Beau Simensen", 14 | "email": "beau@dflydev.com", 15 | "homepage": "http://beausimensen.com" 16 | } 17 | ], 18 | "require": { 19 | "php": ">=5.3.3", 20 | "dflydev/apache-mime-types": "1.0.*", 21 | "webignition/internet-media-type": "0.*" 22 | }, 23 | "autoload": { 24 | "psr-0": { 25 | "Dflydev\\Canal": "src" 26 | } 27 | }, 28 | "extra": { 29 | "branch-alias": { 30 | "dev-master": "1.0-dev" 31 | } 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /phpunit.xml.dist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | ./tests/Dflydev/Canal 6 | 7 | 8 | 9 | 10 | 11 | ./src/Dflydev/Canal/ 12 | 13 | 14 | 15 | -------------------------------------------------------------------------------- /src/Dflydev/Canal/Analyzer/Analyzer.php: -------------------------------------------------------------------------------- 1 | detector = $detector; 23 | $this->internetMediaTypeParser = DefaultInternetMediaTypeParserFactory::create(); 24 | } 25 | 26 | public function setDetector(DetectorInterface $detector) 27 | { 28 | $this->detector = $detector; 29 | 30 | return $this; 31 | } 32 | 33 | public function setInternetMediaTypeParser(InternetMediaTypeParserInterface $internetMediaTypeParser) 34 | { 35 | $this->internetMediaTypeParser = $internetMediaTypeParser; 36 | 37 | return $this; 38 | } 39 | 40 | public function getInternetMediaTypeParser() 41 | { 42 | return $this->internetMediaTypeParser; 43 | } 44 | 45 | public function getInternetMediaTypeFactory() 46 | { 47 | return $this->internetMediaTypeParser->getFactory(); 48 | } 49 | 50 | public function detect($input = null, Metadata $metadata = null) 51 | { 52 | return $this->normalizeInternetMediaType( 53 | $this->detector->detect($this->internetMediaTypeParser, $input, $metadata) 54 | ); 55 | } 56 | 57 | public function detectFromFilename($filename) 58 | { 59 | $metadata = new Metadata; 60 | $metadata->set(Metadata::RESOURCE_NAME_KEY, $filename); 61 | 62 | return $this->normalizeInternetMediaType( 63 | $this->detector->detect($this->internetMediaTypeParser, null, $metadata) 64 | ); 65 | } 66 | 67 | protected function normalizeInternetMediaType($internetMediaType = null) 68 | { 69 | if ($internetMediaType) { 70 | return $internetMediaType; 71 | } 72 | 73 | return $this->internetMediaTypeParser->getFactory()->createApplicationOctetStream(); 74 | } 75 | } 76 | -------------------------------------------------------------------------------- /src/Dflydev/Canal/Detector/ApacheMimeTypesExtensionDetector.php: -------------------------------------------------------------------------------- 1 | repository = $repository; 21 | } 22 | 23 | public function detect(InternetMediaTypeParserInterface $internetMediaTypeParser, $input = null, Metadata $metadata = null) 24 | { 25 | if (null === $metadata) { 26 | return null; 27 | } 28 | 29 | $filepath = $metadata->get(Metadata::RESOURCE_NAME_KEY); 30 | 31 | if (null === $filepath) { 32 | return null; 33 | } 34 | 35 | $extension = strtolower(pathinfo($filepath, PATHINFO_EXTENSION)); 36 | 37 | $type = $this->repository->findType($extension); 38 | 39 | if (null !== $type) { 40 | return $internetMediaTypeParser->parse($type); 41 | } 42 | 43 | return null; 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /src/Dflydev/Canal/Detector/CompositeDetector.php: -------------------------------------------------------------------------------- 1 | detectors = $detectors; 19 | } 20 | 21 | public function addDetector(DetectorInterface $detector) 22 | { 23 | $this->detectors[] = $detector; 24 | } 25 | 26 | public function detect(InternetMediaTypeParserInterface $internetMediaTypeParser, $input = null, Metadata $metadata = null) 27 | { 28 | foreach ($this->detectors as $detector) { 29 | $type = $detector->detect($internetMediaTypeParser, $input, $metadata); 30 | 31 | if (null !== $type) { 32 | return $type; 33 | } 34 | } 35 | 36 | return null; 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /src/Dflydev/Canal/Detector/DefaultDetectorFactory.php: -------------------------------------------------------------------------------- 1 | internetMediaType = $internetMediaType; 13 | } 14 | 15 | public function getType() 16 | { 17 | return $this->internetMediaType->getType(); 18 | } 19 | 20 | public function getSubtype() 21 | { 22 | return $this->internetMediaType->getSubtype(); 23 | } 24 | 25 | public function getParameter($parameter) 26 | { 27 | return $this->internetMediaType->getParameter($parameter); 28 | } 29 | 30 | public function getParameters() 31 | { 32 | return $this->internetMediaType->getParameters(); 33 | } 34 | 35 | public function asString() 36 | { 37 | return (string) $this->internetMediaType; 38 | } 39 | 40 | public function equals(InternetMediaTypeInterface $that = null) 41 | { 42 | if (null === $that) { 43 | return false; 44 | } 45 | 46 | return $this->asString() === $that->asString(); 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /src/Dflydev/Canal/InternetMediaType/Adapter/Webignition/InternetMediaTypeParser.php: -------------------------------------------------------------------------------- 1 | parser = $parser; 20 | $this->factory = new InternetMediaTypeFactory($this); 21 | } 22 | 23 | public function parse($type) 24 | { 25 | return new InternetMediaType($this->parser->parse($type)); 26 | } 27 | 28 | public function getFactory() 29 | { 30 | return $this->factory; 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /src/Dflydev/Canal/InternetMediaType/DefaultInternetMediaTypeParserFactory.php: -------------------------------------------------------------------------------- 1 | internetMediaTypeParser = DefaultInternetMediaTypeParserFactory::create(); 25 | } else { 26 | $this->internetMediaTypeParser = $internetMediaTypeParser; 27 | } 28 | } 29 | 30 | public function createTextPlain() 31 | { 32 | if (null === $this->textPlain) { 33 | $this->textPlain = $this->internetMediaTypeParser->parse(self::TEXT_PLAIN); 34 | } 35 | 36 | return $this->textPlain; 37 | } 38 | 39 | public function createTextHtml() 40 | { 41 | if (null === $this->textHtml) { 42 | $this->textHtml = $this->internetMediaTypeParser->parse(self::TEXT_HTML); 43 | } 44 | 45 | return $this->textHtml; 46 | } 47 | 48 | public function createApplicationOctetStream() 49 | { 50 | if (null === $this->applicationOctetStream) { 51 | $this->applicationOctetStream = $this->internetMediaTypeParser->parse(self::APPLICATION_OCTET_STREAM); 52 | } 53 | 54 | return $this->applicationOctetStream; 55 | } 56 | 57 | public function createApplicationXml() 58 | { 59 | if (null === $this->applicationXml) { 60 | $this->applicationXml = $this->internetMediaTypeParser->parse(self::APPLICATION_XML); 61 | } 62 | 63 | return $this->applicationXml; 64 | } 65 | 66 | public function createApplicationZip() 67 | { 68 | if (null === $this->applicationZip) { 69 | $this->applicationZip = $this->internetMediaTypeParser->parse(self::APPLICATION_ZIP); 70 | } 71 | 72 | return $this->applicationZip; 73 | } 74 | 75 | public function createApplication($type) 76 | { 77 | return $this->internetMediaTypeParser->parse('application/'.$type); 78 | } 79 | 80 | public function createAudio($type) 81 | { 82 | return $this->internetMediaTypeParser->parse('audio/'.$type); 83 | } 84 | 85 | public function createImage($type) 86 | { 87 | return $this->internetMediaTypeParser->parse('image/'.$type); 88 | } 89 | 90 | public function createText($type) 91 | { 92 | return $this->internetMediaTypeParser->parse('text/'.$type); 93 | } 94 | 95 | public function createVideo($type) 96 | { 97 | return $this->internetMediaTypeParser->parse('video/'.$type); 98 | } 99 | } 100 | -------------------------------------------------------------------------------- /src/Dflydev/Canal/InternetMediaType/InternetMediaTypeInterface.php: -------------------------------------------------------------------------------- 1 | getList($name); 14 | 15 | if (count($values)) { 16 | return $values[0]; 17 | } 18 | 19 | return null; 20 | } 21 | 22 | public function getList($name) 23 | { 24 | if (!isset($this->metadata[$name])) { 25 | return array(); 26 | } 27 | 28 | return $this->metadata[$name]; 29 | } 30 | 31 | public function set($name, $value) 32 | { 33 | if (!isset($this->metadata[$name])) { 34 | $this->metadata[$name] = array(); 35 | } 36 | 37 | $this->metadata[$name] = is_array($value) ? $value : array($value); 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /tests/Dflydev/Canal/Analyzer/AnalyzerTest.php: -------------------------------------------------------------------------------- 1 | detectFromFilename('/path/to/some-file.html'); 13 | 14 | $this->assertEquals('text/html', $internetMediaType->asString()); 15 | } 16 | 17 | public function testFallback() 18 | { 19 | $analyzer = new Analyzer; 20 | $internetMediaType = $analyzer->detectFromFilename('/path/to/some-file.canal-extension-foo'); 21 | 22 | $this->assertEquals('application/octet-stream', $internetMediaType->asString()); 23 | } 24 | 25 | public function testKnownTypeWithFactory() 26 | { 27 | $analyzer = new Analyzer; 28 | $internetMediaType = $analyzer->detectFromFilename('/path/to/some-file.html'); 29 | 30 | $this->assertTrue($analyzer->getInternetMediaTypeFactory()->createText('html')->equals($internetMediaType)); 31 | } 32 | 33 | public function testFallbackWithFactory() 34 | { 35 | $analyzer = new Analyzer; 36 | $internetMediaType = $analyzer->detectFromFilename('/path/to/some-file.canal-extension-foo'); 37 | 38 | $this->assertTrue($analyzer->getInternetMediaTypeFactory()->createApplicationOctetStream()->equals($internetMediaType)); 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /tests/Dflydev/Canal/Detector/ApacheMimeTypesExtensionDetectorTest.php: -------------------------------------------------------------------------------- 1 | getMock('Dflydev\Canal\InternetMediaType\InternetMediaTypeParserInterface'); 12 | $metadata = $this->getMock('Dflydev\Canal\Metadata\Metadata'); 13 | 14 | $detector = new ApacheMimeTypesExtensionDetector; 15 | 16 | $this->assertNull($detector->detect($internetMediaTypeParser)); 17 | $this->assertNull($detector->detect($internetMediaTypeParser, null, $metadata)); 18 | } 19 | 20 | public function testDefaultRepositoryWithNonsenseMetadata() 21 | { 22 | $internetMediaTypeParser = $this->getMock('Dflydev\Canal\InternetMediaType\InternetMediaTypeParserInterface'); 23 | $metadata = $this->getMock('Dflydev\Canal\Metadata\Metadata'); 24 | 25 | $metadata 26 | ->expects($this->once()) 27 | ->method('get') 28 | ->with(Metadata::RESOURCE_NAME_KEY) 29 | ->will($this->returnValue('/path/to/some/file.some-extension-clearly-does-not-exist')); 30 | 31 | $detector = new ApacheMimeTypesExtensionDetector; 32 | 33 | $this->assertNull($detector->detect($internetMediaTypeParser)); 34 | $this->assertNull($detector->detect($internetMediaTypeParser, null, $metadata)); 35 | } 36 | 37 | public function testDefaultRepositoryWithMetadata() 38 | { 39 | $internetMediaTypeParser = $this->getMock('Dflydev\Canal\InternetMediaType\InternetMediaTypeParserInterface'); 40 | $metadata = $this->getMock('Dflydev\Canal\Metadata\Metadata'); 41 | $expectedInternetMediaType = $this->getMock('Dflydev\Canal\InternetMediaType\InternetMediaTypeInterface'); 42 | 43 | $metadata 44 | ->expects($this->once()) 45 | ->method('get') 46 | ->with(Metadata::RESOURCE_NAME_KEY) 47 | ->will($this->returnValue('/path/to/some/file.css')); 48 | 49 | $internetMediaTypeParser 50 | ->expects($this->once()) 51 | ->method('parse') 52 | ->with('text/css') 53 | ->will($this->returnValue($expectedInternetMediaType)); 54 | 55 | $expectedInternetMediaType 56 | ->expects($this->once()) 57 | ->method('asString') 58 | ->will($this->returnValue('text/css')); 59 | 60 | $detector = new ApacheMimeTypesExtensionDetector; 61 | 62 | $detected = $detector->detect($internetMediaTypeParser, null, $metadata); 63 | 64 | $this->assertEquals('text/css', $detected->asString()); 65 | } 66 | 67 | public function testCustomRepositoryWithMetadata() 68 | { 69 | $repository = $this->getMock('Dflydev\ApacheMimeTypes\RepositoryInterface'); 70 | $internetMediaTypeParser = $this->getMock('Dflydev\Canal\InternetMediaType\InternetMediaTypeParserInterface'); 71 | $metadata = $this->getMock('Dflydev\Canal\Metadata\Metadata'); 72 | $expectedInternetMediaType = $this->getMock('Dflydev\Canal\InternetMediaType\InternetMediaTypeInterface'); 73 | 74 | $repository 75 | ->expects($this->once()) 76 | ->method('findType') 77 | ->with('css') 78 | ->will($this->returnValue('text/css')); 79 | 80 | $metadata 81 | ->expects($this->once()) 82 | ->method('get') 83 | ->with(Metadata::RESOURCE_NAME_KEY) 84 | ->will($this->returnValue('/path/to/some/file.css')); 85 | 86 | $internetMediaTypeParser 87 | ->expects($this->once()) 88 | ->method('parse') 89 | ->with('text/css') 90 | ->will($this->returnValue($expectedInternetMediaType)); 91 | 92 | $expectedInternetMediaType 93 | ->expects($this->once()) 94 | ->method('asString') 95 | ->will($this->returnValue('text/css')); 96 | 97 | $detector = new ApacheMimeTypesExtensionDetector($repository); 98 | 99 | $detected = $detector->detect($internetMediaTypeParser, null, $metadata); 100 | 101 | $this->assertEquals('text/css', $detected->asString()); 102 | } 103 | } 104 | -------------------------------------------------------------------------------- /tests/Dflydev/Canal/Detector/CompositeDetectorTest.php: -------------------------------------------------------------------------------- 1 | getMock('Dflydev\Canal\InternetMediaType\InternetMediaTypeParserInterface'); 10 | $metadata = $this->getMock('Dflydev\Canal\Metadata\Metadata'); 11 | 12 | $detector = new CompositeDetector; 13 | 14 | $this->assertNull($detector->detect($internetMediaTypeParser)); 15 | $this->assertNull($detector->detect($internetMediaTypeParser, null, $metadata)); 16 | } 17 | 18 | public function testAllFromConstructorWithNoMetadataFail() 19 | { 20 | $internetMediaTypeParser = $this->getMock('Dflydev\Canal\InternetMediaType\InternetMediaTypeParserInterface'); 21 | $internalDetector0 = $this->getMock('Dflydev\Canal\Detector\DetectorInterface'); 22 | $internalDetector1 = $this->getMock('Dflydev\Canal\Detector\DetectorInterface'); 23 | 24 | $internalDetector0 25 | ->expects($this->once()) 26 | ->method('detect') 27 | ->with($internetMediaTypeParser, null, null) 28 | ->will($this->returnValue(null)); 29 | 30 | $internalDetector1 31 | ->expects($this->once()) 32 | ->method('detect') 33 | ->with($internetMediaTypeParser, null, null) 34 | ->will($this->returnValue(null)); 35 | 36 | $detector = new CompositeDetector(array($internalDetector0, $internalDetector1)); 37 | 38 | $this->assertNull($detector->detect($internetMediaTypeParser)); 39 | } 40 | 41 | public function testAllFromConstructorWithMetadataFail() 42 | { 43 | $internetMediaTypeParser = $this->getMock('Dflydev\Canal\InternetMediaType\InternetMediaTypeParserInterface'); 44 | $metadata = $this->getMock('Dflydev\Canal\Metadata\Metadata'); 45 | $internalDetector0 = $this->getMock('Dflydev\Canal\Detector\DetectorInterface'); 46 | $internalDetector1 = $this->getMock('Dflydev\Canal\Detector\DetectorInterface'); 47 | 48 | $internalDetector0 49 | ->expects($this->once()) 50 | ->method('detect') 51 | ->with($internetMediaTypeParser, null, $metadata) 52 | ->will($this->returnValue(null)); 53 | 54 | $internalDetector1 55 | ->expects($this->once()) 56 | ->method('detect') 57 | ->with($internetMediaTypeParser, null, $metadata) 58 | ->will($this->returnValue(null)); 59 | 60 | $detector = new CompositeDetector(array($internalDetector0, $internalDetector1)); 61 | 62 | $this->assertNull($detector->detect($internetMediaTypeParser, null, $metadata)); 63 | } 64 | 65 | public function testAllFromConstructorAndAddWithNoMetadataFail() 66 | { 67 | $internetMediaTypeParser = $this->getMock('Dflydev\Canal\InternetMediaType\InternetMediaTypeParserInterface'); 68 | $internalDetector0 = $this->getMock('Dflydev\Canal\Detector\DetectorInterface'); 69 | $internalDetector1 = $this->getMock('Dflydev\Canal\Detector\DetectorInterface'); 70 | 71 | $internalDetector0 72 | ->expects($this->once()) 73 | ->method('detect') 74 | ->with($internetMediaTypeParser, null, null) 75 | ->will($this->returnValue(null)); 76 | 77 | $internalDetector1 78 | ->expects($this->once()) 79 | ->method('detect') 80 | ->with($internetMediaTypeParser, null, null) 81 | ->will($this->returnValue(null)); 82 | 83 | $detector = new CompositeDetector(array($internalDetector0)); 84 | 85 | $detector->addDetector($internalDetector1); 86 | 87 | $this->assertNull($detector->detect($internetMediaTypeParser)); 88 | } 89 | 90 | public function testAllFromConstructorAndAddWithMetadataFail() 91 | { 92 | $internetMediaTypeParser = $this->getMock('Dflydev\Canal\InternetMediaType\InternetMediaTypeParserInterface'); 93 | $metadata = $this->getMock('Dflydev\Canal\Metadata\Metadata'); 94 | $internalDetector0 = $this->getMock('Dflydev\Canal\Detector\DetectorInterface'); 95 | $internalDetector1 = $this->getMock('Dflydev\Canal\Detector\DetectorInterface'); 96 | 97 | $internalDetector0 98 | ->expects($this->once()) 99 | ->method('detect') 100 | ->with($internetMediaTypeParser, null, $metadata) 101 | ->will($this->returnValue(null)); 102 | 103 | $internalDetector1 104 | ->expects($this->once()) 105 | ->method('detect') 106 | ->with($internetMediaTypeParser, null, $metadata) 107 | ->will($this->returnValue(null)); 108 | 109 | $detector = new CompositeDetector(array($internalDetector0)); 110 | 111 | $detector->addDetector($internalDetector1); 112 | 113 | $this->assertNull($detector->detect($internetMediaTypeParser, null, $metadata)); 114 | } 115 | 116 | public function testFirstDetectorLocates() 117 | { 118 | $internetMediaTypeParser = $this->getMock('Dflydev\Canal\InternetMediaType\InternetMediaTypeParserInterface'); 119 | $metadata = $this->getMock('Dflydev\Canal\Metadata\Metadata'); 120 | $internalDetector0 = $this->getMock('Dflydev\Canal\Detector\DetectorInterface'); 121 | $internalDetector1 = $this->getMock('Dflydev\Canal\Detector\DetectorInterface'); 122 | $expectedInternetMediaType = $this->getMock('Dflydev\Canal\InternetMediaType\InternetMediaTypeInterface'); 123 | 124 | $internalDetector0 125 | ->expects($this->once()) 126 | ->method('detect') 127 | ->with($internetMediaTypeParser, null, $metadata) 128 | ->will($this->returnValue($expectedInternetMediaType)); 129 | 130 | $internalDetector1 131 | ->expects($this->never()) 132 | ->method('detect'); 133 | 134 | $expectedInternetMediaType 135 | ->expects($this->once()) 136 | ->method('asString') 137 | ->will($this->returnValue('text/css')); 138 | 139 | $detector = new CompositeDetector(array($internalDetector0, $internalDetector1)); 140 | 141 | $detected = $detector->detect($internetMediaTypeParser, null, $metadata); 142 | 143 | $this->assertEquals('text/css', $detected->asString()); 144 | } 145 | 146 | public function testLatterDetectorLocates() 147 | { 148 | $internetMediaTypeParser = $this->getMock('Dflydev\Canal\InternetMediaType\InternetMediaTypeParserInterface'); 149 | $metadata = $this->getMock('Dflydev\Canal\Metadata\Metadata'); 150 | $internalDetector0 = $this->getMock('Dflydev\Canal\Detector\DetectorInterface'); 151 | $internalDetector1 = $this->getMock('Dflydev\Canal\Detector\DetectorInterface'); 152 | $expectedInternetMediaType = $this->getMock('Dflydev\Canal\InternetMediaType\InternetMediaTypeInterface'); 153 | 154 | $internalDetector0 155 | ->expects($this->once()) 156 | ->method('detect') 157 | ->with($internetMediaTypeParser, null, $metadata) 158 | ->will($this->returnValue(null)); 159 | 160 | $internalDetector1 161 | ->expects($this->once()) 162 | ->method('detect') 163 | ->with($internetMediaTypeParser, null, $metadata) 164 | ->will($this->returnValue($expectedInternetMediaType)); 165 | 166 | $expectedInternetMediaType 167 | ->expects($this->once()) 168 | ->method('asString') 169 | ->will($this->returnValue('text/css')); 170 | 171 | $detector = new CompositeDetector(array($internalDetector0, $internalDetector1)); 172 | 173 | $detected = $detector->detect($internetMediaTypeParser, null, $metadata); 174 | 175 | $this->assertEquals('text/css', $detected->asString()); 176 | } 177 | } 178 | -------------------------------------------------------------------------------- /tests/Dflydev/Canal/Detector/DefaultDetectorFactoryTest.php: -------------------------------------------------------------------------------- 1 | assertInstanceOf('Dflydev\Canal\Detector\DetectorInterface', $detector); 12 | $this->assertInstanceOf('Dflydev\Canal\Detector\ApacheMimeTypesExtensionDetector', $detector); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /tests/Dflydev/Canal/InternetMediaType/InternetMediaTypeFactoryTest.php: -------------------------------------------------------------------------------- 1 | getMock('Dflydev\Canal\InternetMediaType\InternetMediaTypeParserInterface'); 15 | 16 | new InternetMediaTypeFactory($internetMediaTypeParser); 17 | } 18 | 19 | public function testCreateTextPlain() 20 | { 21 | $internetMediaType = $this->getMock('Dflydev\Canal\InternetMediaType\InternetMediaTypeInterface'); 22 | $internetMediaType 23 | ->expects($this->once()) 24 | ->method('asString') 25 | ->will($this->returnValue('text/plain')); 26 | 27 | $internetMediaTypeParser = $this->getMock('Dflydev\Canal\InternetMediaType\InternetMediaTypeParserInterface'); 28 | $internetMediaTypeParser 29 | ->expects($this->once()) 30 | ->method('parse') 31 | ->with('text/plain') 32 | ->will($this->returnValue($internetMediaType)); 33 | 34 | $factory = new InternetMediaTypeFactory($internetMediaTypeParser); 35 | $textPlain = $factory->createTextPlain(); 36 | $this->assertEquals('text/plain', $textPlain->asString()); 37 | } 38 | 39 | public function testCreateTextHtml() 40 | { 41 | $internetMediaType = $this->getMock('Dflydev\Canal\InternetMediaType\InternetMediaTypeInterface'); 42 | $internetMediaType 43 | ->expects($this->once()) 44 | ->method('asString') 45 | ->will($this->returnValue('text/html')); 46 | 47 | $internetMediaTypeParser = $this->getMock('Dflydev\Canal\InternetMediaType\InternetMediaTypeParserInterface'); 48 | $internetMediaTypeParser 49 | ->expects($this->once()) 50 | ->method('parse') 51 | ->with('text/html') 52 | ->will($this->returnValue($internetMediaType)); 53 | 54 | $factory = new InternetMediaTypeFactory($internetMediaTypeParser); 55 | $textHtml = $factory->createTextHtml(); 56 | $this->assertEquals('text/html', $textHtml->asString()); 57 | } 58 | 59 | public function testCreateApplicationOctetStream() 60 | { 61 | $internetMediaType = $this->getMock('Dflydev\Canal\InternetMediaType\InternetMediaTypeInterface'); 62 | $internetMediaType 63 | ->expects($this->once()) 64 | ->method('asString') 65 | ->will($this->returnValue('application/octet-stream')); 66 | 67 | $internetMediaTypeParser = $this->getMock('Dflydev\Canal\InternetMediaType\InternetMediaTypeParserInterface'); 68 | $internetMediaTypeParser 69 | ->expects($this->once()) 70 | ->method('parse') 71 | ->with('application/octet-stream') 72 | ->will($this->returnValue($internetMediaType)); 73 | 74 | $factory = new InternetMediaTypeFactory($internetMediaTypeParser); 75 | $applicationOctetStream = $factory->createApplicationOctetStream(); 76 | $this->assertEquals('application/octet-stream', $applicationOctetStream->asString()); 77 | } 78 | 79 | public function testCreateApplicationXml() 80 | { 81 | $internetMediaType = $this->getMock('Dflydev\Canal\InternetMediaType\InternetMediaTypeInterface'); 82 | $internetMediaType 83 | ->expects($this->once()) 84 | ->method('asString') 85 | ->will($this->returnValue('application/xml')); 86 | 87 | $internetMediaTypeParser = $this->getMock('Dflydev\Canal\InternetMediaType\InternetMediaTypeParserInterface'); 88 | $internetMediaTypeParser 89 | ->expects($this->once()) 90 | ->method('parse') 91 | ->with('application/xml') 92 | ->will($this->returnValue($internetMediaType)); 93 | 94 | $factory = new InternetMediaTypeFactory($internetMediaTypeParser); 95 | $applicationXml = $factory->createApplicationXml(); 96 | $this->assertEquals('application/xml', $applicationXml->asString()); 97 | } 98 | 99 | public function testCreateApplicationZip() 100 | { 101 | $internetMediaType = $this->getMock('Dflydev\Canal\InternetMediaType\InternetMediaTypeInterface'); 102 | $internetMediaType 103 | ->expects($this->once()) 104 | ->method('asString') 105 | ->will($this->returnValue('application/zip')); 106 | 107 | $internetMediaTypeParser = $this->getMock('Dflydev\Canal\InternetMediaType\InternetMediaTypeParserInterface'); 108 | $internetMediaTypeParser 109 | ->expects($this->once()) 110 | ->method('parse') 111 | ->with('application/zip') 112 | ->will($this->returnValue($internetMediaType)); 113 | 114 | $factory = new InternetMediaTypeFactory($internetMediaTypeParser); 115 | $applicationZip = $factory->createApplicationZip(); 116 | $this->assertEquals('application/zip', $applicationZip->asString()); 117 | } 118 | 119 | public function testCreateCustomApplication() 120 | { 121 | $internetMediaType = $this->getMock('Dflydev\Canal\InternetMediaType\InternetMediaTypeInterface'); 122 | $internetMediaType 123 | ->expects($this->once()) 124 | ->method('asString') 125 | ->will($this->returnValue('application/canal-custom')); 126 | 127 | $internetMediaTypeParser = $this->getMock('Dflydev\Canal\InternetMediaType\InternetMediaTypeParserInterface'); 128 | $internetMediaTypeParser 129 | ->expects($this->once()) 130 | ->method('parse') 131 | ->with('application/canal-custom') 132 | ->will($this->returnValue($internetMediaType)); 133 | 134 | $factory = new InternetMediaTypeFactory($internetMediaTypeParser); 135 | $custom = $factory->createApplication('canal-custom'); 136 | $this->assertEquals('application/canal-custom', $custom->asString()); 137 | } 138 | 139 | public function testCreateCustomAudio() 140 | { 141 | $internetMediaType = $this->getMock('Dflydev\Canal\InternetMediaType\InternetMediaTypeInterface'); 142 | $internetMediaType 143 | ->expects($this->once()) 144 | ->method('asString') 145 | ->will($this->returnValue('audio/canal-custom')); 146 | 147 | $internetMediaTypeParser = $this->getMock('Dflydev\Canal\InternetMediaType\InternetMediaTypeParserInterface'); 148 | $internetMediaTypeParser 149 | ->expects($this->once()) 150 | ->method('parse') 151 | ->with('audio/canal-custom') 152 | ->will($this->returnValue($internetMediaType)); 153 | 154 | $factory = new InternetMediaTypeFactory($internetMediaTypeParser); 155 | $custom = $factory->createAudio('canal-custom'); 156 | $this->assertEquals('audio/canal-custom', $custom->asString()); 157 | } 158 | 159 | public function testCreateCustomImage() 160 | { 161 | $internetMediaType = $this->getMock('Dflydev\Canal\InternetMediaType\InternetMediaTypeInterface'); 162 | $internetMediaType 163 | ->expects($this->once()) 164 | ->method('asString') 165 | ->will($this->returnValue('image/canal-custom')); 166 | 167 | $internetMediaTypeParser = $this->getMock('Dflydev\Canal\InternetMediaType\InternetMediaTypeParserInterface'); 168 | $internetMediaTypeParser 169 | ->expects($this->once()) 170 | ->method('parse') 171 | ->with('image/canal-custom') 172 | ->will($this->returnValue($internetMediaType)); 173 | 174 | $factory = new InternetMediaTypeFactory($internetMediaTypeParser); 175 | $custom = $factory->createImage('canal-custom'); 176 | $this->assertEquals('image/canal-custom', $custom->asString()); 177 | } 178 | 179 | public function testCreateCustomText() 180 | { 181 | $internetMediaType = $this->getMock('Dflydev\Canal\InternetMediaType\InternetMediaTypeInterface'); 182 | $internetMediaType 183 | ->expects($this->once()) 184 | ->method('asString') 185 | ->will($this->returnValue('text/canal-custom')); 186 | 187 | $internetMediaTypeParser = $this->getMock('Dflydev\Canal\InternetMediaType\InternetMediaTypeParserInterface'); 188 | $internetMediaTypeParser 189 | ->expects($this->once()) 190 | ->method('parse') 191 | ->with('text/canal-custom') 192 | ->will($this->returnValue($internetMediaType)); 193 | 194 | $factory = new InternetMediaTypeFactory($internetMediaTypeParser); 195 | $custom = $factory->createText('canal-custom'); 196 | $this->assertEquals('text/canal-custom', $custom->asString()); 197 | } 198 | 199 | public function testCreateCustomVideo() 200 | { 201 | $internetMediaType = $this->getMock('Dflydev\Canal\InternetMediaType\InternetMediaTypeInterface'); 202 | $internetMediaType 203 | ->expects($this->once()) 204 | ->method('asString') 205 | ->will($this->returnValue('video/canal-custom')); 206 | 207 | $internetMediaTypeParser = $this->getMock('Dflydev\Canal\InternetMediaType\InternetMediaTypeParserInterface'); 208 | $internetMediaTypeParser 209 | ->expects($this->once()) 210 | ->method('parse') 211 | ->with('video/canal-custom') 212 | ->will($this->returnValue($internetMediaType)); 213 | 214 | $factory = new InternetMediaTypeFactory($internetMediaTypeParser); 215 | $custom = $factory->createVideo('canal-custom'); 216 | $this->assertEquals('video/canal-custom', $custom->asString()); 217 | } 218 | } 219 | -------------------------------------------------------------------------------- /tests/Dflydev/Canal/Metadata/MetadataTest.php: -------------------------------------------------------------------------------- 1 | set('a', 'A'); 11 | 12 | $this->assertCount(1, $a = $metadata->getList('a')); 13 | $this->assertEquals('A', $a[0]); 14 | $this->assertEquals('A', $metadata->get('a')); 15 | 16 | $this->assertCount(0, $metadata->getList('b')); 17 | $this->assertNull($metadata->get('b')); 18 | } 19 | 20 | public function testMultiple() 21 | { 22 | $metadata = new Metadata; 23 | $metadata->set('a', array('A')); 24 | $metadata->set('b', array('B', 'BB', 'BBB')); 25 | 26 | $this->assertCount(1, $metadata->getList('a')); 27 | $this->assertEquals('A', $metadata->get('a')); 28 | 29 | $this->assertCount(3, $b = $metadata->getList('b')); 30 | $this->assertEquals(array('B', 'BB', 'BBB'), $b); 31 | $this->assertEquals('B', $metadata->get('b')); 32 | } 33 | } 34 | --------------------------------------------------------------------------------