Skip to content

Commit

Permalink
Extended code coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
olvlvl committed Feb 3, 2015
1 parent eb52ded commit 2e6b762
Show file tree
Hide file tree
Showing 22 changed files with 991 additions and 143 deletions.
41 changes: 26 additions & 15 deletions lib/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

namespace ICanBoogie\HTTP;

use ICanBoogie\Accessor\AccessorTrait;

/**
* Representation of a POST file.
*
Expand All @@ -29,7 +31,7 @@
*/
class File implements \ICanBoogie\ToArray
{
use \ICanBoogie\PrototypeTrait;
use AccessorTrait;

static protected $types = [

Expand All @@ -39,6 +41,7 @@ class File implements \ICanBoogie\ToArray
'.jpg' => 'image/jpeg',
'.jpeg' => 'image/jpeg',
'.js' => 'application/javascript',
'.json' => 'application/json',
'.mp3' => 'audio/mpeg',
'.odt' => 'application/vnd.oasis.opendocument.text',
'.pdf' => 'application/pdf',
Expand All @@ -56,6 +59,7 @@ class File implements \ICanBoogie\ToArray
static protected $forced_types = [

'.js',
'.json',
'.php',
'.txt'

Expand All @@ -75,16 +79,25 @@ static public function from($properties_or_name)
{
$properties = isset($_FILES[$properties_or_name])
? $_FILES[$properties_or_name]
: [ 'name' => $properties_or_name];
: [ 'name' => basename($properties_or_name) ];
}
else if (is_array($properties_or_name))
{
$properties = $properties_or_name;
}

$properties = self::filter_initial_properties($properties);

return new static($properties);
}

static private function filter_initial_properties(array $properties)
{
static $initial_properties = [ 'name', 'type', 'size', 'tmp_name', 'error', 'pathname' ];

return array_intersect_key($properties, array_combine($initial_properties, $initial_properties));
}

/**
* Resolve the MIME type of a file.
*
Expand All @@ -99,7 +112,12 @@ static public function resolve_type($pathname, &$extension=null)
{
$extension = '.' . strtolower(pathinfo($pathname, PATHINFO_EXTENSION));

if (!in_array($extension, self::$forced_types) && file_exists($pathname) && extension_loaded('fileinfo'))
if (in_array($extension, self::$forced_types))
{
return self::$types[$extension];
}

if (file_exists($pathname) && extension_loaded('fileinfo'))
{
$fi = new \finfo(FILEINFO_MIME_TYPE);
$type = $fi->file($pathname);
Expand All @@ -108,14 +126,14 @@ static public function resolve_type($pathname, &$extension=null)
{
return isset(self::$types_alias[$type]) ? self::$types_alias[$type] : $type;
}
}
} // @codeCoverageIgnore

if (isset(self::$types[$extension]))
{
return self::$types[$extension];
}

return 'application/octet-stream';
return 'application/octet-stream'; // @codeCoverageIgnore
}

/**
Expand All @@ -131,15 +149,15 @@ static private function format($format, array $args=[], array $options=[])
{
if (class_exists('ICanBoogie\I18n\FormattedString', true))
{
return new \ICanBoogie\I18n\FormattedString($format, $args, $options);
return new \ICanBoogie\I18n\FormattedString($format, $args, $options); // @codeCoverageIgnore
}

if (class_exists('ICanBoogie\FormattedString', true))
{
return new \ICanBoogie\FormattedString($format, $args, $options);
}

return \ICanBoogie\format($format, $args);
return \ICanBoogie\format($format, $args); // @codeCoverageIgnore
}

/*
Expand Down Expand Up @@ -260,15 +278,8 @@ protected function get_pathname()

protected function __construct(array $properties)
{
static $initial_properties = [ 'name', 'type', 'size', 'tmp_name', 'error', 'pathname' ];

foreach ($properties as $property => $value)
{
if (!in_array($property, $initial_properties))
{
continue;
}

$this->$property = $value;
}

Expand Down Expand Up @@ -482,7 +493,7 @@ public function move($destination, $overwrite=false)
{
if (!rename($this->pathname, $destination))
{
throw new \Exception("Unable to move file to destination: $destination.");
throw new \Exception("Unable to move file to destination: $destination."); // @codeCoverageIgnore
}
}
else
Expand Down
2 changes: 1 addition & 1 deletion lib/FileList.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ static public function from($files)

protected $list;

public function __construct(array $files)
public function __construct(array $files = [])
{
foreach ($files as $id => $file)
{
Expand Down
15 changes: 14 additions & 1 deletion lib/Headers.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,10 +140,23 @@ public function __invoke()
continue;
}

header("$field: $value");
$this->send_header($field, $value);
}
}

/**
* Send header field.
*
* Note: The only reason for this method is testing.
*
* @param string $field
* @param string $value
*/
protected function send_header($field, $value)
{
header("$field: $value"); // @codeCoverageIgnore
} // @codeCoverageIgnore

/**
* Checks if a header field exists.
*
Expand Down
16 changes: 4 additions & 12 deletions lib/Headers/Header.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,21 +115,15 @@ static public function from($source)
/**
* Parse the provided source and extract its value and parameters.
*
* @param string|Header $source The source to create the instance from. If the source is
* an instance of {@link Header} its value and parameters are returned.
* @param string $source The source to create the instance from.
*
* @throws \InvalidArgumentException if `$source` is not a string nor an object implementing
* `__toString()`, or and instance of {@link Header}.
* `__toString()`.
*
* @return array
*/
static protected function parse($source)
{
if ($source instanceof self)
{
return [ $source->value, $source->parameters ];
}

if (is_object($source) && method_exists($source, '__toString'))
{
$source = (string) $source;
Expand Down Expand Up @@ -331,14 +325,12 @@ public function __set($property, $value)
*/
public function __unset($property)
{
if (isset($this->parameters[$property]))
if (!isset($this->parameters[$property]))
{
unset($this[$property]);

return;
}

throw new PropertyNotDefined([ $property, $this ]);
unset($this[$property]);
}

/**
Expand Down
34 changes: 13 additions & 21 deletions lib/Headers/HeaderParameter.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

namespace ICanBoogie\HTTP\Headers;

use ICanBoogie\PropertyNotDefined;
use ICanBoogie\Accessor\AccessorTrait;

/**
* Representation of a header parameter.
Expand All @@ -25,20 +25,32 @@
*/
class HeaderParameter
{
use AccessorTrait;

/**
* Token of the parameter.
*
* @var string
*/
protected $attribute;

protected function get_attribute()
{
return $this->attribute;
}

/**
* Value of the parameter.
*
* @var string
*/
public $value;

protected function get_charset()
{
return mb_detect_encoding($this->value) ?: 'ISO-8859-1';
}

/**
* Language of the value.
*
Expand Down Expand Up @@ -157,26 +169,6 @@ public function __construct($attribute, $value=null, $language=null)
$this->language = $language;
}

/**
* Handles the {@link $attribute} and {@link $charset} magic properties.
*
* @param string $property
*
* @throws PropertyNotDefined in attempt to get an undefined property.
*
* @return mixed
*/
public function __get($property)
{
switch ($property)
{
case 'attribute': return $this->attribute;
case 'charset': return mb_detect_encoding($this->value) ?: 'ISO-8859-1';
}

throw new PropertyNotDefined(array($property, $this));
}

/**
* Renders the attribute and value into a string.
*
Expand Down
5 changes: 3 additions & 2 deletions lib/RedirectResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,16 @@ function(Response $response) {
Redirecting to <a href="{$location}">{$title}</a>.
</body>
</html>
EOT;
EOT
; // @codeCoverageIgnore
},

$status, [ 'Location' => $url ] + $headers
);

if (!$this->status->is_redirect)
{
throw new \InvalidArgumentException("The HTTP status code is not a redirect: {$status}.");
throw new StatusCodeNotValid($this->status->code, "The HTTP status code is not a redirect: {$status}.");
}
}
}
Loading

0 comments on commit 2e6b762

Please sign in to comment.