Skip to content

Commit

Permalink
Release 1.0.0: PHP 7.4 compatibility (#1)
Browse files Browse the repository at this point in the history
  • Loading branch information
slepic authored May 10, 2020
1 parent cd4ee94 commit e92d806
Show file tree
Hide file tree
Showing 8 changed files with 81 additions and 94 deletions.
11 changes: 1 addition & 10 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,2 @@
language: php
php:
- '5.6'
- '7.3'

before_script:
- composer self-update
- composer install --no-interaction

script:
- composer test
- make ci
15 changes: 15 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
.phony: install test cs-check cs-fix

install:
docker-compose run --rm php composer install --no-interaction --no-suggest

test:
docker-compose run --rm php vendor/bin/phpunit

cs-check:
docker-compose run --rm php vendor/bin/phpcs --standard=PSR2 src tests

cs-fix:
docker-compose run --rm php vendor/bin/phpcbf --standard=PSR2 src tests

ci: install cs-check test
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ And it also provides one simple implementation called [```OutputBufferTemplate``

## Requirements

PHP 5.6 or 7
PHP 7.4 or 8

## Installation

Expand All @@ -25,6 +25,16 @@ If you create a library that depends on this one and you use composer, please co

## Changelog

### 1.0.0

* bump PHP to ^7.4 || ^8.0
* TemplateInterface::render() now has string return typehint
* OutputBufferTemplate::render throws InvalidArgumentException if data argument contains keys that cannot be used as local variable names
* use squizlabs/php_codesniffer instead of friendsofphp/php-cs-fixer for style check
* bump dev deps to latest versions
* use composer docker image for dev
* move composer scripts to makefile

### 0.2.0

* Added array typehint for first argument of ```TemplateInterface::render()```.
Expand Down
41 changes: 3 additions & 38 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"description": "Simple PHP abstraction of template rendering.",
"type": "library",
"require": {
"php": "^5.6 || ^7.0"
"php": "^7.4 || ^8.0"
},
"autoload": {
"psr-4": {
Expand All @@ -26,42 +26,7 @@
},
"minimum-stability": "stable",
"require-dev": {
"phpunit/phpunit": "~5.0",
"friendsofphp/php-cs-fixer": "^2.14"
},
"scripts": {
"check-cs": [
"vendor/bin/php-cs-fixer fix -vvv --dry-run --ansi --config php-cs-fixer/config.php src",
"vendor/bin/php-cs-fixer fix -vvv --dry-run --ansi --config php-cs-fixer/config.php tests"
],
"check-staged-cs": [
"vendor/bin/php-cs-fixer fix -vvv --dry-run --ansi --config php-cs-fixer/config.php"
],
"diff-cs": [
"vendor/bin/php-cs-fixer fix -vvv --dry-run --diff --ansi --config php-cs-fixer/config.php src",
"vendor/bin/php-cs-fixer fix -vvv --dry-run --diff --ansi --config php-cs-fixer/config.php tests"
],
"diff-staged-cs": [
"vendor/bin/php-cs-fixer fix -vvv --dry-run --diff --ansi --config php-cs-fixer/config.php"
],
"fix-cs": [
"vendor/bin/php-cs-fixer fix -vvv --ansi --config php-cs-fixer/config.php src",
"vendor/bin/php-cs-fixer fix -vvv --ansi --config php-cs-fixer/config.php tests"
],
"fix-json": [
"mv composer.json composer.json.bck",
"cat composer.json.bck | python -mjson.tool > composer.json",
"diff composer.json.bck composer.json -u && rm composer.json.bck"
],
"fix-staged-cs": [
"vendor/bin/php-cs-fixer fix -vvv --ansi --config php-cs-fixer/config.php"
],
"pre-commit": [
"composer check-staged-cs",
"composer test"
],
"test": [
"vendor/bin/phpunit --colors=always"
]
"phpunit/phpunit": "^9.1",
"squizlabs/php_codesniffer": "^3.5"
}
}
9 changes: 9 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
version: '3.5'

services:
php:
image: composer:latest
container_name: php
volumes:
- .:/app:delegated
working_dir: /app
31 changes: 10 additions & 21 deletions src/OutputBufferTemplate.php
Original file line number Diff line number Diff line change
@@ -1,39 +1,28 @@
<?php

declare(strict_types=1);

namespace Slepic\Templating\Template;

/**
* Class OutputBufferTemplate
* @package Slepic\Templating
*
* An implementation of TemplateInterface using native PHP output buffering and a plain PHP script with echo calls.
*/
class OutputBufferTemplate implements TemplateInterface
{
/**
* @var string
*/
private $templateFile;
private string $templateFile;

/**
* OutputBufferTemplate constructor.
* @param string $templateFile
*/
public function __construct($templateFile)
public function __construct(string $templateFile)
{
if (!\is_string($templateFile)) {
throw new \InvalidArgumentException('Template file must be a string.');
}
$this->templateFile = $templateFile;
}

/**
* @param array $data
* @return string
*/
public function render(array $data)
public function render(array $data): string
{
\extract($data);
if (\count($data) !== \extract($data)) {
throw new \InvalidArgumentException(
'Expected associative array where keys are valid variable names.'
);
}
\ob_start();
require($this->templateFile);
return \ob_get_clean();
Expand Down
7 changes: 3 additions & 4 deletions src/TemplateInterface.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
<?php

declare(strict_types=1);

namespace Slepic\Templating\Template;

/**
* Interface TemplateInterface
* @package Slepic\Templating
*
* Represents a template for rendering certain data.
*/
interface TemplateInterface
Expand All @@ -16,5 +15,5 @@ interface TemplateInterface
* @param array $data
* @return string
*/
public function render(array $data);
public function render(array $data): string;
}
49 changes: 29 additions & 20 deletions tests/OutputBufferTemplateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,49 +13,58 @@ class OutputBufferTemplateTest extends TestCase
*
* @return void
*/
public function testImplements()
public function testImplements(): void
{
$template = new OutputBufferTemplate(__DIR__ . '/test_ok.phtml');
$this->assertInstanceOf(TemplateInterface::class, $template);
}


/**
* Tests that test_ok.phtml template is rendered properly.
*
* @param mixed $data
* @param array $data
* @return void
* @dataProvider provideRenderData
*/
public function testRender($data)
public function testRender(array $data): void
{
$template = new OutputBufferTemplate(__DIR__ . '/test_ok.phtml');
$output = $template->render($data);

//prepare expected context as it should be passed to the template through require directive.
//the data variable contains the original data, but if the data contain 'data' key then it overwrites it.
$expectedLocalVars = \array_merge(['data' => $data], (array) $data);
//numeric keys in data are not extracted into the context.
foreach ($expectedLocalVars as $key => $localVar) {
if (\is_numeric($key)) {
unset($expectedLocalVars[$key]);
}
}

//assert the test template has printed out the expected context.
$expectedLocalVars = \array_merge(['data' => $data], $data);

$this->assertSame(\print_r($expectedLocalVars, true), $output);
}

/**
* @return array
*/
public function provideRenderData()
public function provideRenderData(): array
{
return [
[[]],
[['test' => \md5(\time())]],
[['data' => \md5(\time())]],
[[15 => \md5(\time()), 'test' => \md5(\time())]],
];
}

/**
* @param array $data
*
* @dataProvider provideRenderInvalidSymbolsData
*/
public function testRenderInvalidSymbols(array $data): void
{
$template = new OutputBufferTemplate(__DIR__ . '/test_ok.phtml');

$this->expectException(\Throwable::class);

$template->render($data);
}

public function provideRenderInvalidSymbolsData(): array
{
return [
[['a', 'b']],
[['a' => 'b', 2 => 'c']],
[['1badvarname' => 'value']],
];
}
}

0 comments on commit e92d806

Please sign in to comment.