Passed
Branch feature/static-formatter (adf5e0)
by Daniel
02:33
created

FormatterTest.php$1 ➔ getMockFormatter()   A

Complexity

Conditions 1

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 19
c 0
b 0
f 0
cc 1
rs 9.4285

1 Method

Rating   Name   Duplication   Size   Complexity  
A FormatterTest.php$1 ➔ format() 0 6 1
1
<?php declare(strict_types=1);
2
3
/**
4
 * This file is part of the Csv-Machine package.
5
 *
6
 * (c) Dan McAdams <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace RoadBunch\Csv\Tests\Formatter;
13
14
15
use function foo\func;
16
use PHPUnit\Framework\TestCase;
17
use RoadBunch\Csv\Exception\FormatterResultException;
18
use RoadBunch\Csv\Formatter\Formatter;
19
use RoadBunch\Csv\Formatter\FormatterInterface;
20
use RoadBunch\Csv\Formatter\UnderscoreToSpaceFormatter;
21
22
/**
23
 * Class FormatterTest
24
 *
25
 * @author  Dan McAdams
26
 * @package RoadBunch\Csv\Tests\Formatters
27
 */
28
class FormatterTest extends TestCase
29
{
30
    public function testArrayOfNonStrings()
31
    {
32
        $this->expectException(\InvalidArgumentException::class);
33
34
        $formatter = $this->getMockFormatter();
35
36
        $multiArray = [
37
            ['an array'],
38
            new \stdClass(),
39
            $this
40
        ];
41
        $formatter::format($multiArray);
42
    }
43
44
    public function testFormatArrayStrings()
45
    {
46
        $formatter = $this->getMockFormatter();
47
48
        $testArray = ['one', 'two', 'three'];
49
        $this->assertEquals(['ONE', 'TWO', 'THREE'], $formatter::format($testArray));
50
    }
51
52
    public function testFormatterReturnsNonArray()
53
    {
54
        $this->expectException(FormatterResultException::class);
55
56
        $formatter = new class extends Formatter
57
        {
58
            /**
59
             * @param array $data
60
             *
61
             * @return array
62
             * @throws FormatterResultException
63
             */
64
            public static function format(array $data): array
65
            {
66
                $callback = function ($var) {
67
                    return explode('_', $var);
68
                };
69
70
                return parent::applyFilter($callback, $data);
71
            }
72
        };
73
74
        $testArray = ['first_name'];
75
        $formatter::format($testArray);
76
    }
77
78
    /**
79
     * @return FormatterInterface
80
     */
81
    private function getMockFormatter()
82
    {
83
        $formatter = new class extends Formatter
84
        {
85
            /**
86
             * @param array $data
87
             *
88
             * @return array
89
             * @throws FormatterResultException
90
             */
91
            public static function format(array $data): array
92
            {
93
                $callback = function ($var) {
94
                    return strtoupper($var);
95
                };
96
                return parent::applyFilter($callback, $data);
97
            }
98
        };
99
        return $formatter;
100
    }
101
}
102