Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Many symfonies #2

Merged
merged 20 commits into from
Mar 11, 2023
50 changes: 30 additions & 20 deletions .github/workflows/php.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,39 @@ on:
branches: [ main ]

jobs:
build:

run:
runs-on: ubuntu-latest
strategy:
matrix:
symfony-versions: ['^4.4', '^5.0', '^6.0']
php-versions: ['8.1', '8.0', '7.4']
# Exclude combinations that are not installable.
exclude:
- symfony-versions: '^6.0'
php-versions: '7.4'

steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v3

- name: Install dependencies
uses: php-actions/composer@v6
with:
php_version: ${{ matrix.php-versions }}

- name: Validate composer.json and composer.lock
run: composer validate --strict
- name: Update Symfony
uses: php-actions/composer@v6
with:
php_version: ${{ matrix.php-versions }}
command: require
args: symfony/symfony:${{ matrix.symfony-versions }} -W

- name: Cache Composer packages
id: composer-cache
uses: actions/cache@v2
with:
path: vendor
key: ${{ runner.os }}-php-${{ hashFiles('**/composer.lock') }}
restore-keys: |
${{ runner.os }}-php-
- name: PHPUnit (script)
run: ./vendor/bin/phpunit

- name: Install dependencies
run: composer install --prefer-dist --no-progress --no-suggest

- name: PHPUnit (php-actions)
uses: php-actions/phpunit@v2
with:
configuration: ./phpunit.xml
# - name: PHPUnit (php-actions)
# uses: php-actions/phpunit@v3
# with:
# php_version: ${{ matrix.php-versions }}
# # TODO: https://github.com/php-actions/phpunit/issues/51
# vendored_phpunit_path: vendor/bin/phpunit
# configuration: ./phpunit.xml
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ CSVResponse
[![SymfonyInsight](https://insight.symfony.com/projects/98a48652-16bb-4100-89bd-e4ef7579c429/mini.svg)](https://insight.symfony.com/projects/98a48652-16bb-4100-89bd-e4ef7579c429)
![Php Composer](https://github.com/ilbee/csv-response/actions/workflows/php.yml/badge.svg)

Add a CSV export Response in your [Symfony] 4 controller.
Add a CSV export Response in your [Symfony] controller.

Installation
------------
Expand Down
16 changes: 13 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,13 @@
"issues": "https://github.com/ilbee/csv-response/issues"
},
"require": {
"php": ">=7.1.3",
"symfony/symfony": "4.4.*"
"php": ">=7.4 <9",
"symfony/symfony": "^4 || ^5 || ^6"
},
"require-dev": {
"phpunit/phpunit": "^8.0 || ^9.0"
"phpunit/phpunit": "^9.6",
"squizlabs/php_codesniffer": "^3.7",
"rector/rector": "^0.15.21"
},
"autoload": {
"psr-4": {
Expand All @@ -35,5 +37,13 @@
"psr-4" : {
"Ilbee\\CSVResponse\\Tests\\" : "tests/"
}
},
"scripts": {
"phpcbf": "./vendor/bin/phpcbf",
"phpcs": "./vendor/bin/phpcs",
"rector": "./vendor/bin/rector process",
"rector-dry-run": "./vendor/bin/rector process --dry-run",
"test": "./vendor/bin/phpunit",
"test-coverage": "XDEBUG_MODE=coverage ./vendor/bin/phpunit --coverage-html ./coverage"
}
}
12 changes: 12 additions & 0 deletions phpcs.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<ruleset name="ilbee">
<description>PHP CodeSniffer configuration.</description>

<file>src</file>
<file>tests</file>
<file>rector.php</file>

<rule ref="PSR1"/>
<rule ref="PSR2"/>

</ruleset>
9 changes: 8 additions & 1 deletion phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit bootstrap="tests/Bootstrap.php">
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.6/phpunit.xsd"
bootstrap="tests/Bootstrap.php">
<coverage processUncoveredFiles="true">
<include>
<directory suffix=".php">src</directory>
</include>
</coverage>
<testsuites>
<testsuite name="Project Test Suite">
<directory>tests</directory>
Expand Down
22 changes: 22 additions & 0 deletions rector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

declare(strict_types=1);

use Rector\Config\RectorConfig;
use Rector\Php73\Rector\FuncCall\JsonThrowOnErrorRector;
use Rector\Set\ValueObject\LevelSetList;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->paths([
__DIR__ . '/src',
__DIR__ . '/tests',
]);

$rectorConfig->sets([
LevelSetList::UP_TO_PHP_74,
]);

$rectorConfig->skip([
JsonThrowOnErrorRector::class,
]);
};
24 changes: 12 additions & 12 deletions src/CSVResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,18 @@

class CSVResponse extends Response
{
private $fileName = 'CSVExport';
private $separator;
private string $fileName = 'CSVExport';
private ?string $separator = null;

const COMMA = ',';
const SEMICOLON = ';';
public const COMMA = ',';
public const SEMICOLON = ';';

public function __construct(array $data, ?string $fileName = null, ?string $separator = self::SEMICOLON)
{
parent::__construct();

$this->separator = $separator;
if ( $fileName ) {
if ($fileName) {
$this->fileName = $fileName;
}

Expand All @@ -29,7 +29,7 @@ public function __construct(array $data, ?string $fileName = null, ?string $sepa
private function initContent($data): string
{
$fp = fopen('php://temp', 'w');
foreach ( $this->prepareData($data) as $fields) {
foreach ($this->prepareData($data) as $fields) {
fputcsv($fp, $fields, $this->separator);
}

Expand All @@ -44,19 +44,19 @@ private function prepareData(array $data): array
{
$i = 0;
$output = [];
foreach ( $data as $row ) {
if ( $i === 0 ) {
foreach ($data as $row) {
if ($i === 0) {
$head = [];
foreach ( $row as $key => $value ) {
foreach ($row as $key => $value) {
$head[] = $key;
}
$output[] = $head;
}

$line = [];
foreach ( $row as $key => $value ) {
if ( is_object($value) ) {
if ( get_class($value) == 'DateTime' ) {
foreach ($row as $key => $value) {
if (is_object($value)) {
if (get_class($value) == 'DateTime') {
$value = $value->format('Y-m-d H:i:s');
}
}
Expand Down
4 changes: 2 additions & 2 deletions tests/Bootstrap.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?php

if ( !@include_once __DIR__ . '/../vendor/autoload.php' ) {
if (!@include_once __DIR__ . '/../vendor/autoload.php') {
exit("Misisng project dependencies.");
}
}
56 changes: 52 additions & 4 deletions tests/CSVResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,61 @@

namespace Ilbee\CSVResponse\Tests;


use Ilbee\CSVResponse\CSVResponse;
use PHPUnit\Framework\TestCase;

/**
* @covers \Ilbee\CSVResponse\CSVResponse
*/
class CSVResponseTest extends TestCase
{
public function testReadData(): void
protected function getData(): array
{
$data = [];
$data[] = [
'firstName' => 'Marcel',
'lastName' => 'TOTO',
];
$data[] = [
'firstName' => 'Maurice',
'lastName' => 'TATA',
];
return $data;
}

public function testResponse(): void
{
// Defaults to semicolon separator.
$response = new CSVResponse($this->getData());
$this->assertSame(
"firstName;lastName\nMarcel;TOTO\nMaurice;TATA\n",
$response->getContent()
);
// Use a comma to separate values.
$response = new CSVResponse($this->getData(), 'my-file-name', CSVResponse::COMMA);
$this->assertSame(
"firstName,lastName\nMarcel,TOTO\nMaurice,TATA\n",
$response->getContent()
);
$this->assertEquals(
'text/csv',
$response->headers->get('content-type')
);
$this->assertEquals(
'attachment; filename="my-file-name.csv"',
$response->headers->get('content-disposition')
);
}

public function testDateTime(): void
{
$this->assertTrue(true);
$now = new \DateTime();
$response = new CSVResponse([
['datetime' => $now],
]);
$this->assertStringContainsString(
$now->format('Y-m-d H:i:s'),
$response->getContent()
);
}
}
}