Completed
Push — master ( b74cd0...088e4f )
by Dennis
11:47
created

getPostcodeWithUpperCaseReturnsPostcode()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
namespace Dennis\Seeder\Tests\Unit\Provider;
3
4
/***************************************************************
5
 *
6
 *  Copyright notice
7
 *
8
 *  (c) 2016 Dennis Römmich <[email protected]>
9
 *
10
 *  All rights reserved
11
 *
12
 *  This script is part of the TYPO3 project. The TYPO3 project is
13
 *  free software; you can redistribute it and/or modify
14
 *  it under the terms of the GNU General Public License as published by
15
 *  the Free Software Foundation; either version 3 of the License, or
16
 *  (at your option) any later version.
17
 *
18
 *  The GNU General Public License can be found at
19
 *  http://www.gnu.org/copyleft/gpl.html.
20
 *
21
 *  This script is distributed in the hope that it will be useful,
22
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
23
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24
 *  GNU General Public License for more details.
25
 *
26
 *  This copyright notice MUST APPEAR in all copies of the script!
27
 ***************************************************************/
28
use Dennis\Seeder\Provider\Faker;
29
use TYPO3\CMS\Core\Tests\UnitTestCase;
30
31
/**
32
 * Test case for class \Dennis\Seeder\Provider\Faker
33
 *
34
 * @author Dennis Römmich <[email protected]>
35
 */
36
class FakerTest extends UnitTestCase
0 ignored issues
show
Deprecated Code introduced by
The class TYPO3\CMS\Core\Tests\UnitTestCase has been deprecated with message: will be removed once TYPO3 9 LTS is released

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
37
{
38
    /**
39
     * @var Faker $subject
40
     */
41
    protected $subject;
42
43
    protected function setUp()
0 ignored issues
show
Coding Style introduced by
setUp uses the super-global variable $GLOBALS which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
44
    {
45
        $faker = \Faker\Factory::create();
46
        $faker->seed(1234);
47
        $this->subject = new Faker($faker);
48
        $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['seeder']['provider']['customdata'] =
49
            \Dennis\Seeder\Tests\Unit\Provider\CustomDataProvider::class;
50
    }
51
52
    protected function tearDown()
0 ignored issues
show
Coding Style introduced by
tearDown uses the super-global variable $GLOBALS which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
53
    {
54
        unset($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['seeder']['provider']['customdata']);
55
    }
56
57
    /**
58
     * @method guessProviderName
59
     * @test
60
     * @throws \Dennis\Seeder\Provider\NotFoundException
61
     */
62
    public function guessProviderNameByKeyWithEmptyArgumentThrowsNotFoundException()
63
    {
64
        $this->setExpectedException(
65
            \Dennis\Seeder\Provider\NotFoundException::class
66
        );
67
        $this->subject->guessProviderName('');
68
    }
69
70
    /**
71
     * @method guessProviderName
72
     * @test
73
     * @throws \Dennis\Seeder\Provider\NotFoundException
74
     */
75
    public function guessProviderNameReturnsProvidername()
76
    {
77
        $this->assertSame('postcode', $this->subject->guessProviderName('zip'));
78
    }
79
80
    /**
81
     * @method guessProviderNameByKey
82
     * @test
83
     * @throws \Dennis\Seeder\Provider\NotFoundException
84
     */
85
    public function guessProviderNameByKeyReturnsNullIfNotFound()
86
    {
87
        $this->assertSame(null, $this->subject->guessProviderName('doesnotexist'));
88
    }
89
90
    /**
91
     * @method get
92
     * @test
93
     * @throws \Dennis\Seeder\Provider\NotFoundException
94
     */
95
    public function getThrowsExceptionIfNoProviderWasFound()
96
    {
97
        $this->setExpectedException(\Dennis\Seeder\Provider\NotFoundException::class);
98
        $this->subject->get('doesnotexist');
99
    }
100
101
    /**
102
     * @method get
103
     * @test
104
     * @throws \Dennis\Seeder\Provider\NotFoundException
105
     */
106
    public function getZipReturnsPostcode()
107
    {
108
        $this->assertSame('49766-3666', $this->subject->get('zip'));
109
    }
110
111
    /**
112
     * @method get
113
     * @test
114
     * @throws \Dennis\Seeder\Provider\NotFoundException
115
     */
116
    public function getPostcodeReturnsPostcode()
117
    {
118
        $this->assertSame('49766-3666', $this->subject->get('postcode'));
119
    }
120
121
    /**
122
     * @method get
123
     * @test
124
     * @throws \Dennis\Seeder\Provider\NotFoundException
125
     */
126
    public function getPostcodeWithUnderscoreReturnsPostcode()
127
    {
128
        $this->assertSame('49766-3666', $this->subject->get('post_code'));
129
    }
130
131
    /**
132
     * @method get
133
     * @test
134
     * @throws \Dennis\Seeder\Provider\NotFoundException
135
     */
136
    public function getPostcodeWithWeiredCaseReturnsPostcode()
137
    {
138
        $this->assertSame('49766-3666', $this->subject->get('poSt_COde'));
139
    }
140
141
    /**
142
     * @method get
143
     * @test
144
     * @throws \Dennis\Seeder\Provider\NotFoundException
145
     */
146
    public function getPostcodeWithUpperCaseReturnsPostcode()
147
    {
148
        $this->assertSame('49766-3666', $this->subject->get('POSTCODE'));
149
    }
150
151
    /**
152
     * @method get
153
     * @test
154
     * @throws \Dennis\Seeder\Provider\NotFoundException
155
     */
156
    public function guessProviderNameReturnsNullWithSkippedFieldName()
157
    {
158
        $skippedFieldName = 'l10n_parent';
159
        $this->assertSame(null, $this->subject->guessProviderName($skippedFieldName));
160
    }
161
162
    /**
163
     * @method __call
164
     * @test
165
     */
166
    public function returnCustomDataIfCustomProviderIsAvailable()
167
    {
168
        $this->assertSame('customData', $this->subject->getCustomData());
0 ignored issues
show
Documentation Bug introduced by
The method getCustomData does not exist on object<Dennis\Seeder\Provider\Faker>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
169
    }
170
}
171