Completed
Push — master ( 7a3cb1...e42624 )
by Harry
9s
created

LocalCsvFileTest   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 96
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 3
Bugs 1 Features 0
Metric Value
wmc 6
c 3
b 1
f 0
lcom 1
cbo 3
dl 0
loc 96
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A testCloneWillCloneTheCsvDefinition() 0 12 1
A testImplementsInterface() 0 9 1
A testFormatTypeIsCsv() 0 7 1
A testDefaultsAreAssignedWhenNoOptionsSupplied() 0 17 1
B testAssigningOptionsModifiesTheDefinition() 0 24 1
A testSettingOptionsModifiesTheDefinition() 0 19 1
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('--');
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Graze\DataFile\Format\FormatInterface as the method setDelimiter() does only exist in the following implementations of said interface: Graze\DataFile\Format\CsvFormat.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

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

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
34
35
        static::assertNotEquals($file->getFormat()->getDelimiter(), $clone->getFormat()->getDelimiter());
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Graze\DataFile\Format\FormatInterface as the method getDelimiter() does only exist in the following implementations of said interface: Graze\DataFile\Format\CsvFormat.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

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

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
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