1
0
قرینه از https://github.com/matomo-org/matomo.git synced 2025-08-25 00:17:37 +00:00
Files
matomo/tests/PHPUnit/Integration/Settings/Storage/StorageTest.php
Stefan Giehl d6d72d1fa7 [Coding Style] Enable rule PSR1.Methods.CamelCapsMethodName.NotCamelCaps (#22144)
* [Coding Style] Enable rule PSR1.Methods.CamelCapsMethodName.NotCamelCaps

* [Coding Style] Use camel case for method names in API plugin tests (#22145)

* [Coding Style] Use camel case for method names in Core* plugin tests (#22147)

* [Coding Style] Use camel case for method names in core Unit tests (#22149)

* [Coding Style] Use camel case for method names in Actions and BulkTracking plugin tests (#22146)

* [Coding Style] Use camel case for method names in CustomDimensions and CustomJSTracker plugin tests (#22148)

* [Coding Style] Use camel case for method names in core Integration tests (#22151)

* [Coding Style] Use camel case for method names in more core plugin tests (#22153)

* [Coding Style] Use camel case for method names in more core plugin tests (#22157)

* [Coding Style] Use camel case for method names in more core plugin tests

* Update plugins/Monolog/tests/Unit/Processor/ExceptionToTextProcessorTest.php

Co-authored-by: Michal Kleiner <michal@innocraft.com>

---------

Co-authored-by: Michal Kleiner <michal@innocraft.com>

* [Coding Style] Use camel case for method names in more core plugin tests (#22159)

* [Coding Style] Use camel case for method names in remaining tests (#22160)

* [Coding Style] Use camel case for method names in remaining tests

* rename expected test files

---------

Co-authored-by: Michal Kleiner <michal@innocraft.com>
2024-04-25 20:57:55 +02:00

158 خطوط
4.5 KiB
PHP

<?php
/**
* Matomo - free/libre analytics platform
*
* @link https://matomo.org
* @license https://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*/
namespace Piwik\Tests\Integration\Settings\Storage;
use Piwik\Settings\FieldConfig;
use Piwik\Settings\Storage\Backend\BackendInterface;
use Piwik\Settings\Storage\Storage;
use Piwik\Tests\Framework\Mock\Settings\FakeBackend;
use Piwik\Tests\Framework\TestCase\IntegrationTestCase;
use Piwik\Tracker\Cache as TrackerCache;
/**
* @group PluginSettings
* @group Storage
*/
class StorageTest extends IntegrationTestCase
{
/**
* @var Storage
*/
protected $storage;
/**
* @var BackendInterface
*/
protected $backend;
/**
* @var string
*/
protected $settingName = 'myname';
public function setUp(): void
{
parent::setUp();
$this->backend = new FakeBackend('MyTestId');
$this->backend->save(array($this->settingName => 'value1'));
$this->storage = $this->buildStorage();
}
public function testGetBackend()
{
$this->assertSame($this->backend, $this->storage->getBackend());
}
public function testGetValueShouldReturnDefaultValueIfNoValueIsSet()
{
$value = $this->storage->getValue('UnkNownFielD', $default = '123', FieldConfig::TYPE_STRING);
$this->assertSame($default, $value);
}
public function testGetValueShouldReturnDefaultValueAndNotCastDefaultValue()
{
$value = $this->storage->getValue('UnkNownFielD', $default = '123', FieldConfig::TYPE_INT);
$this->assertSame($default, $value);
}
public function testGetValueShouldReturnASavedValueFromBackend()
{
$value = $this->getValueFromStorage($this->settingName);
$this->assertSame('value1', $value);
}
public function testSetValueGetValueShouldSetAndGetActualValue()
{
$this->storage->setValue($this->settingName, 'myRandomVal');
$value = $this->getValueFromStorage($this->settingName);
$this->assertEquals('myRandomVal', $value);
}
public function testSetValueGetValueShouldCastValueWhenGettingTheValue()
{
$this->storage->setValue($this->settingName, '1');
$value = $this->getValueFromStorage($this->settingName, FieldConfig::TYPE_BOOL);
$this->assertTrue($value);
}
public function testSetValueShouldNotSaveItInDatabase()
{
$loaded = $this->backend->load();
$this->storage->setValue($this->settingName, 'myRandomVal');
$this->assertSame($loaded, $this->loadValuesFromBackend());
}
public function testSaveShouldPersistValueInDatabase()
{
$this->storage->setValue($this->settingName, 'myRandomVal');
$this->storage->save();
$this->assertEquals(
array($this->settingName => 'myRandomVal'),
$this->loadValuesFromBackend()
);
}
public function testSaveShouldPersistMultipleValuesContainingInt()
{
$this->storage->setValue($this->settingName, 'myRandomVal');
$this->storage->setValue('mySecondName', 5);
$this->storage->save();
$this->assertEquals(
array($this->settingName => 'myRandomVal', 'mySecondName' => 5),
$this->loadValuesFromBackend()
);
}
public function testSaveShouldNotClearTrackerCacheEntriesIfThereWasNoChange()
{
TrackerCache::setCacheGeneral(array('testSetting' => 1));
$this->assertArrayHasKey('testSetting', TrackerCache::getCacheGeneral());
$this->storage->save();
$this->assertArrayHasKey('testSetting', TrackerCache::getCacheGeneral());
}
public function testSaveShouldClearTrackerCacheEntriesIfThereWasActuallyAChange()
{
TrackerCache::setCacheGeneral(array('testSetting' => 1));
$this->assertArrayHasKey('testSetting', TrackerCache::getCacheGeneral());
$this->storage->setValue('myTest', 5); // it will save only when there was actually a change
$this->storage->save();
$this->assertArrayNotHasKey('testSetting', TrackerCache::getCacheGeneral());
}
private function getValueFromStorage($settingName, $type = null)
{
if (!isset($type)) {
$type = FieldConfig::TYPE_STRING;
}
return $this->storage->getValue($settingName, $default = '', $type);
}
protected function buildStorage()
{
return new Storage($this->backend);
}
protected function loadValuesFromBackend()
{
return $this->backend->load();
}
}