Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tests question #32

Open
wispoz opened this issue Oct 13, 2022 · 1 comment
Open

tests question #32

wispoz opened this issue Oct 13, 2022 · 1 comment

Comments

@wispoz
Copy link

wispoz commented Oct 13, 2022

What steps will reproduce the problem?

I made simple cache (array) and run tests. And its passed. But in my code i dont use ttl.
So in tests case not covered if cache expired.

<?php

namespace Wispoz\ArrayCache;

use Psr\SimpleCache\InvalidArgumentException;

class ArrayCache implements \Psr\SimpleCache\CacheInterface
{
    private const TTL_INFINITY = 0;
    private const TTL_EXPIRED = -1;

    private $cache = [];
    /**
     * @inheritDoc
     */
    public function get(string $key, mixed $default = null): mixed
    {

        $this->validateKey($key);
        if(array_key_exists($key,$this->cache)) {
            return is_object($this->cache[$key]) ? clone $this->cache[$key] :  $this->cache[$key];
        }
        return $default;
    }

    /**
     * @inheritDoc
     */
    public function set(string $key, mixed $value, null|int|\DateInterval $ttl = null): bool
    {
        $ttl = $this->normalizeTtl($ttl);
        $this->validateKey($key);
        if($ttl <= self::TTL_EXPIRED) {
            return $this->delete($key);
        }
        $this->cache[$key]  = is_object($value) ? clone $value : $value;
        return true;
    }

    /**
     * @inheritDoc
     */
    public function delete(string $key): bool
    {
        $this->validateKey($key);
        if(array_key_exists($key,$this->cache)){
            unset($this->cache[$key]);
            return true;
        }

        return false;
    }

    /**
     * @inheritDoc
     */
    public function clear(): bool
    {
        $this->cache = [];

        return true;
    }

    /**
     * @inheritDoc
     */
    public function getMultiple(iterable $keys, mixed $default = null): iterable
    {
        $keys = $this->fromIterable($keys);
        $this->validateKeys($keys);
        $output = [];
        foreach ($keys as $key) {
            $key = (string) $key;
            if(array_key_exists($key,$this->cache)) {
                $output[$key] = $this->cache[$key];
            }else {
                $output[$key] = $default;
            }
        }
        return $output;
    }

    /**
     * @inheritDoc
     */
    public function setMultiple(iterable $values, null|int|\DateInterval $ttl = null): bool
    {
        $values = $this->fromIterable($values);
        foreach ($values as $key=>$value) {
            $this->set($key,$value,$ttl);
        }
        return true;
    }

    /**
     * @inheritDoc
     */
    public function deleteMultiple(iterable $keys): bool
    {
        $keys = $this->fromIterable($keys);
        $this->validateKeys($keys);
        $this->cache = array_filter($this->cache,static function($value,$key) use($keys) {
            $key = (string) $key;
            return !in_array($key,$keys,true);
        },ARRAY_FILTER_USE_BOTH);
        return  true;
    }
    private function normalizeTtl(null|int|string|\DateInterval $ttl): int
    {
        if($ttl ===null) {
            return  self::TTL_INFINITY;
        }

        if($ttl instanceof \DateInterval) {
            return (new \DateTime('@0'))->add($ttl)->getTimestamp();
        }
        return ((int) $ttl) > 0 ? $ttl : -1 ;
    }
    /**
     * @inheritDoc
     */
    public function has(string $key): bool
    {
        return array_key_exists($key,$this->cache);
    }
    private function fromIterable($iterable) :array
    {
        return $iterable instanceof  \Traversable ? iterator_to_array($iterable): (array) $iterable;
    }

    /**
     * @param string $key
     * @return void
     * @throws \Wispoz\ArrayCache\InvalidArgumentException
     */
    private function validateKey(string $key):void
    {
        if($key === '' || strpbrk($key,'/\@:{}[]')) {
            throw new \Wispoz\ArrayCache\InvalidArgumentException();
        }

    }

    /**
     * @param array $keys
     * @return void
     * @throws \Wispoz\ArrayCache\InvalidArgumentException
     */
    private function validateKeys(array $keys): void
    {
        foreach ($keys as $key) {
            $this->validateKey($key);
        }
    }
}

What is the expected result?

Test failed

What do you get instead?

All test passed

Additional info

Q A
Version 1.0.?
PHP version 8.1
Operating system linux
@wispoz
Copy link
Author

wispoz commented Oct 13, 2022

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant