Skip to content

Commit

Permalink
Add test should not pass null key
Browse files Browse the repository at this point in the history
  • Loading branch information
HillLiu committed May 22, 2022
1 parent b3e6640 commit 1c68c33
Show file tree
Hide file tree
Showing 9 changed files with 123 additions and 58 deletions.
51 changes: 51 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
@@ -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"]
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
vendor
composer.lock
.php_cs.cache
.phpunit.result.cache
.*.sw?
16 changes: 0 additions & 16 deletions .travis.yml

This file was deleted.

2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -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)

Expand Down
33 changes: 18 additions & 15 deletions composer.json
Original file line number Diff line number Diff line change
@@ -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"
}
15 changes: 9 additions & 6 deletions get.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
}
}
16 changes: 16 additions & 0 deletions src/_unexpected_key_exception.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace PMVC\PlugIn\get;

use UnexpectedValueException;

${_INIT_CONFIG}[_CLASS] = __NAMESPACE__.'\UnexpectedKeyException';

class UnexpectedKeyException
{
public function __invoke()
{
$message = '[PlugIn:get] should not pass key with null';
throw new UnexpectedValueException($message);
}
}
21 changes: 7 additions & 14 deletions tests/include.php
Original file line number Diff line number Diff line change
@@ -1,18 +1,11 @@
<?php

$path = __DIR__.'/../vendor/autoload.php';
$path = __DIR__ . '/../vendor/autoload.php';
include $path;

if (!class_exists('PHPUnit_Framework_TestCase')) {
class PHPUnit_Framework_TestCase extends
\PHPUnit\Framework\TestCase
{
}
class PHPUnit_Framework_Error extends
\PHPUnit\Framework\Error\Notice
{
}
}

\PMVC\Load::plug();
\PMVC\addPlugInFolders([__DIR__.'/../../']);
\PMVC\Load::plug(
[
'unit' => null,
],
[__DIR__ . '/../../']
);
26 changes: 20 additions & 6 deletions tests/test.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

0 comments on commit 1c68c33

Please sign in to comment.