|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * This file is part of Blitz PHP framework. |
| 5 | + * |
| 6 | + * (c) 2022 Dimitri Sitchet Tomkeu <devcode.dst@gmail.com> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view |
| 9 | + * the LICENSE file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +use BlitzPHP\Formatter\CsvFormatter; |
| 13 | + |
| 14 | +use function Kahlan\allow; |
| 15 | +use function Kahlan\expect; |
| 16 | + |
| 17 | +describe('CsvFormatter', function() { |
| 18 | + beforeEach(function() { |
| 19 | + $this->formatter = new CsvFormatter(); |
| 20 | + }); |
| 21 | + |
| 22 | + describe('->format()', function() { |
| 23 | + it('devrait formater un tableau simple en CSV', function() { |
| 24 | + $data = ['nom' => 'Jean', 'age' => 30]; |
| 25 | + $result = $this->formatter->format($data); |
| 26 | + $expected = mb_convert_encoding("nom,age\nJean,30\n", 'UTF-16LE', 'UTF-8'); |
| 27 | + expect($result)->toBe($expected); |
| 28 | + }); |
| 29 | + |
| 30 | + it('devrait formater un tableau multidimensionnel en CSV', function() { |
| 31 | + $data = [ |
| 32 | + ['nom' => 'Jean', 'age' => 30], |
| 33 | + ['nom' => 'Marie', 'age' => 25] |
| 34 | + ]; |
| 35 | + $result = $this->formatter->format($data); |
| 36 | + $expected = mb_convert_encoding("nom,age\nJean,30\nMarie,25\n", 'UTF-16LE', 'UTF-8'); |
| 37 | + expect($result)->toBe($expected); |
| 38 | + }); |
| 39 | + |
| 40 | + xit('devrait retourner null si l\'ouverture du fichier temporaire échoue', function() { |
| 41 | + allow('fopen')->toBeCalled()->andReturn(false); |
| 42 | + expect($this->formatter->format(['test' => 'data']))->toBeNull(); |
| 43 | + }); |
| 44 | + }); |
| 45 | + |
| 46 | + describe('->parse()', function() { |
| 47 | + it('devrait analyser une chaîne CSV en tableau', function() { |
| 48 | + $csv = "nom,age\nJean,30\nMarie,25"; |
| 49 | + $expected = [ |
| 50 | + ['nom' => 'Jean', 'age' => '30'], |
| 51 | + ['nom' => 'Marie', 'age' => '25'] |
| 52 | + ]; |
| 53 | + expect($this->formatter->parse($csv))->toBe($expected); |
| 54 | + }); |
| 55 | + |
| 56 | + it('devrait analyser une chaîne CSV en tableau', function() { |
| 57 | + $csv = "nom,age,sexe"; |
| 58 | + $expected = ['nom', 'age', 'sexe']; |
| 59 | + expect($this->formatter->parse($csv))->toBe($expected); |
| 60 | + }); |
| 61 | + }); |
| 62 | + |
| 63 | + describe('->setDelimiter()', function() { |
| 64 | + it('devrait définir le délimiteur', function() { |
| 65 | + $this->formatter->setDelimiter(';'); |
| 66 | + expect($this->formatter->getDelimiter())->toBe(';'); |
| 67 | + }); |
| 68 | + |
| 69 | + it('devrait utiliser le premier caractère si une chaîne plus longue est fournie', function() { |
| 70 | + $this->formatter->setDelimiter('abc'); |
| 71 | + expect($this->formatter->getDelimiter())->toBe('a'); |
| 72 | + }); |
| 73 | + |
| 74 | + it('devrait utiliser la virgule par défaut si une chaîne vide est fournie', function() { |
| 75 | + $this->formatter->setDelimiter(''); |
| 76 | + expect($this->formatter->getDelimiter())->toBe(','); |
| 77 | + }); |
| 78 | + }); |
| 79 | + |
| 80 | + describe('->setEnclosure()', function() { |
| 81 | + it('devrait définir l\'encadrement', function() { |
| 82 | + $this->formatter->setEnclosure('\''); |
| 83 | + expect($this->formatter->getEnclosure())->toBe('\''); |
| 84 | + }); |
| 85 | + |
| 86 | + it('devrait utiliser le premier caractère si une chaîne plus longue est fournie', function() { |
| 87 | + $this->formatter->setEnclosure('abc'); |
| 88 | + expect($this->formatter->getEnclosure())->toBe('a'); |
| 89 | + }); |
| 90 | + |
| 91 | + it('devrait utiliser les guillemets doubles par défaut si une chaîne vide est fournie', function() { |
| 92 | + $this->formatter->setEnclosure(''); |
| 93 | + expect($this->formatter->getEnclosure())->toBe('"'); |
| 94 | + }); |
| 95 | + }); |
| 96 | +}); |
0 commit comments