Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Igor Chepurnoy committed Jul 4, 2016
1 parent 3d1b6f3 commit 37eb490
Show file tree
Hide file tree
Showing 9 changed files with 211 additions and 1 deletion.
8 changes: 8 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Ignore all test and documentation for archive
/.gitattributes export-ignore
/.gitignore export-ignore
/.scrutinizer.yml export-ignore
/.travis.yml export-ignore
/phpunit.xml.dist export-ignore
/tests export-ignore
/docs export-ignore
35 changes: 35 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# phpstorm project files
.idea

# netbeans project files
nbproject

# zend studio for eclipse project files
.buildpath
.project
.settings

# windows thumbnail cache
Thumbs.db

# composer vendor dir
/vendor

/composer.lock

# composer itself is not needed
composer.phar

# Mac DS_Store Files
.DS_Store

# phpunit itself is not needed
phpunit.phar
# local phpunit config
/phpunit.xml

# local tests configuration
/tests/data/config.local.php

# runtime cache
/tests/runtime
32 changes: 32 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
language: php

php:
- 5.4
- 5.5
- 5.6
- 7.0
- hhvm

# 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

# 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.1.1"
- export PATH="$HOME/.composer/vendor/bin:$PATH"
- travis_retry composer install --prefer-dist --no-interaction

script:
- phpunit --verbose $PHPUNIT_FLAGS
1 change: 0 additions & 1 deletion composer.lock

This file was deleted.

13 changes: 13 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -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="Yii2tech Test Suite">
<directory>./tests</directory>
</testsuite>
</testsuites>
</phpunit>
29 changes: 29 additions & 0 deletions tests/EnumTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace yii2mod\enum\tests;

use yii2mod\enum\tests\data\BooleanEnum;

/**
* Class EnumTest
* @package yii2mod\enum\tests
*/
class EnumTest extends TestCase
{
public function testEnumMethods()
{
$this->assertEquals([1 => 'YES', 0 => 'NO'], BooleanEnum::getConstantsByValue());
$this->assertEquals(['YES' => 1, 'NO' => 0], BooleanEnum::getConstantsByName());
$this->assertEquals([1 => 'Yes', 0 => 'No'], BooleanEnum::listData());
$this->assertEquals('Yes', BooleanEnum::getLabel(1));
$this->assertEquals('1', BooleanEnum::getValueByName('Yes'));
}

public function testValidation()
{
$this->assertFalse(BooleanEnum::isValidName(1));
$this->assertTrue(BooleanEnum::isValidName('YES'));
$this->assertTrue(BooleanEnum::isValidValue(1));
$this->assertFalse(BooleanEnum::isValidValue('YES'));
}
}
54 changes: 54 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

namespace yii2mod\enum\tests;

use yii\helpers\ArrayHelper;
use Yii;

/**
* This is the base class for all yii framework unit tests.
*/
class TestCase extends \PHPUnit_Framework_TestCase
{
protected function setUp()
{
parent::setUp();
$this->mockApplication();
}

protected function 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' => $this->getVendorPath(),
], $config));
}

/**
* @return string vendor path
*/
protected function getVendorPath()
{
return dirname(__DIR__) . '/vendor';
}

/**
* Destroys application in Yii::$app by setting it to null.
*/
protected function destroyApplication()
{
Yii::$app = null;
}
}
13 changes: 13 additions & 0 deletions tests/bootstrap.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?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');
27 changes: 27 additions & 0 deletions tests/data/BooleanEnum.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace yii2mod\enum\tests\data;

use yii2mod\enum\helpers\BaseEnum;

/**
* Class BooleanEnum
* @package yii2mod\enum\tests\data
*/
class BooleanEnum extends BaseEnum
{
const YES = 1;
const NO = 0;

/**
* @var string message category
* You can set your own message category for translate the values in the $list property
* Values in the $list property will be automatically translated in the function `listData()`
*/
public static $messageCategory = 'app';

public static $list = [
self::YES => 'Yes',
self::NO => 'No'
];
}

0 comments on commit 37eb490

Please sign in to comment.