diff --git a/.github/workflows/php83.yml b/.github/workflows/php83.yml index 19ce53ba..b53391c3 100644 --- a/.github/workflows/php83.yml +++ b/.github/workflows/php83.yml @@ -119,6 +119,7 @@ jobs: - name: Benchmarking run: phpbench run tests/webfiori/benchmark --report=default release_staging: + name: Publish Beta needs: - "test" - "coding_standards_check" @@ -134,6 +135,7 @@ jobs: manifest-file: .release-please-manifest.json token: ${{ secrets.GITHUB_TOKEN }} release_prod: + name: Publish Production needs: - "test" - "coding_standards_check" diff --git a/.github/workflows/php84.yml b/.github/workflows/php84.yml new file mode 100644 index 00000000..42ddd1e2 --- /dev/null +++ b/.github/workflows/php84.yml @@ -0,0 +1,122 @@ +name: Build PHP 8.4 + +on: + push: + branches: [ master, dev ] + pull_request: + branches: [ master ] + +jobs: + + test: + runs-on: ${{ matrix.os }} + services: + sql.data: + image: mcr.microsoft.com/mssql/server:2019-latest + env: + SA_PASSWORD: 1234567890@Eu + ACCEPT_EULA: Y + MSSQL_PID: Express + ports: + - "1433:1433" + strategy: + fail-fast: true + matrix: + os: [ ubuntu-latest ] + php: [8.4] + + name: PHP${{matrix.php}} - ${{matrix.os}} + + steps: + - name: Clone Repo + uses: actions/checkout@v3 + + - name: Setup PHP + uses: shivammathur/setup-php@v2 + with: + php-version: ${{ matrix.php }} + extensions: mysqli, mbstring, sqlsrv + tools: phpunit:9.5.20, composer, symplify/easy-coding-standard:12.0.6, phpbench/phpbench:1.2.14 + + - name: Shutdown Ubuntu MySQL + run: sudo service mysql stop + + - name: Set up MySQL + uses: mirromutth/mysql-action@v1.1 + with: + mysql version: '5.7' + mysql database: 'testing_db' + mysql root password: 123456 + mysql user: 'root' + mysql password: 123456 + + - name: Wait for MySQL + run: | + while ! mysqladmin ping --host=127.0.0.1 --password=123456 --silent; do + sleep 1 + done + + - name: Setup MSSQL + run: sqlcmd -S localhost -U SA -P 1234567890@Eu -Q 'create database testing_db' + + - name: Install Dependencies + run: composer install --prefer-dist --no-interaction --no-dev + + - name: Execute Tests + run: phpunit + + - name: CodeCov + uses: codecov/codecov-action@v4 + with: + token: ${{ secrets.CODECOV_TOKEN }} + + coding_standards_check: + name: "Coding Standards Check" + needs: + - "test" + + runs-on: "ubuntu-latest" + steps: + - name: "Set up PHP" + uses: "shivammathur/setup-php@v2" + with: + php-version: "8.1" + extensions: "mbstring" + tools: composer, symplify/easy-coding-standard:12.0.6 + + - name: "Checkout code" + uses: "actions/checkout@v3" + + - name: Install Dependencies + run: composer install --prefer-dist --no-interaction --no-dev + + - name: "Check Style" + run: "ecs check" + + benchmarking: + needs: + - "test" + runs-on: ${{ matrix.os }} + strategy: + fail-fast: true + matrix: + os: [ ubuntu-latest ] + php: [8.0,8.1,8.2,8.3,8.4] + steps: + - name: Clone Repo + uses: actions/checkout@v4 + + - name: Setup PHP + uses: shivammathur/setup-php@v2 + with: + php-version: ${{ matrix.php }} + extensions: mbstring + tools: composer, phpbench/phpbench + + - name: Install Dependencies + run: composer install --prefer-dist --no-interaction --no-dev + + - name: Benchmarking + run: phpbench run tests/webfiori/benchmark --report=default + + diff --git a/README.md b/README.md index cb26e3e5..6272d3c7 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,7 @@

- + @@ -37,7 +37,8 @@ WebFiori Framework is a mini web development framework which is built using PHP || || || -|
| +|| +|
| ## Key Features diff --git a/composer.json b/composer.json index b0708ead..5fb65799 100644 --- a/composer.json +++ b/composer.json @@ -22,7 +22,7 @@ "ext-mbstring": "*", "ext-fileinfo": "*", "ext-openssl": "*", - "webfiori/http": "v3.3.15", + "webfiori/http": "v3.4.0", "webfiori/file": "v1.3.6", "webfiori/jsonx": "v3.3.0", "webfiori/ui": "v2.6.3", diff --git a/phpunit.xml b/phpunit.xml index 0ed2e986..6d4c4976 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -29,7 +29,7 @@ ./webfiori/framework/router/Router.php ./webfiori/framework/cache/AbstractCacheStore.php - ./webfiori/framework/cache/FileCacheStore.php + ./webfiori/framework/cache/FileStorage.php ./webfiori/framework/cache/Cache.php ./webfiori/framework/session/Session.php diff --git a/tests/webfiori/framework/test/cache/CacheTest.php b/tests/webfiori/framework/test/cache/CacheTest.php index f65ef664..1ce12410 100644 --- a/tests/webfiori/framework/test/cache/CacheTest.php +++ b/tests/webfiori/framework/test/cache/CacheTest.php @@ -11,6 +11,7 @@ class CacheTest extends TestCase { */ public function test00() { $key = 'first'; + $this->assertTrue(Cache::isEnabled()); $data = Cache::get($key, function () { return 'This is a test.'; }); @@ -84,4 +85,62 @@ public function test05() { $this->assertFalse(Cache::has($key)); } } + /** + * @test + */ + public function test06() { + $key = 'bbuu'; + $this->assertTrue(Cache::isEnabled()); + Cache::setEnabled(false); + $data = Cache::get($key, function () { + return 'This is a test.'; + }); + $this->assertEquals('This is a test.', $data); + $this->assertNull(Cache::get($key)); + $this->assertFalse(Cache::isEnabled()); + } + /** + * @test + */ + public function testSet00() { + $key = 'new_cool_key'; + Cache::setEnabled(true); + $this->assertTrue(Cache::isEnabled()); + $this->assertTrue(Cache::set($key, 'This is a test.', 60, false)); + $this->assertEquals('This is a test.', Cache::get($key)); + $item = Cache::getItem($key); + $this->assertEquals(60, $item->getTTL()); + + $this->assertFalse(Cache::set($key, 'This is a test.', 60, false)); + $this->assertEquals('This is a test.', Cache::get($key)); + $item = Cache::getItem($key); + $this->assertEquals(60, $item->getTTL()); + + $this->assertTrue(Cache::set($key, 'This is a test 2.', 660, true)); + $this->assertEquals('This is a test 2.', Cache::get($key)); + $item = Cache::getItem($key); + $this->assertEquals(660, $item->getTTL()); + } + /** + * @test + */ + public function testSetTTL00() { + $key = 'new_cool_key2'; + Cache::setEnabled(true); + $this->assertTrue(Cache::isEnabled()); + $this->assertTrue(Cache::set($key, 'This is a test.', 60, false)); + $item = Cache::getItem($key); + $this->assertEquals(60, $item->getTTL()); + Cache::setTTL($key, 700); + + $item = Cache::getItem($key); + $this->assertEquals(700, $item->getTTL()); + } + /** + * @test + */ + public function testSetTTL01() { + $key = 'not exist cool'; + $this->assertFalse(Cache::setTTL($key, 700)); + } } diff --git a/webfiori/framework/cache/Cache.php b/webfiori/framework/cache/Cache.php index f5d1bf86..5075685e 100644 --- a/webfiori/framework/cache/Cache.php +++ b/webfiori/framework/cache/Cache.php @@ -19,6 +19,7 @@ class Cache { * @var Storage */ private $driver; + private $isEnabled; private static $inst; /** * Removes an item from the cache given its unique identifier. @@ -28,6 +29,15 @@ class Cache { public static function delete(string $key) { self::getDriver()->delete($key); } + /** + * Enable or disable caching. + * + * @param bool $enable If set to true, caching will be enabled. Other than + * that, caching will be disabled. + */ + public static function setEnabled(bool $enable) { + self::getInst()->isEnabled = $enable; + } /** * Removes all items from the cache. */ @@ -61,8 +71,11 @@ public static function get(string $key, callable $generator = null, int $ttl = 6 return null; } $newData = call_user_func_array($generator, $params); - $item = new Item($key, $newData, $ttl, defined('CACHE_SECRET') ? CACHE_SECRET : ''); - self::getDriver()->cache($item); + + if (self::isEnabled()) { + $item = new Item($key, $newData, $ttl, defined('CACHE_SECRET') ? CACHE_SECRET : ''); + self::getDriver()->cache($item); + } return $newData; } @@ -122,10 +135,20 @@ public static function set(string $key, $data, int $ttl = 60, bool $override = f if (!self::has($key) || $override === true) { $item = new Item($key, $data, $ttl, defined('CACHE_SECRET') ? CACHE_SECRET : ''); self::getDriver()->cache($item); + + return true; } return false; } + /** + * Checks if caching is enabled or not. + * + * @return bool True if enabled. False otherwise. + */ + public static function isEnabled() : bool { + return self::getInst()->isEnabled; + } /** * Sets storage engine which is used to store, read, update and delete items * from the cache. @@ -165,6 +188,7 @@ private static function getInst() : Cache { if (self::$inst === null) { self::$inst = new Cache(); self::setDriver(new FileStorage()); + self::setEnabled(true); } return self::$inst;