GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

WebTranslateItFileServiceTest::tearDown()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 8
nc 3
nop 0
1
<?php
2
3
namespace Ozean12\WebTranslateItBundle\Tests\Service;
4
5
use Ozean12\WebTranslateItBundle\DTO\ProjectFileDTO;
6
use Ozean12\WebTranslateItBundle\Service\WebTranslateItFileService;
7
use Ozean12\WebTranslateItBundle\Service\WebTranslateItRepository;
8
9
/**
10
 * Class WebTranslateItFileServiceTest
11
 */
12
class WebTranslateItFileServiceTest extends \PHPUnit_Framework_TestCase
13
{
14
    /**
15
     * @var WebTranslateItFileService
16
     */
17
    private $service;
18
19
    /**
20
     * @var WebTranslateItRepository|\PHPUnit_Framework_MockObject_MockObject
21
     */
22
    private $translationRepository;
23
24
    /**
25
     * Set up
26
     */
27
    public function setUp()
28
    {
29
        $this->translationRepository = $this
30
            ->getMockBuilder(WebTranslateItRepository::class)
31
            ->disableOriginalConstructor()
32
            ->getMock()
33
        ;
34
35
        $this->service = new WebTranslateItFileService($this->translationRepository);
36
37
        parent::setUp();
38
    }
39
40
    /**
41
     * Tear down
42
     */
43
    public function tearDown()
44
    {
45
        $testDirectory = $this->getTranslationDirectory();
46
        if (file_exists($testDirectory)) {
47
            $testFile = sprintf('%s/%s', $testDirectory, $this->getProjectFileDTO()->getName());
48
            if (file_exists($testFile)) {
49
                unlink($testFile);
50
            }
51
            rmdir($testDirectory);
52
        }
53
54
        parent::tearDown();
55
    }
56
57
    /**
58
     * Test should be updated with local file not present
59
     */
60
    public function testShouldBeUpdatedWithNoLocalFile()
61
    {
62
        $this->service->prepare($this->getTranslationDirectory());
63
64
        $projectFile = new ProjectFileDTO();
65
        $filePath = sprintf('%s/%s', $this->getTranslationDirectory(), 'some_other_file.yml');
66
67
        $this->assertEquals(true, $this->service->shouldBeUpdated($filePath, $projectFile));
68
    }
69
70
    /**
71
     * Test should be updated
72
     *
73
     * @dataProvider shouldBeUpdatedDataProvider
74
     *
75
     * @param string $localTimestamp
76
     * @param string $remoteTimestamp
77
     * @param string $localContent
78
     * @param string $remoteContent
79
     * @param bool   $expectedResult
80
     * @param string $message
81
     */
82
    public function testShouldBeUpdated(
83
        $localTimestamp,
84
        $remoteTimestamp,
85
        $localContent,
86
        $remoteContent,
87
        $expectedResult,
88
        $message
89
    ) {
90
        $this->service->prepare($this->getTranslationDirectory());
91
        $filePath = sprintf('%s/%s', $this->getTranslationDirectory(), $this->getProjectFileDTO()->getName());
92
        file_put_contents($filePath, $localContent);
93
        touch($filePath, (new \DateTime($localTimestamp))->getTimestamp());
94
95
        $projectFile = new ProjectFileDTO();
96
        $projectFile
97
            ->setUpdatedAt(new \DateTime($remoteTimestamp))
98
            ->setHashFile(sha1($remoteContent))
99
        ;
100
101
        $this->assertEquals($expectedResult, $this->service->shouldBeUpdated($filePath, $projectFile), $message);
102
    }
103
104
    /**
105
     * @return array
106
     */
107
    public function shouldBeUpdatedDataProvider()
108
    {
109
        return [
110
            ['today', 'today', 'hello', 'hello', false, 'Everything equal - skip'],
111
            ['yesterday', 'today', 'hello', 'hello', true, 'Local file is older - pull'],
112
            ['today', 'yesterday', 'hello', 'hello', false, 'Local file is newer - skip'],
113
            ['today', 'today', 'hello', 'hello1', true, 'Hash doesn\'t match - pull'],
114
        ];
115
    }
116
117
    /**
118
     * Test update
119
     */
120
    public function testUpdate()
121
    {
122
        $projectFile = $this->getProjectFileDTO();
123
        $filePath = sprintf('%s/%s', $this->getTranslationDirectory(), $projectFile->getName());
124
        $this->translationRepository->method('pullFile')->willReturn($this->getPullFileContent());
125
        $this->translationRepository->expects($this->once())->method('pullFile')->with($projectFile->getName());
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Ozean12\WebTranslateItBu...ebTranslateItRepository.

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...
126
127
        $this->service->prepare($this->getTranslationDirectory());
128
        $this->service->update($filePath, $projectFile);
129
        $this->assertFileExists($filePath);
130
    }
131
132
    /**
133
     * Test prepare
134
     */
135
    public function testPrepare()
136
    {
137
        $this->service->prepare($this->getTranslationDirectory());
138
        $this->assertFileExists($this->getTranslationDirectory());
139
    }
140
141
    /**
142
     * @return string
143
     */
144
    private function getTranslationDirectory()
145
    {
146
        return __DIR__.'/translations';
147
    }
148
149
    /**
150
     * @return string
151
     */
152
    private function getPullFileContent()
153
    {
154
        return 'hello: Hello';
155
    }
156
157
    /**
158
     * @return ProjectFileDTO
159
     */
160
    private function getProjectFileDTO()
161
    {
162
        $file = new ProjectFileDTO();
163
164
        return $file
165
            ->setId(43)
166
            ->setName('test_messages.yml')
167
        ;
168
    }
169
}
170