Skip to content

Commit 6c3f69e

Browse files
Удалены лишние функции, добавлена среда окружения и тесты
1 parent 1f59ad8 commit 6c3f69e

21 files changed

+345
-33
lines changed

.github/workflows/php.yml

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
name: build
2+
3+
on:
4+
push:
5+
branches: [ stable, development ]
6+
pull_request:
7+
branches: [ stable, development ]
8+
9+
jobs:
10+
build:
11+
runs-on: ubuntu-latest
12+
continue-on-error: ${{ matrix.experimental }}
13+
strategy:
14+
max-parallel: 2
15+
fail-fast: false
16+
matrix:
17+
php: [7.4, 8.0]
18+
experimental: [false]
19+
20+
steps:
21+
- uses: actions/checkout@v2
22+
23+
- name: Setup PHP Action
24+
uses: shivammathur/[email protected]
25+
with:
26+
php-version: ${{ matrix.php }}
27+
coverage: xdebug
28+
29+
- name: Validate composer.json and composer.lock
30+
run: composer validate --strict
31+
32+
- name: Cache Composer packages
33+
id: composer-cache
34+
uses: actions/cache@v2
35+
with:
36+
path: vendor
37+
key: ${{ runner.os }}-php-${{ hashFiles('**/composer.lock') }}
38+
restore-keys: |
39+
${{ runner.os }}-php-
40+
41+
- name: Install dependencies with Composer
42+
uses: ramsey/composer-install@v1
43+
44+
- name: Run unit tests
45+
run: php vendor/bin/codecept run unit
46+
47+
- name: Run Codecept coverage
48+
run: php vendor/bin/codecept run --coverage --coverage-xml --coverage-phpunit
49+
50+
- name: Download artifacts
51+
uses: actions/download-artifact@v2
52+
53+
- name: Codecov
54+
uses: codecov/[email protected]
55+
with:
56+
token: ${{ secrets.CODECOV_TOKEN }}
57+
directory: ./tests/_output
58+
files: ./tests/_output/coverage.xml
59+
flags: unittests
60+
verbose: true
61+
fail_ci_if_error: true

README.md

+10-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
# php-headers
22

3+
![release](https://img.shields.io/github/v/release/mepihindeveloper/php-headers?label=version)
4+
[![Packagist Version](https://img.shields.io/packagist/v/mepihindeveloper/php-headers)](https://packagist.org/packages/mepihindeveloper/php-headers)
5+
[![PHP Version Require](http://poser.pugx.org/mepihindeveloper/php-headers/require/php)](https://packagist.org/packages/mepihindeveloper/php-headers)
6+
![license](https://img.shields.io/github/license/mepihindeveloper/php-headers)
7+
8+
![build](https://github.com/mepihindeveloper/php-headers/actions/workflows/php.yml/badge.svg?branch=stable)
9+
[![codecov](https://codecov.io/gh/mepihindeveloper/php-headers/branch/stable/graph/badge.svg?token=36PP7VKHKG)](https://codecov.io/gh/mepihindeveloper/php-headers)
10+
11+
312
Компонент для работы с заголовками в PHP
413

514
# Структура
@@ -19,13 +28,13 @@ src/
1928

2029
| Метод | Аргументы | Возвращаемые данные | Исключения | Описание |
2130
|---------------------|-----------------------------|---------------------|--------------------------|-----------------------------------------------------------------------------|
22-
| set(array $params) | Заголовок(и) [key => value] | void | | Устанавливает заголовок(и) |
2331
| add(array $params) | Заголовок(и) [key => value] | void | | Добавляет заголовок. Если заголовок уже существует, то он будет перезаписан |
2432
| remove(string $key) | Заголовок | void | | Удаляет заголовок |
2533
| removeAll | | void | | Удаляет все заголовки |
2634
| has(string $key) | Заголовок | bool | | Проверяет наличие заголовка. Проверка идет на наличие ключа и значения |
2735
| get(string $key) | Заголовок | string | InvalidArgumentException | Получает значение заголовка |
2836
| getAll | | | | Получает все заголовки |
37+
| getIsApache | | bool | | Возвращает, является ли сервер Apache |
2938

3039
# Контакты
3140

codeception.yml

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
paths:
2+
tests: tests
3+
output: tests/_output
4+
data: tests/_data
5+
support: tests/_support
6+
envs: tests/_envs
7+
actor_suffix: Tester
8+
extensions:
9+
enabled:
10+
- Codeception\Extension\RunFailed
11+
coverage:
12+
enabled: true
13+
include:
14+
- src/*
15+
exclude:
16+
- src/interfaces/*

composer.json

+6-1
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,10 @@
2424
}
2525
},
2626
"minimum-stability": "dev",
27-
"prefer-stable": true
27+
"prefer-stable": true,
28+
"require-dev": {
29+
"codeception/codeception": "^4.1",
30+
"codeception/module-phpbrowser": "^1.0.0",
31+
"codeception/module-asserts": "^1.0.0"
32+
}
2833
}

src/Headers.php

+19-22
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,38 @@
1414
*/
1515
class Headers implements HeadersInterface {
1616

17+
public const APACHE = 'Apache';
18+
1719
/**
1820
* @var array Заголовки
1921
*/
2022
private array $headers;
23+
/**
24+
* @var bool Является ли сервер Apache
25+
*/
26+
private bool $isApache;
2127

2228
public function __construct() {
29+
$this->isApache = array_key_exists('SERVER_SOFTWARE', $_SERVER) && $_SERVER['SERVER_SOFTWARE'] === self::APACHE;
2330
$this->headers = $this->getAllHeaders();
2431
}
2532

33+
/**
34+
* Возвращает, является ли сервер Apache
35+
*
36+
* @return bool
37+
*/
38+
public function getIsApache():bool {
39+
return $this->isApache;
40+
}
41+
2642
/**
2743
* Получает все заголовки методами apache и nginx
2844
*
2945
* @return array
3046
*/
31-
private function getAllHeaders(): array {
32-
if (function_exists('getallheaders')) {
47+
public function getAllHeaders(): array {
48+
if ($this->isApache && function_exists('getallheaders')) {
3349
return getallheaders() !== false ? getallheaders() : [];
3450
}
3551

@@ -48,25 +64,10 @@ private function getAllHeaders(): array {
4864
return $headers;
4965
}
5066

51-
/**
52-
* @inheritDoc
53-
*/
54-
public function set(array $params): void {
55-
$this->getAll();
56-
57-
foreach ($params as $header => $value) {
58-
$this->headers[$header] = $value;
59-
}
60-
61-
$this->add($params);
62-
}
63-
6467
/**
6568
* @inheritDoc
6669
*/
6770
public function getAll(): array {
68-
$this->headers = !empty($this->headers) ? $this->headers : $this->getAllHeaders();
69-
7071
return $this->headers;
7172
}
7273

@@ -78,16 +79,14 @@ public function add(array $params): void {
7879
$headerExists = array_key_exists($header, $this->headers);
7980
$this->headers[$header] = $value;
8081

81-
header("{$header}: {$value}", $headerExists);
82+
header("$header: $value", $headerExists);
8283
}
8384
}
8485

8586
/**
8687
* @inheritDoc
8788
*/
8889
public function remove(string $key): void {
89-
$this->getAll();
90-
9190
unset($this->headers[$key]);
9291
header_remove($key);
9392
}
@@ -116,8 +115,6 @@ public function get(string $key): string {
116115
* @inheritDoc
117116
*/
118117
public function has(string $key): bool {
119-
$this->getAll();
120-
121118
return array_key_exists($key, $this->headers);
122119
}
123120
}

src/interfaces/HeadersInterface.php

-9
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,6 @@
1313
*/
1414
interface HeadersInterface {
1515

16-
/**
17-
* Устанавливает заголовок(и)
18-
*
19-
* @param array $params Заголовок(и) [key => value]
20-
*
21-
* @return void
22-
*/
23-
public function set(array $params): void;
24-
2516
/**
2617
* Добавляет заголовок. Если заголовок уже существует, то он будет перезаписан.
2718
*

tests/_data/.gitkeep

Whitespace-only changes.

tests/_output/.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
*
2+
!.gitignore

tests/_support/AcceptanceTester.php

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
4+
/**
5+
* Inherited Methods
6+
* @method void wantToTest($text)
7+
* @method void wantTo($text)
8+
* @method void execute($callable)
9+
* @method void expectTo($prediction)
10+
* @method void expect($prediction)
11+
* @method void amGoingTo($argumentation)
12+
* @method void am($role)
13+
* @method void lookForwardTo($achieveValue)
14+
* @method void comment($description)
15+
* @method void pause()
16+
*
17+
* @SuppressWarnings(PHPMD)
18+
*/
19+
class AcceptanceTester extends \Codeception\Actor
20+
{
21+
use _generated\AcceptanceTesterActions;
22+
23+
/**
24+
* Define custom actions here
25+
*/
26+
}

tests/_support/FunctionalTester.php

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
4+
/**
5+
* Inherited Methods
6+
* @method void wantToTest($text)
7+
* @method void wantTo($text)
8+
* @method void execute($callable)
9+
* @method void expectTo($prediction)
10+
* @method void expect($prediction)
11+
* @method void amGoingTo($argumentation)
12+
* @method void am($role)
13+
* @method void lookForwardTo($achieveValue)
14+
* @method void comment($description)
15+
* @method void pause()
16+
*
17+
* @SuppressWarnings(PHPMD)
18+
*/
19+
class FunctionalTester extends \Codeception\Actor
20+
{
21+
use _generated\FunctionalTesterActions;
22+
23+
/**
24+
* Define custom actions here
25+
*/
26+
}

tests/_support/Helper/Acceptance.php

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
namespace Helper;
3+
4+
// here you can define custom actions
5+
// all public methods declared in helper class will be available in $I
6+
7+
class Acceptance extends \Codeception\Module
8+
{
9+
10+
}

tests/_support/Helper/Functional.php

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
namespace Helper;
3+
4+
// here you can define custom actions
5+
// all public methods declared in helper class will be available in $I
6+
7+
class Functional extends \Codeception\Module
8+
{
9+
10+
}

tests/_support/Helper/Unit.php

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
namespace Helper;
3+
4+
// here you can define custom actions
5+
// all public methods declared in helper class will be available in $I
6+
7+
class Unit extends \Codeception\Module
8+
{
9+
10+
}

tests/_support/UnitTester.php

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
4+
/**
5+
* Inherited Methods
6+
* @method void wantToTest($text)
7+
* @method void wantTo($text)
8+
* @method void execute($callable)
9+
* @method void expectTo($prediction)
10+
* @method void expect($prediction)
11+
* @method void amGoingTo($argumentation)
12+
* @method void am($role)
13+
* @method void lookForwardTo($achieveValue)
14+
* @method void comment($description)
15+
* @method void pause()
16+
*
17+
* @SuppressWarnings(PHPMD)
18+
*/
19+
class UnitTester extends \Codeception\Actor
20+
{
21+
use _generated\UnitTesterActions;
22+
23+
/**
24+
* Define custom actions here
25+
*/
26+
}

tests/_support/_generated/.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
*
2+
!.gitignore

tests/acceptance.suite.yml

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Codeception Test Suite Configuration
2+
#
3+
# Suite for acceptance tests.
4+
# Perform tests in browser using the WebDriver or PhpBrowser.
5+
# If you need both WebDriver and PHPBrowser tests - create a separate suite.
6+
7+
actor: AcceptanceTester
8+
modules:
9+
enabled:
10+
- PhpBrowser:
11+
url: http://localhost/myapp
12+
- \Helper\Acceptance
13+
step_decorators: ~

tests/acceptance/.gitkeep

Whitespace-only changes.

tests/functional.suite.yml

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Codeception Test Suite Configuration
2+
#
3+
# Suite for functional tests
4+
# Emulate web requests and make application process them
5+
# Include one of framework modules (Symfony2, Yii2, Laravel5, Phalcon4) to use it
6+
# Remove this suite if you don't use frameworks
7+
8+
actor: FunctionalTester
9+
modules:
10+
enabled:
11+
# add a framework module here
12+
- \Helper\Functional
13+
step_decorators: ~

tests/functional/.gitkeep

Whitespace-only changes.

tests/unit.suite.yml

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Codeception Test Suite Configuration
2+
#
3+
# Suite for unit or integration tests.
4+
5+
actor: UnitTester
6+
modules:
7+
enabled:
8+
- Asserts
9+
- \Helper\Unit
10+
step_decorators: ~

0 commit comments

Comments
 (0)