<?php
/**
* SeekQuarry/Yioop --
* Open Source Pure PHP Search Engine, Crawler, and Indexer
*
* Copyright (C) 2009 - 2026 Chris Pollett chris@pollett.org
*
* LICENSE:
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see
* <a href="https://www.gnu.org/licenses/">https://www.gnu.org/licenses/</a>.
*
* END LICENSE
*
* @author Chris Pollett chris@pollett.org
* @license https://www.gnu.org/licenses/ GPL3
* @link https://www.seekquarry.com/
* @copyright 2009 - 2026
* @package seek_quarry\tests
*/
namespace seekquarry\yioop\tests;
use seekquarry\yioop\configs as C;
use seekquarry\yioop\library\UnitTest;
/**
* Checks the p() profile-value accessor: a stored value is returned on the
* next read, a stored zero is kept rather than reseeded, an unset label falls
* back to the boot constant, and an unknown label throws.
*
* @author Chris Pollett
*/
class ProfileConstantTest extends UnitTest
{
/**
* No set up is needed; p() keeps its own process-level cache.
*/
public function setUp()
{
}
/**
* No tear down is needed.
*/
public function tearDown()
{
}
/**
* A value stored through the authorized ProfileModel::updateProfile
* caller comes back on the next read.
*/
public function setThenReadTestCase()
{
(new ProfileModel())->updateProfile('P_TEST_ALPHA', 'hello');
$this->assertEqual(C\p('P_TEST_ALPHA'), 'hello',
"a value stored through updateProfile is returned on read");
}
/**
* A stored zero must be kept. An earlier version keyed on empty() would
* treat the zero as absent and try to reseed from a constant.
*/
public function falsyValueSticksTestCase()
{
(new ProfileModel())->updateProfile('P_TEST_BETA', 0);
$this->assertTrue(C\p('P_TEST_BETA') === 0,
"a stored zero is kept, not treated as an unset label");
}
/**
* A label never stored falls back to the boot constant of that name.
*/
public function seedsFromConstantTestCase()
{
$this->assertEqual(C\p('NAME_LEN'), C\NAME_LEN,
"an unset label seeds from the boot constant of the same name");
}
/**
* Reading a label with no cache entry and no matching constant throws.
*/
public function undefinedThrowsTestCase()
{
$threw = false;
try {
C\p('P_TEST_UNDEFINED_LABEL');
} catch (\Exception $e) {
$threw = true;
}
$this->assertTrue($threw,
"reading a label with neither a cache entry nor a constant throws");
}
/**
* A set attempted from anywhere other than ProfileModel::updateProfile
* is rejected, so the cache changes only when the profile does.
*/
public function unauthorizedSetThrowsTestCase()
{
$threw = false;
try {
C\p('P_TEST_GAMMA', 'nope', true);
} catch (\Exception $e) {
$threw = true;
}
$this->assertTrue($threw,
"a direct set not from updateProfile is rejected");
}
}
/**
* Minimal stand-in whose updateProfile method is the authorized caller the
* p() set guard checks for, letting the set path be tested without writing
* Profile.php or touching the database.
*/
class ProfileModel
{
/**
* Stores a profile value through the p() accessor, matching the caller
* shape (ProfileModel::updateProfile) the guard allows.
*
* @param string $label profile setting name
* @param mixed $value value to store under $label
*/
public function updateProfile($label, $value)
{
C\p($label, $value, true);
}
}