Sfoglia il codice sorgente

deblan/csv upgraded to the v2 and more units tests

tags/v2.0.0^0
Simon Vieille 2 anni fa
parent
commit
31adc51188
6 ha cambiato i file con 114 aggiunte e 38 eliminazioni
  1. 37
    4
      README.md
  2. 1
    1
      composer.json
  3. 6
    6
      example.php
  4. 9
    10
      src/Deblan/CsvValidator/Validator.php
  5. 44
    0
      tests/ExampleTest.php
  6. 17
    17
      tests/ValidatorTest.php

+ 37
- 4
README.md Vedi File

@@ -45,7 +45,7 @@ $validator->addFieldConstraint(0, new Email());
45 45
 $validator->addFieldConstraint(1, new Date());
46 46
 
47 47
 // Validate the legend
48
-$validator->setExpectedLegend(array('foo', 'bar', 'bim'));
48
+$validator->setExpectedHeaders(['foo', 'bar', 'bim']);
49 49
 
50 50
 // An line must contain 3 columns
51 51
 $validator->addDataConstraint(new Callback(function($data, ExecutionContextInterface $context) {
@@ -55,10 +55,10 @@ $validator->addDataConstraint(new Callback(function($data, ExecutionContextInter
55 55
 }));
56 56
 
57 57
 // Initialisation of the parser
58
-$parser = new CsvParser(__DIR__.'/tests/fixtures/example.csv');
59
-$parser->setHasLegend(true);
58
+$parser = new CsvParser();
59
+$parser->setHasHeaders(true);
60 60
 
61
-$validator->validate($parser);
61
+$validator->validate($parser->parseFile(__DIR__.'/tests/fixtures/example.csv'));
62 62
 
63 63
 if ($validator->isValid() === false) {
64 64
     foreach ($validator->getErrors() as $error) {
@@ -80,6 +80,39 @@ EOF;
80 80
 }
81 81
 ```
82 82
 
83
+Run `example.php` and see results:
84
+
85
+```
86
+<ul>
87
+    <li>Line: 1</li>
88
+    <li>Column: </li>
89
+    <li>
90
+        <p>Invalid legend.</p>
91
+    </li>
92
+</ul>
93
+<ul>
94
+    <li>Line: 4</li>
95
+    <li>Column: </li>
96
+    <li>
97
+        <p>The line must contain 3 columns</p>
98
+    </li>
99
+</ul>
100
+<ul>
101
+    <li>Line: 2</li>
102
+    <li>Column: 1</li>
103
+    <li>
104
+        <p>This value is not a valid email address.</p>
105
+    </li>
106
+</ul>
107
+<ul>
108
+    <li>Line: 3</li>
109
+    <li>Column: 2</li>
110
+    <li>
111
+        <p>This value is not a valid date.</p>
112
+    </li>
113
+</ul>
114
+```
115
+
83 116
 Contributors
84 117
 ------------
85 118
 

+ 1
- 1
composer.json Vedi File

@@ -17,6 +17,6 @@
17 17
     "require": {
18 18
         "php": ">=5.6.0",
19 19
         "symfony/validator": "2.*",
20
-        "deblan/csv": "v1.1"
20
+        "deblan/csv": "v2.0.*"
21 21
     }
22 22
 }

+ 6
- 6
example.php Vedi File

@@ -20,7 +20,7 @@ $validator->addFieldConstraint(0, new Email());
20 20
 $validator->addFieldConstraint(1, new Date());
21 21
 
22 22
 // Validate the legend
23
-$validator->setExpectedLegend(array('foo', 'bar', 'bim'));
23
+$validator->setExpectedHeaders(['foo', 'bar', 'bim']);
24 24
 
25 25
 // An line must contain 3 columns
26 26
 $validator->addDataConstraint(new Callback(function($data, ExecutionContextInterface $context) {
@@ -30,17 +30,17 @@ $validator->addDataConstraint(new Callback(function($data, ExecutionContextInter
30 30
 }));
31 31
 
32 32
 // Initialisation of the parser
33
-$parser = new CsvParser(__DIR__.'/tests/fixtures/example.csv');
34
-$parser->setHasLegend(true);
33
+$parser = new CsvParser();
34
+$parser->setHasHeaders(true);
35 35
 
36
-$validator->validate($parser);
36
+$validator->validate($parser->parseFile(__DIR__.'/tests/fixtures/example.csv'));
37 37
 
38 38
 if ($validator->isValid() === false) {
39 39
     foreach ($validator->getErrors() as $error) {
40
-        $line = $error->getLine(); 
40
+        $line = $error->getLine();
41 41
         $column = $error->getColumn();
42 42
         $message = $error->getViolation()->getMessage();
43
-        
43
+
44 44
         echo <<<EOF
45 45
 <ul>
46 46
     <li>Line: $line</li>

+ 9
- 10
src/Deblan/CsvValidator/Validator.php Vedi File

@@ -49,7 +49,7 @@ class Validator
49 49
     /**
50 50
      * @var array
51 51
      */
52
-    protected $expectedLegend = [];
52
+    protected $expectedHeaders = [];
53 53
 
54 54
     /**
55 55
      * Constructor.
@@ -105,9 +105,9 @@ class Validator
105 105
      *
106 106
      * @return Validator
107 107
      */
108
-    public function setExpectedLegend(array $legend)
108
+    public function setExpectedHeaders(array $legend)
109 109
     {
110
-        $this->expectedLegend = $legend;
110
+        $this->expectedHeaders = $legend;
111 111
 
112 112
         return $this;
113 113
     }
@@ -121,13 +121,12 @@ class Validator
121 121
     {
122 122
         if ($this->parser !== $parser) {
123 123
             $this->parser = $parser;
124
-            $this->parser->parse();
125 124
             $this->errors = [];
126 125
         } elseif ($this->hasValidate) {
127 126
             return;
128 127
         }
129 128
 
130
-        $this->validateLegend();
129
+        $this->validateHeaders();
131 130
         $this->validateDatas();
132 131
         $this->validateFields();
133 132
 
@@ -137,17 +136,17 @@ class Validator
137 136
     /**
138 137
      * Validates the legend.
139 138
      */
140
-    protected function validateLegend()
139
+    protected function validateHeaders()
141 140
     {
142
-        if (!$this->parser->getHasLegend()) {
141
+        if (!$this->parser->getHasHeaders()) {
143 142
             return;
144 143
         }
145 144
 
146
-        if (empty($this->expectedLegend)) {
145
+        if (empty($this->expectedHeaders)) {
147 146
             return;
148 147
         }
149 148
 
150
-        if ($this->parser->getLegend() !== $this->expectedLegend) {
149
+        if ($this->parser->getHeaders() !== $this->expectedHeaders) {
151 150
             $this->mergeErrorMessage('Invalid legend.', 1);
152 151
         }
153 152
     }
@@ -294,7 +293,7 @@ class Validator
294 293
      */
295 294
     protected function getTrueLine($line)
296 295
     {
297
-        if ($this->parser->getHasLegend()) {
296
+        if ($this->parser->getHasHeaders()) {
298 297
             ++$line;
299 298
         }
300 299
 

+ 44
- 0
tests/ExampleTest.php Vedi File

@@ -0,0 +1,44 @@
1
+<?php
2
+
3
+/**
4
+ * class ExampleTest.
5
+ *
6
+ * @author Simon Vieille <simon@deblan.fr>
7
+ */
8
+class ExampleTest extends \PHPUnit_Framework_TestCase
9
+{
10
+    public function testExemple()
11
+    {
12
+        $content = shell_exec('php -f '.__DIR__.'/../example.php');
13
+
14
+        $this->assertEquals('<ul>
15
+    <li>Line: 1</li>
16
+    <li>Column: </li>
17
+    <li>
18
+        <p>Invalid legend.</p>
19
+    </li>
20
+</ul>
21
+<ul>
22
+    <li>Line: 4</li>
23
+    <li>Column: </li>
24
+    <li>
25
+        <p>The line must contain 3 columns</p>
26
+    </li>
27
+</ul>
28
+<ul>
29
+    <li>Line: 2</li>
30
+    <li>Column: 1</li>
31
+    <li>
32
+        <p>This value is not a valid email address.</p>
33
+    </li>
34
+</ul>
35
+<ul>
36
+    <li>Line: 3</li>
37
+    <li>Column: 2</li>
38
+    <li>
39
+        <p>This value is not a valid date.</p>
40
+    </li>
41
+</ul>
42
+', $content);
43
+    }
44
+}

+ 17
- 17
tests/ValidatorTest.php Vedi File

@@ -16,20 +16,20 @@ class ValidatorTest extends \PHPUnit_Framework_TestCase
16 16
         $validator->isValid();
17 17
     }
18 18
 
19
-    public function testExpectedLegend()
19
+    public function testExpectedHeaders()
20 20
     {
21 21
         $parser = $this->generateParser('example.csv');
22
-        $parser->setHasLegend(true);
22
+        $parser->setHasHeaders(true);
23 23
 
24 24
         $validator = $this->generateValidator();
25
-        $validator->setExpectedLegend(['foo', 'bar', 'boo']);
26
-        $validator->validate($parser);
25
+        $validator->setExpectedHeaders(['foo', 'bar', 'boo']);
26
+        $validator->validate($parser->parseFile(__DIR__.'/fixtures/example.csv'));
27 27
         $this->assertEquals(true, $validator->isValid());
28 28
         $this->assertEquals(0, count($validator->getErrors()));
29 29
 
30 30
         $validator = $this->generateValidator();
31
-        $validator->setExpectedLegend(['bad', 'legend']);
32
-        $validator->validate($parser);
31
+        $validator->setExpectedHeaders(['bad', 'legend']);
32
+        $validator->validate($parser->parseFile(__DIR__.'/fixtures/example.csv'));
33 33
         $this->assertEquals(false, $validator->isValid());
34 34
         $this->assertEquals(1, count($validator->getErrors()));
35 35
     }
@@ -38,7 +38,7 @@ class ValidatorTest extends \PHPUnit_Framework_TestCase
38 38
     {
39 39
         $parser = $this->generateParser('example.csv');
40 40
         $validator = $this->generateValidator();
41
-        $validator->validate($parser);
41
+        $validator->validate($parser->parseFile(__DIR__.'/fixtures/example.csv'));
42 42
         $this->assertEquals(true, $validator->isValid());
43 43
     }
44 44
 
@@ -47,14 +47,14 @@ class ValidatorTest extends \PHPUnit_Framework_TestCase
47 47
         $parser = $this->generateParser('example.csv');
48 48
         $validator = $this->generateValidator();
49 49
         $validator->addFieldConstraint(0, new NotBlank());
50
-        $validator->validate($parser);
50
+        $validator->validate($parser->parseFile(__DIR__.'/fixtures/example.csv'));
51 51
         $this->assertEquals(true, $validator->isValid());
52 52
         $this->assertEquals(0, count($validator->getErrors()));
53 53
 
54 54
         $parser = $this->generateParser('example.csv');
55 55
         $validator = $this->generateValidator();
56 56
         $validator->addFieldConstraint(1, new NotBlank());
57
-        $validator->validate($parser);
57
+        $validator->validate($parser->parseFile(__DIR__.'/fixtures/example.csv'));
58 58
         $this->assertEquals(true, $validator->isValid());
59 59
         $this->assertEquals(0, count($validator->getErrors()));
60 60
 
@@ -62,24 +62,24 @@ class ValidatorTest extends \PHPUnit_Framework_TestCase
62 62
         $validator = $this->generateValidator();
63 63
         $validator->addFieldConstraint(0, new NotBlank());
64 64
         $validator->addFieldConstraint(1, new NotBlank());
65
-        $validator->validate($parser);
65
+        $validator->validate($parser->parseFile(__DIR__.'/fixtures/example.csv'));
66 66
         $this->assertEquals(true, $validator->isValid());
67 67
         $this->assertEquals(0, count($validator->getErrors()));
68 68
     }
69 69
 
70 70
     public function testFieldContraintsKo()
71 71
     {
72
-        $parser = $this->generateParser('example.csv');
72
+        $parser = $this->generateParser();
73 73
         $validator = $this->generateValidator();
74 74
         $validator->addFieldConstraint(0, new Email());
75
-        $validator->validate($parser);
75
+        $validator->validate($parser->parseFile(__DIR__.'/fixtures/example.csv'));
76 76
         $this->assertEquals(false, $validator->isValid());
77 77
         $this->assertEquals(2, count($validator->getErrors()));
78 78
 
79
-        $parser = $this->generateParser('example.csv');
79
+        $parser = $this->generateParser();
80 80
         $validator = $this->generateValidator();
81 81
         $validator->addFieldConstraint(1, new Email());
82
-        $validator->validate($parser);
82
+        $validator->validate($parser->parseFile(__DIR__.'/fixtures/example.csv'));
83 83
         $this->assertEquals(false, $validator->isValid());
84 84
         $this->assertEquals(5, count($validator->getErrors()));
85 85
 
@@ -87,14 +87,14 @@ class ValidatorTest extends \PHPUnit_Framework_TestCase
87 87
         $validator = $this->generateValidator();
88 88
         $validator->addFieldConstraint(0, new Email());
89 89
         $validator->addFieldConstraint(1, new Email());
90
-        $validator->validate($parser);
90
+        $validator->validate($parser->parseFile(__DIR__.'/fixtures/example.csv'));
91 91
         $this->assertEquals(false, $validator->isValid());
92 92
         $this->assertEquals(7, count($validator->getErrors()));
93 93
     }
94 94
 
95
-    protected function generateParser($file)
95
+    protected function generateParser()
96 96
     {
97
-        return new CsvParser(__DIR__.'/fixtures/'.$file);
97
+        return new CsvParser();
98 98
     }
99 99
 
100 100
     protected function generateValidator()

Loading…
Annulla
Salva