Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions .github/CODE_OF_CONDUCT.md
Original file line number Diff line number Diff line change
@@ -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/
12 changes: 12 additions & 0 deletions .github/CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -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.
1 change: 1 addition & 0 deletions .github/FUNDING.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
custom: "https://www.paypal.me/ddrv"
22 changes: 22 additions & 0 deletions .github/ISSUE_TEMPLATE/bug_report.md
Original file line number Diff line number Diff line change
@@ -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 | ?
8 changes: 8 additions & 0 deletions .github/ISSUE_TEMPLATE/custom.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
name: Custom issue template
about: Describe this issue template's purpose here.
title: ''
labels: ''
assignees: ddrv

---
20 changes: 20 additions & 0 deletions .github/ISSUE_TEMPLATE/feature_request.md
Original file line number Diff line number Diff line change
@@ -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.
5 changes: 5 additions & 0 deletions .github/PULL_REQUEST_TEMPLATE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
| Q | A
| ------------- | ---
| Bug fix? | yes/no
| New feature? | yes/no
| Tickets | Fix #...
33 changes: 33 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -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
5 changes: 2 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down
2 changes: 1 addition & 1 deletion phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit bootstrap="vendor/autoload.php" colors="true">
<phpunit bootstrap="vendor/autoload.php" colors="true" cacheResult="false">
<testsuites>
<testsuite name="all">
<directory>tests</directory>
Expand Down
131 changes: 131 additions & 0 deletions stuff/ArrayCache.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
<?php

namespace Stuff\Webclient\Extension\Cache;

use DateInterval;
use Psr\SimpleCache\CacheInterface;

class ArrayCache implements CacheInterface
{

/**
* @var array
*/
private $storage = [];

/**
* @inheritDoc
*/
public function get($key, $default = null)
{
if (!$this->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;
}
}
39 changes: 39 additions & 0 deletions stuff/HttpFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

declare(strict_types=1);

namespace Stuff\Webclient\Extension\Cache;

use GuzzleHttp\Psr7\Response;
use GuzzleHttp\Psr7\Stream;
use Psr\Http\Message\ResponseFactoryInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\StreamFactoryInterface;
use Psr\Http\Message\StreamInterface;

class HttpFactory implements ResponseFactoryInterface, StreamFactoryInterface
{
public function createResponse(int $code = 200, string $reasonPhrase = ''): ResponseInterface
{
return new Response($code, [], null, '1.1', $reasonPhrase);
}

public function createStream(string $content = ''): StreamInterface
{
$resource = fopen('php://temp', 'w+');
fwrite($resource, $content);
rewind($resource);
return $this->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);
}
}
12 changes: 5 additions & 7 deletions tests/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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,
Expand All @@ -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');
}
}