Commit c2036dfb authored by Ad Schellevis's avatar Ad Schellevis

(mvc) setup initial framework unit tests

parent d8e91d1e
<?xml version="1.0" encoding="UTF-8"?>
<phpunit bootstrap="./setup.php"
backupGlobals="false"
backupStaticAttributes="false"
verbose="true"
colors="false"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
syntaxCheck="true">
<groups>
<exclude>
<group>Ignore</group>
</exclude>
</groups>
<testsuite name="OPNsense - Testsuite">
<directory suffix="Test.php">./app</directory>
</testsuite>
</phpunit>
\ No newline at end of file
<?php
return new \Phalcon\Config(array(
'application' => array(
'controllersDir' => __DIR__ . '/../../../app/controllers/',
'modelsDir' => __DIR__ . '/../../../app/models/',
'viewsDir' => __DIR__ . '/../../../app/views/',
'pluginsDir' => __DIR__ . '/../../../app/plugins/',
'libraryDir' => __DIR__ . '/../../../app/library/',
'cacheDir' => __DIR__ . '/../../../app/cache/',
'baseUri' => '/opnsense_gui/',
),
'globals' => array(
'config_path' => '/conf/',
'temp_path' => '/tmp/',
'debug' => false,
'simulate_mode' => false
)
));
<?php
/**
* Copyright (C) 2016 Deciso B.V.
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
* AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
* OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
*/
namespace tests\OPNsense\Base\BaseModel;
use OPNsense\Base\BaseModel;
/**
* Class TestModel
*/
class TestModel extends BaseModel
{
}
<model>
<mount>//tests/OPNsense/TestModel</mount>
<description>
OPNsense RecursiveModel test
</description>
<items>
<!-- container -->
<general>
<!-- fields -->
<FromEmail type="EmailField">
<default>sample@example.com</default>
<Required>Y</Required>
</FromEmail>
</general>
</items>
</model>
<?php
/**
* Copyright (C) 2016 Deciso B.V.
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
* AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
* OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
*/
namespace tests\OPNsense\Base;
require_once 'BaseModel/TestModel.php';
class BaseModelTest extends \PHPUnit_Framework_TestCase
{
private static $model = null;
public function testCanBeCreated()
{
BaseModelTest::$model = new BaseModel\TestModel();
$this->assertInstanceOf('tests\OPNsense\Base\BaseModel\TestModel', BaseModelTest::$model);
}
/**
* @depends testCanBeCreated
*/
public function testGeneralAvailable()
{
$this->assertNotNull(BaseModelTest::$model->general);
}
/**
* @depends testGeneralAvailable
*/
public function testCanSetStringValue()
{
BaseModelTest::$model->general->FromEmail = "test@test.nl";
$this->assertEquals(BaseModelTest::$model->general->FromEmail, "test@test.nl");
}
/**
* @expectedException InvalidArgumentException
* @expectedExceptionMessage FromEmailXXX not an attribute of general
* @depends testGeneralAvailable
*/
public function testCannotSetNonExistingField()
{
BaseModelTest::$model->general->FromEmailXXX = "test@test.nl";
}
}
<?php
/**
* Copyright (C) 2016 Deciso B.V.
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
* AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
* OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
*/
namespace tests\OPNsense\Base\FieldTypes;
/**
* Class Field_Framework_TestCase
* @package tests\OPNsense\Base\FieldTypes
*/
class Field_Framework_TestCase extends \PHPUnit_Framework_TestCase
{
/**
* Validate and throw exception
* @param $field feld type
* @throws \Phalcon\Validation\Exception
*/
public function validateThrow($field)
{
$validation = new \Phalcon\Validation();
foreach ($field->getValidators() as $validator) {
$validation->add("testfield", $validator);
}
$messages = $validation->validate(array("testfield" => (string)$field));
if (count($messages)) {
foreach ($messages as $message) {
throw new \Phalcon\Validation\Exception($message->getType());
}
}
return;
}
/**
* Validate and return exceptions
* @param $field feld type
* @return array
*/
public function validate($field)
{
$result = array();
$validation = new \Phalcon\Validation();
foreach ($field->getValidators() as $validator) {
$validation->add("testfield", $validator);
}
$messages = $validation->validate(array("testfield" => (string)$field));
if (count($messages)) {
foreach ($messages as $message) {
$result[] = $message->getType();
}
}
return $result;
}
/**
* do not test
* @group Ignore
*/
public function testIgnore()
{
return;
}
}
\ No newline at end of file
<?php
/**
* Copyright (C) 2016 Deciso B.V.
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
* AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
* OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
*/
namespace tests\OPNsense\Base\FieldTypes;
// @CodingStandardsIgnoreStart
require_once 'Field_Framework_TestCase.php';
// @CodingStandardsIgnoreEnd
use \OPNsense\Base\FieldTypes\IntegerField;
class IntegerFieldTest extends Field_Framework_TestCase
{
/**
* test construct
*/
public function testCanBeCreated()
{
$this->assertInstanceOf('\OPNsense\Base\FieldTypes\IntegerField', new IntegerField());
}
/**
* @expectedException \Phalcon\Validation\Exception
* @expectedExceptionMessage MaxMinValidator
*/
public function testValueLargerThenMax()
{
$field = new IntegerField();
$field->setMaximumValue(100);
$field->setMinimumValue(10);
$field->setValue("120");
$this->validateThrow($field);
}
/**
* @expectedException \Phalcon\Validation\Exception
* @expectedExceptionMessage MaxMinValidator
*/
public function testValueSmallerThenMin()
{
$field = new IntegerField();
$field->setMaximumValue(100);
$field->setMinimumValue(10);
$field->setValue("5");
$this->validateThrow($field);
}
/**
* not a number
*/
public function testNotANumber()
{
$field = new IntegerField();
$field->setMaximumValue(100);
$field->setMinimumValue(10);
$field->setValue("5x1");
$this->assertContains('IntegerValidator', $this->validate($field));
}
}
<?php
/**
* Copyright (C) 2016 Deciso B.V.
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
* AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
* OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
*/
use Phalcon\Di;
use Phalcon\Di\FactoryDefault;
ini_set('display_errors', 1);
error_reporting(E_ALL);
/**
* Read the configuration
*/
$config = include __DIR__ . "/app/config/config.php";
/**
* Read auto-loader
*/
include __DIR__ . "/../app/config/loader.php";
$di = new FactoryDefault();
Di::reset();
$di->set('config', $config);
Di::setDefault($di);
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment