Permalink
Browse files

Unit tests added

  • Loading branch information...
1 parent c430e99 commit 16ff3853e992a40266b8b9154f99107ca9597531 @klimov-paul klimov-paul committed Jun 2, 2015
Showing with 222 additions and 0 deletions.
  1. +48 −0 .travis.yml
  2. +13 −0 phpunit.xml.dist
  3. +66 −0 tests/ModuleTest.php
  4. +80 −0 tests/TestCase.php
  5. +15 −0 tests/bootstrap.php
View
@@ -0,0 +1,48 @@
+language: php
+
+php:
+ - 5.4
+ - 5.5
+ - 5.6
+ - 7.0
+ - hhvm
+ - hhvm-nightly
+
+# run build against hhvm but allow them to fail
+# http://docs.travis-ci.com/user/build-configuration/#Rows-That-are-Allowed-To-Fail
+matrix:
+ fast_finish: true
+ allow_failures:
+ - php: hhvm-nightly
+ - php: 7.0
+
+# faster builds on new travis setup not using sudo
+sudo: false
+
+# cache vendor dirs
+cache:
+ directories:
+ - $HOME/.composer/cache
+
+install:
+ - travis_retry composer self-update && composer --version
+ - travis_retry composer global require "fxp/composer-asset-plugin:1.0.0"
+ - export PATH="$HOME/.composer/vendor/bin:$PATH"
+ - travis_retry composer install --prefer-dist --no-interaction
+ - composer show -i
+
+before_script:
+ - |
+ if [ $TRAVIS_PHP_VERSION = '5.6' ]; then
+ PHPUNIT_FLAGS="--coverage-clover=coverage.clover"
+ fi
+
+script:
+ - phpunit --verbose $PHPUNIT_FLAGS
+
+after_script:
+ - |
+ if [ $TRAVIS_PHP_VERSION = '5.6' ]; then
+ travis_retry wget https://scrutinizer-ci.com/ocular.phar
+ php ocular.phar code-coverage:upload --format=php-clover coverage.clover
+ fi
View
@@ -0,0 +1,13 @@
+<?xml version="1.0" encoding="utf-8"?>
+<phpunit bootstrap="./tests/bootstrap.php"
+ colors="true"
+ convertErrorsToExceptions="true"
+ convertNoticesToExceptions="true"
+ convertWarningsToExceptions="true"
+ stopOnFailure="false">
+ <testsuites>
+ <testsuite name="Test Suite">
+ <directory>./tests</directory>
+ </testsuite>
+ </testsuites>
+</phpunit>
View
@@ -0,0 +1,66 @@
+<?php
+
+namespace yiiunit\extensions\debug;
+
+use yii\debug\Module;
+
+class ModuleTest extends TestCase
+{
+ protected function setUp()
+ {
+ parent::setUp();
+ $this->mockWebApplication();
+ }
+
+ // Tests :
+
+ /**
+ * Data provider for [[testCheckAccess()]]
+ * @return array test data
+ */
+ public function dataProviderCheckAccess()
+ {
+ return [
+ [
+ [],
+ '10.20.30.40',
+ false
+ ],
+ [
+ ['10.20.30.40'],
+ '10.20.30.40',
+ true
+ ],
+ [
+ ['*'],
+ '10.20.30.40',
+ true
+ ],
+ [
+ ['10.20.30.*'],
+ '10.20.30.40',
+ true
+ ],
+ [
+ ['10.20.30.*'],
+ '10.20.40.40',
+ false
+ ],
+ ];
+ }
+
+ /**
+ * @dataProvider dataProviderCheckAccess
+ *
+ * @param array $allowedIPs
+ * @param string $userIp
+ * @param boolean $expectedResult
+ */
+ public function testCheckAccess(array $allowedIPs, $userIp, $expectedResult)
+ {
+ $module = new Module('debug');
+ $module->allowedIPs = $allowedIPs;
+ $_SERVER['REMOTE_ADDR'] = $userIp;
+ $this->assertEquals($expectedResult, $this->invoke($module, 'checkAccess'));
+ }
+}
View
@@ -0,0 +1,80 @@
+<?php
+
+namespace yiiunit\extensions\debug;
+
+use yii\di\Container;
+use yii\helpers\ArrayHelper;
+use Yii;
+
+/**
+ * This is the base class for all yii framework unit tests.
+ */
+abstract class TestCase extends \PHPUnit_Framework_TestCase
+{
+ /**
+ * Clean up after test.
+ * By default the application created with [[mockApplication]] will be destroyed.
+ */
+ protected function tearDown()
+ {
+ parent::tearDown();
+ $this->destroyApplication();
+ }
+
+ /**
+ * Populates Yii::$app with a new application
+ * The application will be destroyed on tearDown() automatically.
+ * @param array $config The application configuration, if needed
+ * @param string $appClass name of the application class to create
+ */
+ protected function mockApplication($config = [], $appClass = '\yii\console\Application')
+ {
+ new $appClass(ArrayHelper::merge([
+ 'id' => 'testapp',
+ 'basePath' => __DIR__,
+ 'vendorPath' => dirname(__DIR__) . '/vendor',
+ ], $config));
+ }
+
+ protected function mockWebApplication($config = [], $appClass = '\yii\web\Application')
+ {
+ new $appClass(ArrayHelper::merge([
+ 'id' => 'testapp',
+ 'basePath' => __DIR__,
+ 'vendorPath' => dirname(__DIR__) . '/vendor',
+ 'components' => [
+ 'request' => [
+ 'cookieValidationKey' => 'wefJDF8sfdsfSDefwqdxj9oq',
+ 'scriptFile' => __DIR__ .'/index.php',
+ 'scriptUrl' => '/index.php',
+ ],
+ ]
+ ], $config));
+ }
+
+ /**
+ * Destroys application in Yii::$app by setting it to null.
+ */
+ protected function destroyApplication()
+ {
+ Yii::$app = null;
+ Yii::$container = new Container();
+ }
+
+ /**
+ * Invokes object method, even if it is private or protected.
+ * @param object $object object.
+ * @param string $method method name.
+ * @param array $args method arguments
+ * @return mixed method result
+ */
+ protected function invoke($object, $method, array $args = [])
+ {
+ $classReflection = new \ReflectionClass(get_class($object));
+ $methodReflection = $classReflection->getMethod($method);
+ $methodReflection->setAccessible(true);
+ $result = $methodReflection->invokeArgs($object, $args);
+ $methodReflection->setAccessible(false);
+ return $result;
+ }
+}
View
@@ -0,0 +1,15 @@
+<?php
+
+// ensure we get report on all possible php errors
+error_reporting(-1);
+
+define('YII_ENABLE_ERROR_HANDLER', false);
+define('YII_DEBUG', true);
+$_SERVER['SCRIPT_NAME'] = '/' . __DIR__;
+$_SERVER['SCRIPT_FILENAME'] = __FILE__;
+
+require_once(__DIR__ . '/../vendor/autoload.php');
+require_once(__DIR__ . '/../vendor/yiisoft/yii2/Yii.php');
+
+Yii::setAlias('@yiiunit/extensions/debug', __DIR__);
+Yii::setAlias('@yii/debug', dirname(__DIR__));

0 comments on commit 16ff385

Please sign in to comment.