Skip to content

Commit

Permalink
feat: add trait
Browse files Browse the repository at this point in the history
  • Loading branch information
brokeyourbike committed Oct 19, 2021
1 parent 0c5c687 commit 0e8789d
Show file tree
Hide file tree
Showing 14 changed files with 367 additions and 0 deletions.
15 changes: 15 additions & 0 deletions .editorconfig
@@ -0,0 +1,15 @@
root = true

[*]
charset = utf-8
end_of_line = lf
insert_final_newline = true
indent_style = space
indent_size = 4
trim_trailing_whitespace = true

[*.md]
trim_trailing_whitespace = false

[*.{yml,yaml}]
indent_size = 2
11 changes: 11 additions & 0 deletions .gitattributes
@@ -0,0 +1,11 @@
# Auto detect text files and perform LF normalization
* text=auto

.gitattributes export-ignore
.github export-ignore
.gitignore export-ignore
.github/ export-ignore
tests/ export-ignore
phpunit.xml export-ignore
phpstan.neon export-ignore
psalm.xml export-ignore
36 changes: 36 additions & 0 deletions .github/workflows/ci.yml
@@ -0,0 +1,36 @@
name: ci

on:
push:
branches: [ main ]
pull_request:
branches: [ main ]

jobs:
laravel-tests:
runs-on: ubuntu-latest
timeout-minutes: 5
strategy:
matrix:
php-version: [7.4,8.0]

steps:
- name: Checkout code
uses: actions/checkout@v2

- name: Install PHP
uses: shivammathur/setup-php@v2
with:
php-version: "${{ matrix.php-version }}"
coverage: "pcov"

- name: Install PHP dependencies
run: composer install -q --no-ansi --no-interaction --no-scripts --no-progress --prefer-dist

- name: Run PHPUnit
run: vendor/bin/phpunit --coverage-clover=coverage.xml

- name: Upload coverage
uses: codecov/codecov-action@v2
with:
files: ./coverage.xml
18 changes: 18 additions & 0 deletions .github/workflows/release-please.yml
@@ -0,0 +1,18 @@
name: release-please

on:
push:
branches:
- main

jobs:
update_release_draft:
runs-on: ubuntu-latest
timeout-minutes: 5

steps:
- uses: google-github-actions/release-please-action@v2
with:
release-type: php
- uses: actions/checkout@v2
if: ${{ steps.release.outputs.release_created }}
66 changes: 66 additions & 0 deletions .github/workflows/static.yml
@@ -0,0 +1,66 @@
name: static analysis

on:
push:
branches: [ main ]
pull_request:
branches: [ main ]

jobs:
phpstan:
name: PHPStan
runs-on: ubuntu-latest
timeout-minutes: 5
strategy:
matrix:
php-version: [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-version }}"
coverage: none
extensions: mbstring
tools: composer

- name: Download dependencies
run: composer update --no-interaction --no-progress

- name: Download PHPStan
run: composer bin phpstan require phpstan/phpstan

- name: Execute PHPStan
run: vendor/bin/phpstan analyze --no-progress

psalm:
name: Psalm
runs-on: ubuntu-latest
timeout-minutes: 5
strategy:
matrix:
php-version: [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-version }}"
coverage: none
extensions: mbstring
tools: composer

- name: Download dependencies
run: composer update --no-interaction --no-progress

- name: Download Psalm
run: composer bin psalm require vimeo/psalm

- name: Execute Psalm
run: vendor/bin/psalm --no-progress --shepherd --output-format=github
25 changes: 25 additions & 0 deletions .gitignore
@@ -0,0 +1,25 @@
/vendor/
/vendor-bin/
node_modules/
npm-debug.log
yarn-error.log

# Laravel 4 specific
bootstrap/compiled.php
app/storage/

# Laravel 5 & Lumen specific
public/storage
public/hot

# Laravel 5 & Lumen specific with changed public path
public_html/storage
public_html/hot

storage/*.key
.env
Homestead.yaml
Homestead.json
/.vagrant
.phpunit.result.cache
composer.lock
38 changes: 38 additions & 0 deletions README.md
@@ -1,2 +1,40 @@
# resolve-uri

[![Latest Stable Version](https://img.shields.io/github/v/release/brokeyourbike/resolve-uri)](https://github.com/brokeyourbike/resolve-uri/releases)
[![Total Downloads](https://poser.pugx.org/brokeyourbike/resolve-uri/downloads)](https://packagist.org/packages/brokeyourbike/resolve-uri)
[![License: MPL-2.0](https://img.shields.io/badge/license-MPL--2.0-purple.svg)](https://github.com/brokeyourbike/resolve-uri/blob/main/LICENSE)

[![ci](https://github.com/brokeyourbike/resolve-uri/actions/workflows/ci.yml/badge.svg)](https://github.com/brokeyourbike/resolve-uri/actions/workflows/ci.yml)
[![codecov](https://codecov.io/gh/brokeyourbike/resolve-uri/branch/main/graph/badge.svg?token=ImcgnxzGfc)](https://codecov.io/gh/brokeyourbike/resolve-uri)
[![Type Coverage](https://shepherd.dev/github/brokeyourbike/resolve-uri/coverage.svg)](https://shepherd.dev/github/brokeyourbike/resolve-uri)

Trait for resolving URI

## Installation

```bash
composer require brokeyourbike/resolve-uri
```

## Usage

```php
use BrokeYourBike\ResolveUri\ResolveUriTrait;

class APIClient
{
use ResolveUriTrait;

public function fetchProduct(string $productId)
{
$uri = $this->fetchUriFor('https://example.com', "products/{$productId}");
}
}
```

## Why

Resolve URI when `base_url` is not defined in HTTP client.

## License
[Mozilla Public License v2.0](https://github.com/brokeyourbike/resolve-uri/blob/main/LICENSE)
33 changes: 33 additions & 0 deletions composer.json
@@ -0,0 +1,33 @@
{
"name": "brokeyourbike/resolve-uri",
"description": "Trait for resolving URI",
"type": "library",
"license": "MPL-2.0",
"autoload": {
"psr-4": {
"BrokeYourBike\\ResolveUri\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"BrokeYourBike\\Tests\\ResolveUri\\": "tests/"
}
},
"authors": [
{
"name": "Ivan Stasiuk",
"email": "brokeyourbike@gmail.com",
"homepage": "https://github.com/brokeyourbike"
}
],
"minimum-stability": "stable",
"require": {
"php": "^7.4|^8.0",
"psr/http-message": "^1.0",
"guzzlehttp/psr7": "^2"
},
"require-dev": {
"bamarni/composer-bin-plugin": "^1.4",
"phpunit/phpunit": "^9.5"
}
}
5 changes: 5 additions & 0 deletions phpstan.neon
@@ -0,0 +1,5 @@
parameters:
checkMissingIterableValueType: false
level: max
paths:
- src
19 changes: 19 additions & 0 deletions phpunit.xml
@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="utf-8"?>
<phpunit
colors="true"
beStrictAboutOutputDuringTests="true"
beStrictAboutTestsThatDoNotTestAnything="true"
bootstrap="vendor/autoload.php"
>
<testsuites>
<testsuite name="HTTP Client Test Suite">
<directory suffix=".php">./tests</directory>
</testsuite>
</testsuites>

<coverage>
<include>
<directory suffix=".php">src</directory>
</include>
</coverage>
</phpunit>
16 changes: 16 additions & 0 deletions psalm.xml
@@ -0,0 +1,16 @@
<?xml version="1.0"?>
<psalm
totallyTyped="true"
resolveFromConfigFile="false"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="https://getpsalm.org/schema/config"
xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd"
>
<projectFiles>
<directory name="src" />
<ignoreFiles>
<directory name="vendor" />
<directory name="vendor-bin" />
</ignoreFiles>
</projectFiles>
</psalm>
29 changes: 29 additions & 0 deletions src/ResolveUriTrait.php
@@ -0,0 +1,29 @@
<?php

// Copyright (C) 2021 Ivan Stasiuk <brokeyourbike@gmail.com>.
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
// You can obtain one at https://mozilla.org/MPL/2.0/.

namespace BrokeYourBike\ResolveUri;

use GuzzleHttp\Psr7\Utils;
use GuzzleHttp\Psr7\UriResolver;

/**
* @author Ivan Stasiuk <brokeyourbike@gmail.com>
*/
trait ResolveUriTrait
{
/**
* @return \Psr\Http\Message\UriInterface
*/
public function resolveUriFor(string $baseUri, string $uri): \Psr\Http\Message\UriInterface
{
$baseUri = Utils::uriFor($baseUri);
$uri = Utils::uriFor($uri);

return UriResolver::resolve($baseUri, $uri);
}
}
19 changes: 19 additions & 0 deletions tests/ApiClientFixture.php
@@ -0,0 +1,19 @@
<?php

// Copyright (C) 2021 Ivan Stasiuk <brokeyourbike@gmail.com>.
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
// You can obtain one at https://mozilla.org/MPL/2.0/.

namespace BrokeYourBike\Tests\ResolveUri;

use BrokeYourBike\ResolveUri\ResolveUriTrait;

/**
* @author Ivan Stasiuk <brokeyourbike@gmail.com>
*/
class ApiClientFixture
{
use ResolveUriTrait;
}
37 changes: 37 additions & 0 deletions tests/ResolveUriTest.php
@@ -0,0 +1,37 @@
<?php

// Copyright (C) 2021 Ivan Stasiuk <brokeyourbike@gmail.com>.
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
// You can obtain one at https://mozilla.org/MPL/2.0/.

namespace BrokeYourBike\Tests\ResolveUri;

use PHPUnit\Framework\TestCase;
use BrokeYourBike\ResolveUri\ResolveUriTrait;

/**
* @author Ivan Stasiuk <brokeyourbike@gmail.com>
*/
class ResolveUriTest extends TestCase
{
/** @test */
public function it_uses_json_request_result_trait(): void
{
$usedTraits = class_uses(ApiClientFixture::class);

$this->assertArrayHasKey(ResolveUriTrait::class, $usedTraits);
}

/** @test */
public function it_can_resolve_uri()
{
$apiClient = new ApiClientFixture();

$uri = $apiClient->resolveUriFor('https://example.com', 'hello-world');

$this->assertInstanceOf(\Psr\Http\Message\UriInterface::class, $uri);
$this->assertSame('https://example.com/hello-world', (string) $uri);
}
}

0 comments on commit 0e8789d

Please sign in to comment.