tests: unwind BaseModel test side effect on AclConfig

Give the test its own empty config to migrate into, which makes
resetting it irrelevant.  A tiny bit of magic is needed to bootstrap
a config.xml so it's not the one that is checked in and we also
remove the temporary files from the tree so it does not confuse git.

save() is a far-reaching side effect in the test, but more or less
necessary for better test coverage.  But I'd still want a simpler
test around Core\Config and this offers merely offers the groundwork
also removing the need to reset this from make commands.
This commit is contained in:
Franco Fichtner 2025-12-05 10:04:08 +01:00
parent 28ea98a303
commit fdf52dce8e
4 changed files with 47 additions and 42 deletions

View file

@ -363,19 +363,13 @@ migrate:
validate:
@${PLUGINCTL} -v
# XXX we should stop treating AclConfig dir as the test's actual /conf dir
TEST_NO_CLOBBER= ${TESTDIR}/app/models/OPNsense/ACL/AclConfig/config.xml
test:
.if exists(${TESTDIR})
@if [ "$$(${VERSIONBIN} -v)" != "${CORE_PKGVERSION}" ]; then \
echo "Installed version does not match, expected ${CORE_PKGVERSION}"; \
exit 1; \
fi
@cd ${TESTDIR} && cp ${TEST_NO_CLOBBER} ${TEST_NO_CLOBBER}.save && \
phpunit || true; rm -rf ${TESTDIR}/.phpunit.result.cache \
${TESTDIR}/app/models/OPNsense/ACL/AclConfig/backup; \
mv ${TEST_NO_CLOBBER}.save ${TEST_NO_CLOBBER}
@cd ${TESTDIR} && phpunit || true; rm -rf ${TESTDIR}/.phpunit.result.cache
.endif
clean: clean-pkgdir clean-wrksrc clean-mfcdir checkout

1
plist
View file

@ -1036,6 +1036,7 @@
/usr/local/opnsense/mvc/tests/app/models/OPNsense/Base/BaseModel/Migrations/M1_0_1.php
/usr/local/opnsense/mvc/tests/app/models/OPNsense/Base/BaseModel/TestModel.php
/usr/local/opnsense/mvc/tests/app/models/OPNsense/Base/BaseModel/TestModel.xml
/usr/local/opnsense/mvc/tests/app/models/OPNsense/Base/BaseModelConfig/backup/config.xml
/usr/local/opnsense/mvc/tests/app/models/OPNsense/Base/BaseModelTest.php
/usr/local/opnsense/mvc/tests/app/models/OPNsense/Base/Constraints/ComparedToFieldConstraintTest.php
/usr/local/opnsense/mvc/tests/app/models/OPNsense/Base/Constraints/UniqueConstraintTest.php

View file

@ -0,0 +1,8 @@
<?xml version="1.0"?>
<opnsense>
<revision>
<username>(system)</username>
<description>/usr/local/bin/phpunit made changes</description>
<time>1764925143.48</time>
</revision>
</opnsense>

View file

@ -1,33 +1,34 @@
<?php
/**
* Copyright (C) 2016 Deciso B.V.
* All rights reserved.
/*
* Copyright (C) 2016-2025 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:
* 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.
* 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.
* 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.
* 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;
use OPNsense\Core\AppConfig;
use OPNsense\Core\Config;
require_once 'BaseModel/TestModel.php';
@ -36,26 +37,19 @@ class BaseModelTest extends \PHPUnit\Framework\TestCase
{
private static $model = null;
public function testResetConfig()
public static function cleanupTestFiles()
{
// reset version, force migrations
if (
!empty(Config::getInstance()->object()->tests) &&
!empty(Config::getInstance()->object()->tests->OPNsense) &&
!empty(Config::getInstance()->object()->tests->OPNsense->TestModel)
) {
Config::getInstance()->object()->tests->OPNsense->TestModel['version'] = '0.0.0';
Config::getInstance()->object()->tests->OPNsense->TestModel->general->FromEmail = "sample@example.com";
}
BaseModelTest::$model = new BaseModel\TestModel();
$this->assertEquals((string)BaseModelTest::$model->general->FromEmail, 'sample@example.com');
array_map('unlink', glob(__DIR__ . '/BaseModelConfig/backup/config-*.xml'));
@unlink(__DIR__ . '/BaseModelConfig/config.xml');
}
/**
* @depends testResetConfig
*/
public function testCanBeCreated()
{
BaseModelTest::cleanupTestFiles();
(new AppConfig())->update('application.configDir', __DIR__ . '/BaseModelConfig');
Config::getInstance()->forceReload();
BaseModelTest::$model = new BaseModel\TestModel();
$this->assertInstanceOf('tests\OPNsense\Base\BaseModel\TestModel', BaseModelTest::$model);
}
@ -411,4 +405,12 @@ class BaseModelTest extends \PHPUnit\Framework\TestCase
"xxx"
);
}
/**
* @afterClass
*/
public static function postCleanup()
{
BaseModelTest::cleanupTestFiles();
}
}