UpdaterTest::testUpdater()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
dl 0
loc 22
rs 9.568
c 0
b 0
f 0
nc 2
nop 1
1
<?php
2
/**
3
 * integer_net Magento Module
4
 *
5
 * @category   IntegerNet
6
 * @package    IntegerNet_Anonymizer
7
 * @copyright  Copyright (c) 2015 integer_net GmbH (http://www.integer-net.de/)
8
 * @author     Fabian Schmengler <[email protected]>
9
 */
10
11
namespace IntegerNet\Anonymizer;
12
13
14
use IntegerNet\Anonymizer\Mock\AnonymizableMock;
15
use IntegerNet\Anonymizer\Mock\CollectionMock;
16
17
class UpdaterTest extends \PHPUnit_Framework_TestCase
18
{
19
    /**
20
     * @var Anonymizer|\PHPUnit_Framework_MockObject_MockObject
21
     */
22
    private $anonymizerMock;
23
    /**
24
     * @var Updater
25
     */
26
    private $updater;
27
28
    protected function setUp()
29
    {
30
        $this->anonymizerMock = $this->getMock(Anonymizer::__CLASS, array('anonymize', 'resetUniqueGenerator'), array(), '', false);
31
        $this->updater = new Updater($this->anonymizerMock);
32
    }
33
34
    /**
35
     * @test
36
     * @dataProvider getCollectionData
37
     */
38
    public function testUpdater($collectionData)
39
    {
40
        $entityCount = count($collectionData);
41
42
        $this->anonymizerMock->expects($this->exactly(1))->method('resetUniqueGenerator');
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in IntegerNet\Anonymizer\Anonymizer.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
43
        $this->anonymizerMock->expects($this->exactly($entityCount))->method('anonymize');
44
45
        $entityModel = $this->getEntityMock();
46
        foreach ($collectionData as $i => $entityData) {
47
            $entityModel->expects($this->at($i * 3))->method('setRawData')
48
                ->with($entityData);
49
            $entityModel->expects($this->at($i * 3 + 1))->method('updateValues');
50
            $entityModel->expects($this->at($i * 3 + 2))->method('clearInstance');
51
        }
52
        $entityModel->expects($this->exactly($entityCount))->method('setRawData');
53
        $entityModel->expects($this->exactly($entityCount))->method('updateValues');
54
        $entityModel->expects($this->exactly($entityCount))->method('clearInstance');
55
56
        $collectionIterator = new CollectionMock($collectionData);
57
58
        $this->updater->update($collectionIterator, $entityModel);
59
    }
60
61
    private function getEntityMock()
62
    {
63
        return $this->getMock(AnonymizableMock::__CLASS, array('setRawData', 'updateValues', 'clearInstance'));
64
    }
65
66
    /**
67
     * @test
68
     * @dataProvider getCollectionData
69
     */
70
    public function testOutputControl($collectionData)
71
    {
72
        $stream = fopen('php://temp', 'r+');
73
        $this->updater->setOutputStream($stream);
74
        $this->updater->update(new CollectionMock($collectionData), $this->getEntityMock());
75
        $actualOutput = stream_get_contents($stream, -1, 0);
76
        $this->assertContains('Updater started at', $actualOutput);
77
        $this->assertContains('Updating Mock: 1/5 ', $actualOutput);
78
        $this->assertContains('Updating Mock: 2/5 ', $actualOutput);
79
        $this->assertContains('Updating Mock: 3/5 ', $actualOutput);
80
        $this->assertContains('Updating Mock: 4/5 ', $actualOutput);
81
        $this->assertContains('Updating Mock: 5/5 ', $actualOutput);
82
        $this->assertContains('Updater finished at', $actualOutput);
83
        fclose($stream);
84
85
        $stream = fopen('php://temp', 'r+');
86
        $this->updater->setOutputStream($stream);
87
        $this->updater->setProgressSteps(2);
88
        $this->updater->update(new CollectionMock($collectionData), $this->getEntityMock());
89
        $actualOutput = stream_get_contents($stream, -1, 0);
90
        $this->assertContains('Updater started at', $actualOutput);
91
        $this->assertNotContains('Updating Mock: 1/5 ', $actualOutput);
92
        $this->assertContains('Updating Mock: 2/5 ', $actualOutput);
93
        $this->assertNotContains('Updating Mock: 3/5 ', $actualOutput);
94
        $this->assertContains('Updating Mock: 4/5 ', $actualOutput);
95
        $this->assertNotContains('Updating Mock: 5/5 ', $actualOutput);
96
        $this->assertContains('Updater finished at', $actualOutput);
97
        fclose($stream);
98
99
        $stream = fopen('php://temp', 'r+');
100
        $this->updater->setOutputStream($stream);
101
        $this->updater->setShowProgress(false);
102
        $this->updater->update(new CollectionMock($collectionData), $this->getEntityMock());
103
        $actualOutput = stream_get_contents($stream, -1, 0);
104
        $this->assertContains('Updater started at', $actualOutput);
105
        $this->assertNotContains('Updating ', $actualOutput);
106
        $this->assertContains('Updater finished at', $actualOutput);
107
        fclose($stream);
108
109
    }
110
111
    public static function getCollectionData()
112
    {
113
        return array(
114
            array(
115
                'collectionData' => array(
116
                    array('email' => '[email protected]', 'name' => 'Death'),
117
                    array('email' => '[email protected]', 'name' => 'Pestilence'),
118
                    array('email' => '[email protected]', 'name' => 'Famine'),
119
                    array('email' => '[email protected]', 'name' => 'War'),
120
                    array('email' => '[email protected]', 'name' => 'Ronny')
121
                )
122
            )
123
        );
124
    }
125
126
}
127