|
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
|
|
|
$format = $clone->getFormat(); |
|
32
|
|
|
static::assertInstanceOf(CsvFormatInterface::class, $format); |
|
33
|
|
|
static::assertInstanceOf(CsvFormat::class, $format); |
|
34
|
|
|
|
|
35
|
|
|
static::assertNotSame($file, $clone); |
|
36
|
|
|
|
|
37
|
|
|
$format->setDelimiter('--'); |
|
38
|
|
|
|
|
39
|
|
|
static::assertNotEquals($file->getFormat()->getDelimiter(), $format->getDelimiter()); |
|
|
|
|
|
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
public function testImplementsInterface() |
|
43
|
|
|
{ |
|
44
|
|
|
$file = (new LocalFile('fake/path')) |
|
45
|
|
|
->setFormat(new CsvFormat()); |
|
46
|
|
|
|
|
47
|
|
|
static::assertInstanceOf(FormatAwareInterface::class, $file); |
|
48
|
|
|
static::assertInstanceOf(FormatInterface::class, $file->getFormat()); |
|
49
|
|
|
static::assertInstanceOf(CsvFormatInterface::class, $file->getFormat()); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
public function testFormatTypeIsCsv() |
|
53
|
|
|
{ |
|
54
|
|
|
$file = (new LocalFile('fake/path')) |
|
55
|
|
|
->setFormat(new CsvFormat()); |
|
56
|
|
|
|
|
57
|
|
|
static::assertEquals('csv', $file->getFormatType()); |
|
58
|
|
|
} |
|
59
|
|
|
} |
|
60
|
|
|
|
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: