From 1c68c33f00bc74a7317ed0e0fe56ad4d6162bb0a Mon Sep 17 00:00:00 2001 From: Hill Date: Sun, 22 May 2022 13:17:08 +0000 Subject: [PATCH] Add test should not pass null key --- .circleci/config.yml | 51 +++++++++++++++++++++++++++++++ .gitignore | 1 + .travis.yml | 16 ---------- README.md | 2 +- composer.json | 33 +++++++++++--------- get.php | 15 +++++---- src/_unexpected_key_exception.php | 16 ++++++++++ tests/include.php | 21 +++++-------- tests/test.php | 26 ++++++++++++---- 9 files changed, 123 insertions(+), 58 deletions(-) create mode 100644 .circleci/config.yml delete mode 100644 .travis.yml create mode 100644 src/_unexpected_key_exception.php diff --git a/.circleci/config.yml b/.circleci/config.yml new file mode 100644 index 0000000..f9a78a6 --- /dev/null +++ b/.circleci/config.yml @@ -0,0 +1,51 @@ +version: 2.1 + +jobs: + unittest: + parameters: + php-version: + type: string + plugin-name: + type: string + default: "get" + docker: + - image: hillliu/pmvc-phpunit:<< parameters.php-version >> + working_directory: /var/www/<< parameters.plugin-name >> + steps: + - checkout + - run: + name: "Display information" + command: | + date + php -v + php -m + php -r "if(function_exists('gd_info'))print_r(gd_info());" + composer --version + phpunit --version + - run: + name: Composer install packages + command: | + composer update + composer install --prefer-source + - run: + name: PHPUnit + command: | + ENABLE_COVERAGE=false + if [ "<< parameters.php-version >>" == "8.1" ] && [ "$ENABLE_COVERAGE" == "true" ]; then + XDEBUG_MODE=coverage phpunit --coverage-clover clover.xml + coveralls --coverage_clover=clover.xml -v -o coveralls-upload.json + else + phpunit + fi + - store_artifacts: + path: /var/www/<< parameters.plugin-name >>/clover.xml + - store_artifacts: + path: /var/www/<< parameters.plugin-name >>/coveralls-upload.json + +workflows: + run-job: + jobs: + - unittest: + matrix: + parameters: + php-version: ["8.1", "8.0", "5.6"] diff --git a/.gitignore b/.gitignore index 400294a..721877b 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ vendor composer.lock .php_cs.cache +.phpunit.result.cache .*.sw? diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index fd6a0a3..0000000 --- a/.travis.yml +++ /dev/null @@ -1,16 +0,0 @@ -language: php -dist: trusty -sudo: required -group: edge - -php: - - 5.6 - - 7.1 - -before_script: - - composer self-update - - composer install --prefer-source - - composer require phpunit/phpunit 4.8.35 - -script: - - vendor/bin/phpunit diff --git a/README.md b/README.md index 2412f11..f8a2dcb 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ [![Latest Stable Version](https://poser.pugx.org/pmvc-plugin/get/v/stable)](https://packagist.org/packages/pmvc-plugin/get) [![Latest Unstable Version](https://poser.pugx.org/pmvc-plugin/get/v/unstable)](https://packagist.org/packages/pmvc-plugin/get) -[![Build Status](https://travis-ci.org/pmvc-plugin/get.svg?branch=master)](https://travis-ci.org/pmvc-plugin/get) +[![CircleCI](https://circleci.com/gh/pmvc-plugin/get/tree/master.svg?style=svg)](https://circleci.com/gh/pmvc-plugin/get/tree/master) [![License](https://poser.pugx.org/pmvc-plugin/get/license)](https://packagist.org/packages/pmvc-plugin/get) [![Total Downloads](https://poser.pugx.org/pmvc-plugin/get/downloads)](https://packagist.org/packages/pmvc-plugin/get) diff --git a/composer.json b/composer.json index a14c978..0eada70 100644 --- a/composer.json +++ b/composer.json @@ -1,17 +1,20 @@ { - "name": "pmvc-plugin/get", - "description": "smart get function, help you get configure from different storage simply. ", - "type": "library", - "keywords": ["get", "pmvc", "plug-in"], - "license": "MIT", - "authors": [ - { - "name": "Hill", - "email": "hill@kimo.com" - } - ], - "require": { - "pmvc/pmvc": "*" - }, - "minimum-stability": "dev" + "name": "pmvc-plugin/get", + "description": "smart get function, help you get configure from different storage simply. ", + "type": "library", + "keywords": ["get", "pmvc", "plug-in"], + "license": "MIT", + "authors": [ + { + "name": "Hill", + "email": "hill@kimo.com" + } + ], + "require": { + "pmvc/pmvc": "*" + }, + "require-dev": { + "pmvc-plugin/unit": "*" + }, + "minimum-stability": "dev" } diff --git a/get.php b/get.php index 9be3d13..5cbac96 100755 --- a/get.php +++ b/get.php @@ -9,15 +9,18 @@ /** * Put this outside class, so we don't need init it in very begin */ -\PMVC\l(__DIR__.'/src/GetInterface.php'); +\PMVC\l(__DIR__ . '/src/GetInterface.php'); -${_INIT_CONFIG}[_CLASS] = __NAMESPACE__.'\get'; +${_INIT_CONFIG}[_CLASS] = __NAMESPACE__ . '\get'; class get extends PlugIn { public function get($k, $default = null) { - $result =& \PMVC\getOption($k); + if (is_null($k)) { + $this->unexpected_key_exception(); + } + $result = &\PMVC\getOption($k); if (is_null($result)) { foreach ($this[GET_ORDER] as $get) { $plug = \PMVC\plug($get); @@ -42,15 +45,15 @@ public function get($k, $default = null) public function init() { if (!is_array($this[GET_ORDER])) { - $this->unexpected_order_value_exception($this[GET_ORDER]); + $this->unexpected_order_value_exception($this[GET_ORDER]); } } public function offsetSet($k, $v) { if ($k === GET_ORDER && !is_array($v)) { - $this->unexpected_order_value_exception($v); - } + $this->unexpected_order_value_exception($v); + } return parent::offsetSet($k, $v); } } diff --git a/src/_unexpected_key_exception.php b/src/_unexpected_key_exception.php new file mode 100644 index 0000000..2191ce0 --- /dev/null +++ b/src/_unexpected_key_exception.php @@ -0,0 +1,16 @@ + null, + ], + [__DIR__ . '/../../'] +); diff --git a/tests/test.php b/tests/test.php index ae14a58..b3e3ba6 100644 --- a/tests/test.php +++ b/tests/test.php @@ -2,17 +2,31 @@ namespace PMVC\PlugIn\get; -use PHPUnit_Framework_TestCase; +use PMVC\TestCase; -class GetTest extends PHPUnit_Framework_TestCase +const emptyOrder = ['order' => []]; + +class GetTest extends TestCase { - private $_plug='get'; - function testPlugin() + private $_plug = 'get'; + public function testPlugin() { ob_start(); - print_r(\PMVC\plug($this->_plug, ['order'=>[]])); + print_r(\PMVC\plug($this->_plug, emptyOrder)); $output = ob_get_contents(); ob_end_clean(); - $this->assertContains($this->_plug,$output); + $this->haveString($this->_plug, $output); + } + + /** + * @expectedException UnexpectedValueException + * @expectedExceptionMessage should not pass key with null + */ + public function testGetNullKey() + { + $get = \PMVC\plug($this->_plug, emptyOrder); + $this->willThrow(function () use ($get) { + $get->get(null); + }, false); } }