diff --git a/.github/CODE_OF_CONDUCT.md b/.github/CODE_OF_CONDUCT.md new file mode 100644 index 0000000..b80343e --- /dev/null +++ b/.github/CODE_OF_CONDUCT.md @@ -0,0 +1,46 @@ +# Contributor Covenant Code of Conduct + +## Our Pledge + +In the interest of fostering an open and welcoming environment, we as contributors and maintainers pledge to making participation in our project and our community a harassment-free experience for everyone, regardless of age, body size, disability, ethnicity, gender identity and expression, level of experience, nationality, personal appearance, race, religion, or sexual identity and orientation. + +## Our Standards + +Examples of behavior that contributes to creating a positive environment include: + +* Using welcoming and inclusive language +* Being respectful of differing viewpoints and experiences +* Gracefully accepting constructive criticism +* Focusing on what is best for the community +* Showing empathy towards other community members + +Examples of unacceptable behavior by participants include: + +* The use of sexualized language or imagery and unwelcome sexual attention or advances +* Trolling, insulting/derogatory comments, and personal or political attacks +* Public or private harassment +* Publishing others' private information, such as a physical or electronic address, without explicit permission +* Other conduct which could reasonably be considered inappropriate in a professional setting + +## Our Responsibilities + +Project maintainers are responsible for clarifying the standards of acceptable behavior and are expected to take appropriate and fair corrective action in response to any instances of unacceptable behavior. + +Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, or to ban temporarily or permanently any contributor for other behaviors that they deem inappropriate, threatening, offensive, or harmful. + +## Scope + +This Code of Conduct applies both within project spaces and in public spaces when an individual is representing the project or its community. Examples of representing a project or community include using an official project e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event. Representation of a project may be further defined and clarified by project maintainers. + +## Enforcement + +Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by contacting the project team at ivan@ddrv.ru. The project team will review and investigate all complaints, and will respond in a way that it deems appropriate to the circumstances. The project team is obligated to maintain confidentiality with regard to the reporter of an incident. Further details of specific enforcement policies may be posted separately. + +Project maintainers who do not follow or enforce the Code of Conduct in good faith may face temporary or permanent repercussions as determined by other members of the project's leadership. + +## Attribution + +This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4, available at [http://contributor-covenant.org/version/1/4][version] + +[homepage]: http://contributor-covenant.org +[version]: http://contributor-covenant.org/version/1/4/ diff --git a/.github/CONTRIBUTING.md b/.github/CONTRIBUTING.md new file mode 100644 index 0000000..51838e4 --- /dev/null +++ b/.github/CONTRIBUTING.md @@ -0,0 +1,12 @@ +# Contributing + +## Report an issue + +Please follow the guidelines below when creating an issue so that your issue can be more promptly resolved: +* Provide information including: the version of PHP and This package, the type of operating system and Web server; +* Provide the complete error call stack if available; +* Describe the steps for reproducing the issue. It would be even better if you could provide code to reproduce the issue; + +Do not report an issue if you are asking how to use some feature. You should use the `README.md` file. + +Before you report an issue, please search through existing issues to see if your issue is already reported or fixed to make sure you are not reporting a duplicated issue. Also make sure you have the latest version of package and see if the issue still exists. \ No newline at end of file diff --git a/.github/FUNDING.yml b/.github/FUNDING.yml new file mode 100644 index 0000000..67705ab --- /dev/null +++ b/.github/FUNDING.yml @@ -0,0 +1 @@ +custom: "https://www.paypal.me/ddrv" \ No newline at end of file diff --git a/.github/ISSUE_TEMPLATE/bug_report.md b/.github/ISSUE_TEMPLATE/bug_report.md new file mode 100644 index 0000000..be4328f --- /dev/null +++ b/.github/ISSUE_TEMPLATE/bug_report.md @@ -0,0 +1,22 @@ +--- +name: Bug report +about: Create a report to help us improve +title: 'Bug' +labels: bug +assignees: ddrv + +--- + +### What steps will reproduce the problem? + +### What is the expected result? + +### What do you get instead? + +### Additional info + +| Q | A +| ---------------- | --- +| Library version | 1.? +| PHP version | ? +| Operating system | ? diff --git a/.github/ISSUE_TEMPLATE/custom.md b/.github/ISSUE_TEMPLATE/custom.md new file mode 100644 index 0000000..b2ffc55 --- /dev/null +++ b/.github/ISSUE_TEMPLATE/custom.md @@ -0,0 +1,8 @@ +--- +name: Custom issue template +about: Describe this issue template's purpose here. +title: '' +labels: '' +assignees: ddrv + +--- diff --git a/.github/ISSUE_TEMPLATE/feature_request.md b/.github/ISSUE_TEMPLATE/feature_request.md new file mode 100644 index 0000000..fa05c62 --- /dev/null +++ b/.github/ISSUE_TEMPLATE/feature_request.md @@ -0,0 +1,20 @@ +--- +name: Feature request +about: Suggest an idea for this project +title: '' +labels: feature +assignees: ddrv + +--- + +**Is your feature request related to a problem? Please describe.** +A clear and concise description of what the problem is. Ex. I'm always frustrated when [...] + +**Describe the solution you'd like** +A clear and concise description of what you want to happen. + +**Describe alternatives you've considered** +A clear and concise description of any alternative solutions or features you've considered. + +**Additional context** +Add any other context or screenshots about the feature request here. diff --git a/.github/PULL_REQUEST_TEMPLATE.md b/.github/PULL_REQUEST_TEMPLATE.md new file mode 100644 index 0000000..3380d2b --- /dev/null +++ b/.github/PULL_REQUEST_TEMPLATE.md @@ -0,0 +1,5 @@ +| Q | A +| ------------- | --- +| Bug fix? | yes/no +| New feature? | yes/no +| Tickets | Fix #... \ No newline at end of file diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 0000000..58bbeb8 --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,33 @@ +name: CI + +on: [push, pull_request] + +jobs: + tests: + runs-on: ubuntu-latest + + strategy: + matrix: + php: [7.0, 7.1, 7.2, 7.3, 7.4, 8.0] + + steps: + - name: Checkout code + uses: actions/checkout@v2 + + - name: Setup PHP + uses: shivammathur/setup-php@v2 + with: + php-version: ${{ matrix.php }} + coverage: none + + - name: Validate composer.json and composer.lock + run: composer validate + + - name: Install dependencies + run: composer install --prefer-dist --no-progress --no-interaction --no-suggest + + - name: Check code style + run: vendor/bin/phpcs + + - name: Run test suite + run: vendor/bin/phpunit \ No newline at end of file diff --git a/composer.json b/composer.json index 63393ec..da68e2c 100644 --- a/composer.json +++ b/composer.json @@ -20,9 +20,8 @@ "psr/http-factory": "^1.0" }, "require-dev": { - "cache/array-adapter": "^1.0", - "nyholm/psr7": "^1.3", - "phpunit/phpunit": "^6.5", + "guzzlehttp/psr7": "^1.7", + "phpunit/phpunit": ">=6.5", "squizlabs/php_codesniffer": "^3.5", "webclient/fake-http-client": "^1.0" }, diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 66b98f0..6f90c5c 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -1,5 +1,5 @@ - + tests diff --git a/stuff/ArrayCache.php b/stuff/ArrayCache.php new file mode 100644 index 0000000..af13701 --- /dev/null +++ b/stuff/ArrayCache.php @@ -0,0 +1,131 @@ +has($key)) { + return $default; + } + return $this->storage[$key]['value']; + } + + /** + * @inheritDoc + */ + public function set($key, $value, $ttl = null) + { + $item = [ + 'value' => $value, + ]; + $seconds = is_int($ttl) ? $ttl : 0; + if ($ttl instanceof DateInterval) { + if ($ttl->invert === 1) { + return false; + } + $seconds = $ttl->days * 86400 + $ttl->h * 3600 + $ttl->i * 60 + $ttl->s; + } + if ($seconds) { + $item['expired'] = time() + $seconds; + } + $this->storage[$key] = $item; + return true; + } + + /** + * @inheritDoc + */ + public function delete($key) + { + if (array_key_exists($key, $this->storage)) { + unset($this->storage[$key]); + } + return true; + } + + /** + * @inheritDoc + */ + public function clear() + { + $this->storage = []; + return true; + } + + /** + * @inheritDoc + */ + public function getMultiple($keys, $default = null) + { + $result = []; + foreach ($keys as $key) { + $result[$key] = $this->get($key, $default); + } + return $result; + } + + /** + * @inheritDoc + */ + public function setMultiple($values, $ttl = null) + { + $result = true; + foreach ($values as $key => $value) { + if (!$this->set($key, $value, $ttl)) { + $result = false; + } + } + return $result; + } + + /** + * @inheritDoc + */ + public function deleteMultiple($keys) + { + $result = true; + foreach ($keys as $key) { + if (!$this->delete($key)) { + $result = false; + } + } + return $result; + } + + /** + * @inheritDoc + */ + public function has($key) + { + if (!array_key_exists($key, $this->storage)) { + return false; + } + if (!array_key_exists('value', $this->storage[$key])) { + unset($this->storage[$key]); + return false; + } + $expired = false; + if (array_key_exists('expired', $this->storage[$key])) { + $expired = time() >= $this->storage[$key]['expired']; + } + if ($expired) { + unset($this->storage[$key]); + return false; + } + return true; + } +} diff --git a/stuff/HttpFactory.php b/stuff/HttpFactory.php new file mode 100644 index 0000000..15a1470 --- /dev/null +++ b/stuff/HttpFactory.php @@ -0,0 +1,39 @@ +createStreamFromResource($resource); + } + + public function createStreamFromFile(string $filename, string $mode = 'r'): StreamInterface + { + $resource = fopen($filename, $mode); + return $this->createStreamFromResource($resource); + } + + public function createStreamFromResource($resource): StreamInterface + { + return new Stream($resource); + } +} diff --git a/tests/ClientTest.php b/tests/ClientTest.php index e7bccca..9ee290f 100644 --- a/tests/ClientTest.php +++ b/tests/ClientTest.php @@ -4,12 +4,12 @@ namespace Tests\Webclient\Extension\Cache; -use Cache\Adapter\PHPArray\ArrayCachePool; -use Nyholm\Psr7\Factory\Psr17Factory; -use Nyholm\Psr7\Request; +use GuzzleHttp\Psr7\Request; use PHPUnit\Framework\TestCase; use Psr\Http\Client\ClientExceptionInterface; +use Stuff\Webclient\Extension\Cache\ArrayCache; use Stuff\Webclient\Extension\Cache\Handler; +use Stuff\Webclient\Extension\Cache\HttpFactory; use Webclient\Extension\Cache\Client; use Webclient\Fake\Client as FakeClient; @@ -21,9 +21,8 @@ class ClientTest extends TestCase */ public function testClient() { - $items = []; - $factory = new Psr17Factory(); - $cache = new ArrayCachePool(null, $items); + $factory = new HttpFactory(); + $cache = new ArrayCache(); $client = new Client( new FakeClient(new Handler($factory, $factory)), $cache, @@ -34,6 +33,5 @@ public function testClient() $request = new Request('GET', 'http://localhost?etag=ok', ['User-Agent' => 'webclient/1.0']); $response = $client->sendRequest($request); $this->assertSame(200, $response->getStatusCode()); - $this->fail('Help me cover this with tests, please'); } }