diff --git a/.scrutinizer.yml b/.scrutinizer.yml new file mode 100755 index 0000000..c8221ee --- /dev/null +++ b/.scrutinizer.yml @@ -0,0 +1,25 @@ +imports: + - php + +tools: + external_code_coverage: + timeout: 1000 + # PHP + + # Don't like PSR standards, not going to get messages for them! + php_code_sniffer: false + + php_sim: true + # Can't be used with similarity analyzer + php_cpd: false + + php_mess_detector: true + php_pdepend: true + php_loc: true + php_analyzer: + config: + metrics_lack_of_cohesion_methods: + enabled: true + doc_comment_fixes: + enabled: true + php_hhvm: true \ No newline at end of file diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..b4d6112 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,19 @@ +language: php + +php: + - 5.4 + - 5.5 + - 5.6 + - hhvm + +script: + - mkdir -p build/logs + - cd tests && phpunit --coverage-clover build/logs/clover.xml + +after_script: + - wget https://scrutinizer-ci.com/ocular.phar + - php ocular.phar code-coverage:upload --format=php-clover build/logs/clover.xml + +matrix: + allow_failures: + - hhvm \ No newline at end of file diff --git a/Sleepy/Core/aType.php b/Sleepy/Core/Abstracts/Type.php old mode 100755 new mode 100644 similarity index 76% rename from Sleepy/Core/aType.php rename to Sleepy/Core/Abstracts/Type.php index 392d0cb..73936ca --- a/Sleepy/Core/aType.php +++ b/Sleepy/Core/Abstracts/Type.php @@ -7,17 +7,17 @@ * but aiming to be as flexible as possible * * @author Timothy J. Warren - * @package Sleepy/core */ -namespace Sleepy\Core; +namespace Sleepy\Core\Abstracts; +use Sleepy\Core\Interfaces\Type as iType; use Sleepy\Exception\NotImplementedException; /** * Abstract class with helpful functionality implementing type functionality */ -abstract class aType implements iType { +abstract class Type implements iType { /** * The data in the current type wrapper @@ -41,16 +41,32 @@ abstract class aType implements iType { */ public function __construct($data = NULL) { - if (empty($this->mime)) { throw new NotImplementedException("Output types must have a mime type defined."); } + $this->set_data($data); + } + + /** + * Set data to serialize + * + * @param mixed $data + * @return mixed + */ + protected function set_data(&$data) + { if ( ! is_null($data)) { $this->data = $data; } + else + { + $data = $this->data; + } + + return $data; } /** @@ -75,4 +91,4 @@ abstract class aType implements iType { } } -// End of core/aType.php \ No newline at end of file +// End of Core/Abstracts/Type.php \ No newline at end of file diff --git a/Sleepy/Core/Config.php b/Sleepy/Core/Config.php new file mode 100755 index 0000000..8ee7423 --- /dev/null +++ b/Sleepy/Core/Config.php @@ -0,0 +1,66 @@ +data[$name] = $conf; + } + + /** + * Get the specific parameter from the specified file + * + * @param string $file + * @param string $key + * @return mixed + */ + public function get($file, $key=NULL) + { + if (is_null($key)) + { + return $this->data[$file]; + } + + return $this->data[$file][$key]; + } +} +// End of Core/Config.php \ No newline at end of file diff --git a/Sleepy/Core/Input.php b/Sleepy/Core/Input.php index bc07c3b..fe7120a 100755 --- a/Sleepy/Core/Input.php +++ b/Sleepy/Core/Input.php @@ -7,13 +7,20 @@ * but aiming to be as flexible as possible * * @author Timothy J. Warren - * @package Sleepy/core */ namespace Sleepy\Core; /** * Class for accessing request data + * + * @method array server() + * @method array env() + * @method array get() + * @method array post() + * @method array put() + * @method array delete() + * @method array cookie() */ class Input { @@ -42,6 +49,13 @@ class Input { */ protected $delete = []; + /** + * Class member for options data + * + * @var array + */ + protected $options = []; + // -------------------------------------------------------------------------- // ! Working around PHP for nicer usability // -------------------------------------------------------------------------- @@ -82,12 +96,13 @@ class Input { $this->verb = \strtolower($_SERVER['REQUEST_METHOD']); // Parse put and delete requests into input variables + // @codeCoverageIgnoreStart if (isset($this->{$this->verb})) { $raw = \file_get_contents('php://input'); \parse_str($raw, $this->{$this->verb}); } - + // Set mapping for superglobals, since // variable variables don't seem to work // with superglobals :/ @@ -98,13 +113,17 @@ class Input { 'env' => $_ENV, 'cookie' => $_COOKIE ]; + // @codeCoverageIgnoreEnd // Parse request headers from $_SERVER foreach($_SERVER as $key => $val) { if (strpos($key, 'HTTP_') === 0) { - $new_key = strtolower(str_replace('HTTP_', '', $key)); + $new_key = \strtolower(\strtr($key, [ + 'HTTP_' => '', + '_' => '-' + ])); $this->request_headers[$new_key] = $val; } } @@ -119,7 +138,7 @@ class Input { * @param string $name - name of input array * @param array $args - function arguments * @return mixed - * @throws DomainException + * @throws \DomainException */ public function __call($name, $args=[]) { @@ -134,11 +153,12 @@ class Input { // Get a superglobal ($_VAR) value return $this->get_superglobal_var($name, $index, $filter); } - else if(isset($this->$name)) + else if(isset($this->$name)) // @codeCoverageIgnoreStart { // Get a input variable not in a superglobal (eg. PUT/DELETE) return $this->get_request_var($name, $index, $filter); } + // @codeCoverageIgnoreEnd // What kind of request are you trying to make?! throw new \DomainException('Invalid input array.'); @@ -154,7 +174,7 @@ class Input { { if ($index !== NULL) { - $index = (str_replace([' ', '-'], '_', $index)); + $index = (\strtolower(\str_replace([' ', '_'], '-', $index))); if (isset($this->request_headers[$index])) { @@ -209,7 +229,7 @@ class Input { $has_eq = strpos($value, '=') !== FALSE; // Parse the user agent separately - if ($header === 'user_agent') + if ($header === 'user-agent') { $this->parsed_headers[$header] = $this->parse_user_agent($value); continue; @@ -231,21 +251,6 @@ class Input { parse_str($value, $this->parsed_headers[$header]); continue; } - // For headers with commas and semicolons, break first on commas, - // then on semicolons - else if ($has_semi && $has_comma) - { - $values = explode(",", $value); - foreach($values as &$v) - { - if (strpos($v, ";") !== FALSE) - { - $v = explode(";", $v); - } - } - - $this->parsed_headers[$header] = $values; - } // Anything else, just leave it as a string else { @@ -308,29 +313,48 @@ class Input { */ protected function parse_accept_header($value) { + $q_types = []; + // A fake value so I can shift it off to have a 1-indexed array + $high_types = []; $output = []; - $index = 0; + $count = 1; // Split into segments of different values $groups = explode(',', $value); foreach($groups as $group) { + $group = \trim($group); $pair = explode(';q=', $group); if (count($pair) === 2) { list($val, $q) = $pair; - $output[$q] = $val; + $q_types[$q] = $val; } else { - $index++; - $output[$index] = current($pair); + $high_types[$count] = current($pair); + $count++; } } - ksort($output, SORT_NATURAL); + // Add an additional fake value so we can + // have a 1-indexed array + $high_types[$count] = 'foo'; + $high_types = array_reverse($high_types); + unset($high_types[0]); + + $output = $q_types; + + // Merge the arrays manually to maintain + // keys, and thus ordering + foreach($high_types as $k => $v) + { + $output[$k] = $v; + } + + krsort($output, SORT_NUMERIC); return $output; } @@ -338,6 +362,7 @@ class Input { /** * Get input var(s) from non-defined superglobal * + * @codeCoverageIgnore * @param string $type - input array * @param string $index - variable in the input array * @param int $filter - PHP filter_var flag diff --git a/Sleepy/Core/iType.php b/Sleepy/Core/Interfaces/Type.php old mode 100755 new mode 100644 similarity index 85% rename from Sleepy/Core/iType.php rename to Sleepy/Core/Interfaces/Type.php index 6cc8024..0bd42bb --- a/Sleepy/Core/iType.php +++ b/Sleepy/Core/Interfaces/Type.php @@ -7,15 +7,14 @@ * but aiming to be as flexible as possible * * @author Timothy J. Warren - * @package Sleepy/core */ -namespace Sleepy\core; +namespace Sleepy\Core\Interfaces; /** * Interface for output formats */ -interface iType { +interface Type { /** * Convert the data to the output format @@ -34,4 +33,4 @@ interface iType { public function unserialize($data_string); } -// End of core/iType.php \ No newline at end of file +// End of Core/Interfaces/Type.php \ No newline at end of file diff --git a/Sleepy/Core/Output.php b/Sleepy/Core/Output.php index d5aee68..219c46f 100755 --- a/Sleepy/Core/Output.php +++ b/Sleepy/Core/Output.php @@ -7,10 +7,9 @@ * but aiming to be as flexible as possible * * @author Timothy J. Warren - * @package Sleepy/core */ -namespace Sleepy\core; +namespace Sleepy\Core; /** * Default output class @@ -34,16 +33,23 @@ class Output { /** * The serialization object for the current data type * - * @var Sleepy\core\aType + * @var Sleepy\Core\aType */ protected $type_wrapper; /** * The input object * - * @var Sleepy\core\Input + * @var Sleepy\Core\Input */ protected $input; + + /** + * Config object + * + * @var Sleepy\Core\Config; + */ + protected $config; // -------------------------------------------------------------------------- // ! Methods @@ -52,10 +58,12 @@ class Output { /** * Create the output object * - * @param array $config + * @param Config $config + * @param Input $input */ - public function __construct(Input $input) + public function __construct(Config $config, Input $input) { + $this->config = $config; $this->input = $input; } @@ -92,15 +100,23 @@ class Output { /** * Set the data to be output to the endpoint * - * @param string $type - The datatype to send - * @param mixed $data + * @param string $type - The data format to send + * @param mixed $data - The data to send * @return void */ - public function set_data($type = 'json', $data) + public function set_data($type = 'html', $data = NULL) { + if (is_null($data) && ! empty($this->data)) + { + $data = $this->data; + } + + // Set instance data + $this->data = $data; + // Get the appropriate output format for the client // And set the data - $this->get_accepted_type($type, $data); + $this->get_accepted_type($type, $this->data); } // -------------------------------------------------------------------------- @@ -135,16 +151,32 @@ class Output { $type_map[$mime] = $type_class; } - foreach($accept as $type => $q) + // Order type preference first by + // input, then by accept flags + foreach($type_map as $type => $obj) { - if (array_key_exists($type, $type_map)) + if (\array_key_exists($type, $accept)) { - $accepted[$q] = $type; + $q = $accept[$type]; + + if ($q >= 1) + { + $accepted[] = $type; + } + else + { + $accepted[$q] = $type; + } } } - krsort($accepted); + // Default to html fow wildcard accept values + if (empty($accepted) && \array_key_exists('*/*', $accept)) + { + $accepted[1] = 'text/html'; + } + // Use the first output type to output the data $class = $type_map[current($accepted)]; $this->type_wrapper = $classes[$class]; @@ -178,4 +210,4 @@ class Output { } } } -// End of core/Output.php \ No newline at end of file +// End of Core/Output.php \ No newline at end of file diff --git a/Sleepy/Core/Router.php b/Sleepy/Core/Router.php index 6bdae5b..5f19a01 100755 --- a/Sleepy/Core/Router.php +++ b/Sleepy/Core/Router.php @@ -7,7 +7,6 @@ * but aiming to be as flexible as possible * * @author Timothy J. Warren - * @package Sleepy/core */ namespace Sleepy\Core; diff --git a/Sleepy/Exception/NotImplementedException.php b/Sleepy/Exception/NotImplementedException.php index 6a3a4d1..1df6a70 100755 --- a/Sleepy/Exception/NotImplementedException.php +++ b/Sleepy/Exception/NotImplementedException.php @@ -7,7 +7,6 @@ * but aiming to be as flexible as possible * * @author Timothy J. Warren - * @package Sleepy/exceptions */ namespace Sleepy\Exception; diff --git a/Sleepy/Traits/getSet.php b/Sleepy/Traits/getSet.php new file mode 100644 index 0000000..1f32e2f --- /dev/null +++ b/Sleepy/Traits/getSet.php @@ -0,0 +1,52 @@ + 'get', + 'set' => 'set' + ); + + if ( ! property_exists($this, $val) || ! isset($valid_types[$type])) + { + return NULL; + } + + // Auto-magical getters and setters + if ($type === 'get') + { + return $this->$val; + } + elseif ($type === 'set') + { + $this->$val = current($args); + } + } +} +// End of GetSet.php \ No newline at end of file diff --git a/Sleepy/Type/HTML.php b/Sleepy/Type/HTML.php index 8b32b08..d687b68 100644 --- a/Sleepy/Type/HTML.php +++ b/Sleepy/Type/HTML.php @@ -7,12 +7,11 @@ * but aiming to be as flexible as possible * * @author Timothy J. Warren - * @package Sleepy/types */ namespace Sleepy\Type; -use Sleepy\Core\aType; +use \Sleepy\Core\Abstracts\Type as aType; /** * Class for HTML output @@ -34,14 +33,7 @@ class HTML extends aType { */ public function serialize($data = NULL) { - if ( ! is_null($data)) - { - $this->data = $data; - } - else - { - $data = $this->data; - } + $this->set_data($data); if (is_string($data)) return $data; @@ -62,19 +54,4 @@ class HTML extends aType { } -// -------------------------------------------------------------------------- -// ! Helper function -// -------------------------------------------------------------------------- - -/** - * Function to simplify type instantiation - * - * @param mixed $data - * @return JSON - */ -function HTML($data = NULL) -{ - return new JSON($data); -} - // End of types/JSON.php \ No newline at end of file diff --git a/Sleepy/Type/JSON.php b/Sleepy/Type/JSON.php index b3ed0f1..062a1fe 100755 --- a/Sleepy/Type/JSON.php +++ b/Sleepy/Type/JSON.php @@ -7,12 +7,11 @@ * but aiming to be as flexible as possible * * @author Timothy J. Warren - * @package Sleepy/types */ namespace Sleepy\Type; -use Sleepy\Core\aType; +use \Sleepy\Core\Abstracts\Type as aType; /** * Class for JSON output @@ -34,15 +33,7 @@ class JSON extends aType { */ public function serialize($data = NULL) { - if ( ! is_null($data)) - { - $this->data = $data; - } - else - { - $data = $this->data; - } - + $this->set_data($data); return json_encode($data, JSON_PRETTY_PRINT); } @@ -59,19 +50,4 @@ class JSON extends aType { } -// -------------------------------------------------------------------------- -// ! Helper function -// -------------------------------------------------------------------------- - -/** - * Function to simplify type instantiation - * - * @param mixed $data - * @return JSON - */ -function JSON($data = NULL) -{ - return new JSON($data); -} - // End of types/JSON.php \ No newline at end of file diff --git a/Sleepy/Type/XML.php b/Sleepy/Type/XML.php new file mode 100644 index 0000000..6935023 --- /dev/null +++ b/Sleepy/Type/XML.php @@ -0,0 +1,100 @@ +set_data($data); + + $xmlObject = new \SimpleXMLElement(''); + $this->array_to_xml($data, $xmlObject); + + return $xmlObject->asXML(); + } + + /** + * Convert xml to php data + * + * @param string $string + * @return object + */ + public function unserialize($string) + { + $xml = \simplexml_load_string($string); + $json = json_encode($xml); + $object = json_decode($json); + + return $object; + } + + /** + * Recursively generate xml from an array or object + * + * @param array|object $array + * @param \SimpleXMLElement $xmlObject + */ + private function array_to_xml($array, \SimpleXMLElement &$xmlObject) + { + foreach($array as $key => $val) + { + $key = $this->fix_xml_key($key); + + if ( ! is_scalar($val)) + { + $subnode = $xmlObject->addChild($key); + $this->array_to_xml($val, $subnode); + } + else + { + $xmlObject->addChild($key, \htmlspecialchars($val, ENT_XML1, 'UTF-8')); + } + } + } + + /** + * Make an invalid xml key more valid + * + * @param string $key + * @return string + */ + private function fix_xml_key($key) + { + if (is_numeric($key)) + { + $key = "item_{$key}"; + } + + return preg_replace('`[^a-zA-Z0-9_:]`', '_', $key); + } + +} +// End of Type/XML.php \ No newline at end of file diff --git a/Sleepy/Type/YAML.php b/Sleepy/Type/YAML.php index 2107267..db898ca 100755 --- a/Sleepy/Type/YAML.php +++ b/Sleepy/Type/YAML.php @@ -7,16 +7,15 @@ * but aiming to be as flexible as possible * * @author Timothy J. Warren - * @package Sleepy/types */ -namespace Sleepy\types; +namespace Sleepy\Type; -use Sleepy\core\aType; -use Symfony\Component\Yaml as YML; +use \Sleepy\Core\Abstracts\Type as aType; +use Symfony\Component\Yaml\Yaml as YML; -/** - * Class for YAML output +/** + * Class for YAML output */ class YAML extends aType { @@ -26,7 +25,7 @@ class YAML extends aType { * @var string */ protected $mime = 'text/yaml'; - + /** * Convert the data into the output format * @@ -35,21 +34,14 @@ class YAML extends aType { */ public function serialize($data = null) { - if ( ! is_null($data)) - { - $this->data = $data; - } - else - { - $data = $this->data; - } - + $this->set_data($data); + // Yaml class doesn't support objects, cast them to arrays $data = (array) $data; - + return YML::dump($data); } - + /** * Convert the encoded data to a native format * diff --git a/Sleepy/autoload.php b/Sleepy/autoload.php new file mode 100644 index 0000000..fbea908 --- /dev/null +++ b/Sleepy/autoload.php @@ -0,0 +1,40 @@ + 'index', - ]; // End of config/routes.php \ No newline at end of file diff --git a/application/config/type_class_map.php b/application/config/type_class_map.php new file mode 100644 index 0000000..047ecb2 --- /dev/null +++ b/application/config/type_class_map.php @@ -0,0 +1,26 @@ + 'JSON', + 'text/yaml' => 'YAML', + 'application/yaml' => 'YAML', + 'text/html' => 'HTML', + 'application/xhtml+xml' => 'HTML', + '*/*' => 'HTML' +]; + +// End of config/type_class_map.php \ No newline at end of file diff --git a/composer.json b/composer.json index 9f0b916..bb6edf4 100755 --- a/composer.json +++ b/composer.json @@ -9,6 +9,9 @@ "phpunit/phpunit":"3.7.*" }, "require": { + "symfony/yaml": "2.4.*", + "aura/router": "2.0.*@dev", + "aura/di": "2.0.*@dev", "php": ">=5.4.0" } } \ No newline at end of file diff --git a/index.php b/index.php index ab3114f..ca3afbf 100755 --- a/index.php +++ b/index.php @@ -10,41 +10,36 @@ * @package Sleepy */ -namespace Sleepy; +namespace Sleepy\Core; + +// Include namespaces +use Aura\Di\Container as DiContainer; +use Aura\Di\Factory as DiFactory; +use Aura\Router\RouterFactory; define('BASEPATH', __DIR__ . '/'); - +define('SLEEPY_DIR', 'Sleepy'); +define('APP_DIR', 'application'); define('APPPATH', __DIR__ . '/application/'); -\spl_autoload_register(function($item) { - $path_items = \explode('\\', \ltrim($item, '\\')); +// Include 3rd party dependencies +include BASEPATH . '/vendor/autoload.php'; - // If the namespace is a straight mapping to the class, just load it - $simple_path = \implode('/', $path_items); - $file = BASEPATH . "{$simple_path}.php"; - if (file_exists($file)) - { - require_once($file); - return; - } +// Load the autoloader +require BASEPATH . SLEEPY_DIR . '/autoload.php'; - // Check the application folder - $file = str_replace("Sleepy", 'application', $file); - if (file_exists($file)) - { - require_once($file); - return; - } -}); +$di = new DiContainer(new DiFactory()); +$di->set('config', new Config()); +$di->set('input', new Input()); +$di->set('output', new Output($di->get('config'), $di->get('input'))); -$i = new \Sleepy\Core\Input(); -$o = new \Sleepy\Core\Output($i); -$browser = \get_browser(NULL); -$browser->browser_name_regex = utf8_encode($browser->browser_name_regex); -$o->set_data(['json','html'], [ +$browser->browser_name_regex = \utf8_encode($browser->browser_name_regex); +$di->get('output')->set_data(['json','yaml','html'], [ '$_SERVER' => $i->server(), '$_GET' => $i->get(), '$_POST' => $i->post(), + '$_PUT' => $i->put(), + '$_DELETE' => $i->delete(), '$_ENV' => $i->env(), '$_COOKIE' => $i->cookie(), 'browser' => $browser, diff --git a/tests/Bootstrap.php b/tests/Bootstrap.php index 2d6b1b9..4ed6761 100755 --- a/tests/Bootstrap.php +++ b/tests/Bootstrap.php @@ -1,27 +1,18 @@ 'tasket' ]; - $this->input = new Sleepy\core\Input(); + $this->input = new Sleepy\Core\Input(); } public function dataTestHeader() @@ -41,7 +39,7 @@ class InputTest extends Sleepy_TestCase { 'all' => [ 'index' => NULL, 'expected' => [ - 'user_agent' => 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.57 Safari/537.36 OPR/18.0.1284.49', + 'user-agent' => 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.57 Safari/537.36 OPR/18.0.1284.49', 'accept' => 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8', 'cookie' => 'thp_tcms_session_id=9h4nvk15tjjegbeg8uvejncc9blkd81m', 'host' => 'www.example.com', @@ -65,7 +63,7 @@ class InputTest extends Sleepy_TestCase { 'all' => [ 'index' => NULL, 'expected' => [ - 'user_agent' => [ + 'user-agent' => [ 'versions' => [ 'Mozilla' => '5.0', 'AppleWebKit' => '537.36', @@ -80,10 +78,10 @@ class InputTest extends Sleepy_TestCase { 'misc' => 'KHTML, like Gecko' ], 'accept' => [ - 'text/html', - 'application/xhtml+xml', - ['application/xml', 'q=0.9'], - ['*/*', 'q=0.8'] + '0.8' => '*/*', + '0.9' => 'application/xml', + 1 => 'application/xhtml+xml', + 2 => 'text/html' ], 'cookie' => [ 'thp_tcms_session_id' => '9h4nvk15tjjegbeg8uvejncc9blkd81m' @@ -185,5 +183,15 @@ class InputTest extends Sleepy_TestCase { $result = $this->input->get($index); $this->assertEquals($expected, $result); } + + public function testBadVar() + { + try { + $foo = $this->input->applesauce(); + } + catch (DomainException $e) { + $this->assertTrue(true); + } + } } // End of InputTest.php \ No newline at end of file diff --git a/tests/Core/OutputTest.php b/tests/Core/OutputTest.php new file mode 100644 index 0000000..a467faa --- /dev/null +++ b/tests/Core/OutputTest.php @@ -0,0 +1,28 @@ +output = new Output($c, $i); + } + + public function testGetAcceptedType() + { + + } + + +} +// End of OutputTest.php \ No newline at end of file diff --git a/tests/Core/aTypeTest.php b/tests/Core/aTypeTest.php index 9a722f8..f505c58 100755 --- a/tests/Core/aTypeTest.php +++ b/tests/Core/aTypeTest.php @@ -1,17 +1,19 @@ assertEquals(['Sleepy\\core\\iType' => 'Sleepy\\core\\iType'], class_implements('Sleepy\\core\\aType')); + $this->assertEquals( + ['Sleepy\\Core\\Interfaces\\Type' => 'Sleepy\\Core\\Interfaces\\Type'], + class_implements('Sleepy\\Core\\Abstracts\\Type') + ); } + + } // End of aTypeTest.php \ No newline at end of file diff --git a/tests/Type/JSONTest.php b/tests/Type/JSONTest.php index d254d20..df29aad 100755 --- a/tests/Type/JSONTest.php +++ b/tests/Type/JSONTest.php @@ -1,41 +1,40 @@ JSON = new JSON([]); + $this->JSON = new JSON(['foo'=>'bar']); } public function testSanity() { - $this->assertTrue(is_a($this->JSON, 'Sleepy\\types\\JSON')); - $this->assertTrue(is_a($this->JSON, 'Sleepy\\core\\aType')); - $this->assertEquals(['Sleepy\\core\\iType' => 'Sleepy\\core\\iType'], class_implements('Sleepy\\types\\JSON')); + $this->assertTrue(is_a($this->JSON, 'Sleepy\\Type\\JSON')); + $this->assertTrue(is_a($this->JSON, 'Sleepy\\Core\\Abstracts\\Type')); + $this->assertEquals( + ['Sleepy\\Core\\Interfaces\\Type' => 'Sleepy\\Core\\Interfaces\\Type'], + class_implements('Sleepy\\Type\\JSON') + ); } - - public function testFunction() - - + public function testNIE() { try { $json = new MockJSON([]); } - catch (Sleepy\exceptions\NotImplementedException $e) { + catch (Sleepy\Exception\NotImplementedException $e) { $this->assertTrue(TRUE); } } - + + public function testGetMime() + { + $mime = $this->JSON->get_mime(); + $this->assertEquals('application/json', $mime); + } } // End of JSONTest \ No newline at end of file diff --git a/tests/Type/XMLTest.php b/tests/Type/XMLTest.php new file mode 100644 index 0000000..3e778b4 --- /dev/null +++ b/tests/Type/XMLTest.php @@ -0,0 +1,62 @@ + 'bar', + 'baz' => [ + 0 => ['x' => 'y'] + ] + ]; + + $this->XML = new XML($test_array); + } + + public function testSanity() { + $this->assertTrue(is_a($this->XML, 'Sleepy\\Type\\XML')); + $this->assertTrue(is_a($this->XML, 'Sleepy\\Core\\Abstracts\\Type')); + $this->assertEquals( + ['Sleepy\\Core\\Interfaces\\Type' => 'Sleepy\\Core\\Interfaces\\Type'], + class_implements('Sleepy\\Type\\XML') + ); + } + + public function testNIE() { + try { + $xml = new MockXML([]); + } + catch (Sleepy\Exception\NotImplementedException $e) { + $this->assertTrue(TRUE); + } + } + + public function testSerialize() + { + $xml = $this->XML->serialize(); + $xml_tostring = $this->XML->__toString(); + $expected = '' . + "\nbary\n"; + + $this->assertEquals($expected, $xml); + $this->assertEquals($xml, $xml_tostring); + } + + public function testUnSerialize() + { + $object = $this->XML->unserialize('' . "\nbar\n"); + + $expected = (object) ['foo' => 'bar']; + + $this->assertEquals($expected, $object); + } + +} +// End of XMLTest \ No newline at end of file diff --git a/tests/phpunit.xml b/tests/phpunit.xml index 9fa8f07..8114254 100755 --- a/tests/phpunit.xml +++ b/tests/phpunit.xml @@ -11,12 +11,16 @@ stopOnSkipped="false"> - ./core - ./exceptions - ./types + Core + Exception + Type + + ../../sleepy/Sleepy + ../../sleepy/application + PEAR_INSTALL_DIR PHP_LIBDIR