1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of graze/data-file |
4
|
|
|
* |
5
|
|
|
* Copyright (c) 2016 Nature Delivered Ltd. <https://www.graze.com> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
* |
10
|
|
|
* @license https://github.com/graze/data-file/blob/master/LICENSE.md |
11
|
|
|
* @link https://github.com/graze/data-file |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Graze\DataFile\Test\Unit\Node; |
15
|
|
|
|
16
|
|
|
use Graze\DataFile\Format\CsvFormat; |
17
|
|
|
use Graze\DataFile\Format\CsvFormatInterface; |
18
|
|
|
use Graze\DataFile\Format\FormatAwareInterface; |
19
|
|
|
use Graze\DataFile\Format\FormatInterface; |
20
|
|
|
use Graze\DataFile\Node\LocalFile; |
21
|
|
|
use Graze\DataFile\Test\TestCase; |
22
|
|
|
|
23
|
|
|
class LocalCsvFileTest extends TestCase |
24
|
|
|
{ |
25
|
|
|
public function testCloneWillCloneTheCsvDefinition() |
26
|
|
|
{ |
27
|
|
|
$file = (new LocalFile('some/path/here')) |
28
|
|
|
->setFormat(new CsvFormat()); |
29
|
|
|
$clone = $file->getClone(); |
30
|
|
|
|
31
|
|
|
static::assertNotSame($file, $clone); |
32
|
|
|
|
33
|
|
|
$clone->getFormat()->setDelimiter('--'); |
|
|
|
|
34
|
|
|
|
35
|
|
|
static::assertNotEquals($file->getFormat()->getDelimiter(), $clone->getFormat()->getDelimiter()); |
|
|
|
|
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function testImplementsInterface() |
39
|
|
|
{ |
40
|
|
|
$file = (new LocalFile('fake/path')) |
41
|
|
|
->setFormat(new CsvFormat()); |
42
|
|
|
|
43
|
|
|
static::assertInstanceOf(FormatAwareInterface::class, $file); |
44
|
|
|
static::assertInstanceOf(FormatInterface::class, $file->getFormat()); |
45
|
|
|
static::assertInstanceOf(CsvFormatInterface::class, $file->getFormat()); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function testFormatTypeIsCsv() |
49
|
|
|
{ |
50
|
|
|
$file = (new LocalFile('fake/path')) |
51
|
|
|
->setFormat(new CsvFormat()); |
52
|
|
|
|
53
|
|
|
static::assertEquals('csv', $file->getFormatType()); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function testDefaultsAreAssignedWhenNoOptionsSupplied() |
57
|
|
|
{ |
58
|
|
|
$file = (new LocalFile('fake/path')) |
59
|
|
|
->setFormat(new CsvFormat()); |
60
|
|
|
|
61
|
|
|
$format = $file->getFormat(); |
62
|
|
|
|
63
|
|
|
static::assertInstanceOf(CsvFormatInterface::class, $format); |
64
|
|
|
|
65
|
|
|
static::assertEquals(',', $format->getDelimiter(), "Default Delimiter should be ','"); |
66
|
|
|
static::assertTrue($format->hasQuotes(), "Quoting should be on by default"); |
67
|
|
|
static::assertEquals('\\N', $format->getNullOutput(), "Null character should be '\\N'"); |
68
|
|
|
static::assertTrue($format->hasHeaders(), "Headers should be on by default"); |
69
|
|
|
static::assertEquals(1, $format->getHeaders(), "Headers should have a default of 1"); |
70
|
|
|
static::assertEquals("\n", $format->getLineTerminator(), "Line terminator should be '\\n'"); |
71
|
|
|
static::assertEquals('"', $format->getQuoteCharacter(), "Default quote character should be \""); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
public function testAssigningOptionsModifiesTheDefinition() |
75
|
|
|
{ |
76
|
|
|
$file = (new LocalFile('fake/path')) |
77
|
|
|
->setFormat(new CsvFormat([ |
78
|
|
|
'delimiter' => "\t", |
79
|
|
|
'quoteCharacter' => '', |
80
|
|
|
'nullOutput' => '', |
81
|
|
|
'headers' => 0, |
82
|
|
|
'lineTerminator' => "----", |
83
|
|
|
])); |
84
|
|
|
|
85
|
|
|
$format = $file->getFormat(); |
86
|
|
|
|
87
|
|
|
static::assertEquals("\t", $format->getDelimiter(), "Delimiter should be set to '\\t' (tab)"); |
88
|
|
|
static::assertFalse($format->hasQuotes(), "Quoting should be off"); |
89
|
|
|
static::assertEquals('', $format->getNullOutput(), "Null character should be '' (blank)'"); |
90
|
|
|
static::assertFalse($format->hasHeaders(), "Headers should be off"); |
91
|
|
|
static::assertEquals("----", $format->getLineTerminator(), "Line terminator should be '----'"); |
92
|
|
|
static::assertEquals( |
93
|
|
|
'', |
94
|
|
|
$format->getQuoteCharacter(), |
95
|
|
|
"Default quote character should be blank when useQuotes is false" |
96
|
|
|
); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
public function testSettingOptionsModifiesTheDefinition() |
100
|
|
|
{ |
101
|
|
|
$file = (new LocalFile('fake/path')) |
102
|
|
|
->setFormat(new CsvFormat()); |
103
|
|
|
$format = $file->getFormat(); |
104
|
|
|
|
105
|
|
|
static::assertSame($format, $format->setDelimiter("\t"), "SetDelimiter should be fluent"); |
106
|
|
|
static::assertEquals("\t", $format->getDelimiter(), "Delimiter should be set to '\\t' (tab)"); |
107
|
|
|
static::assertSame($format, $format->setQuoteCharacter(''), "setQuoteCharacter should be fluent"); |
108
|
|
|
static::assertEquals('', $format->getQuoteCharacter(), "Quote character should be blank"); |
109
|
|
|
static::assertFalse($format->hasQuotes(), "Quoting should be off"); |
110
|
|
|
static::assertSame($format, $format->setNullOutput(''), "setNullOutput should be fluent"); |
111
|
|
|
static::assertEquals('', $format->getNullOutput(), "Null character should be '' (blank)'"); |
112
|
|
|
static::assertSame($format, $format->setHeaders(0), "setHeaders should be fluent"); |
113
|
|
|
static::assertFalse($format->hasHeaders(), "Headers should be off"); |
114
|
|
|
static::assertEquals(0, $format->getHeaders(), "Headers should be 0"); |
115
|
|
|
static::assertSame($format, $format->setLineTerminator('----'), "setLineTerminator should be fluent"); |
116
|
|
|
static::assertEquals("----", $format->getLineTerminator(), "Line terminator should be '----'"); |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: