ContentManagerTest::testUpdateDetachesObject()   B
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 26
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 26
rs 8.8571
c 0
b 0
f 0
cc 1
eloc 18
nc 1
nop 0
1
<?php
2
3
/*
4
 * This file is part of the Ivoaz ContentEditable bundle.
5
 *
6
 * (c) Ivo Azirjans <[email protected]>
7
 *
8
 * This source file is subject to the MIT license that is bundled
9
 * with this source code in the file LICENSE.
10
 */
11
12
namespace Ivoaz\Bundle\ContentEditableBundle\Tests\Manager;
13
14
use Doctrine\Common\Persistence\ObjectManager;
15
use Doctrine\Common\Persistence\ObjectRepository;
16
use Ivoaz\Bundle\ContentEditableBundle\Manager\ContentManager;
17
use Ivoaz\Bundle\ContentEditableBundle\Exception\MissingLocaleException;
18
use Ivoaz\Bundle\ContentEditableBundle\Model\Content;
19
use Symfony\Component\HttpFoundation\Request;
20
use Symfony\Component\HttpFoundation\RequestStack;
21
22
class ContentManagerTest extends \PHPUnit_Framework_TestCase
23
{
24
    /**
25
     * @var ObjectManager|\PHPUnit_Framework_MockObject_MockObject
26
     */
27
    private $om;
28
29
    /**
30
     * @var ObjectRepository|\PHPUnit_Framework_MockObject_MockObject
31
     */
32
    private $repository;
33
34
    public function setUp()
35
    {
36
        $this->repository = $this->getMock(ObjectRepository::class);
0 ignored issues
show
Deprecated Code introduced by
The method PHPUnit_Framework_TestCase::getMock() has been deprecated with message: Method deprecated since Release 5.4.0

This method has been deprecated. The supplier of the class has supplied an explanatory message.

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

Loading history...
37
38
        $this->om = $this->getMock(ObjectManager::class);
0 ignored issues
show
Deprecated Code introduced by
The method PHPUnit_Framework_TestCase::getMock() has been deprecated with message: Method deprecated since Release 5.4.0

This method has been deprecated. The supplier of the class has supplied an explanatory message.

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

Loading history...
39
        $this->om->method('getRepository')
40
            ->with(Content::class)
41
            ->willReturn($this->repository);
42
    }
43
44
    public function testUpdateFlushesObject()
45
    {
46
        $manager = new ContentManager($this->om);
47
48
        $content = new Content();
49
50
        $this->om->method('contains')
51
            ->with($content)
52
            ->willReturn(true);
53
54
        $this->om->expects($this->once())
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Doctrine\Common\Persistence\ObjectManager.

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...
55
            ->method('flush')
56
            ->with($content);
57
58
        $manager->update($content);
59
    }
60
61
    public function testUpdateDetachesObject()
62
    {
63
        $manager = new ContentManager($this->om);
64
65
        $content = new Content();
66
        $managedContent = new Content();
67
68
        $this->om->method('contains')
69
            ->with($content)
70
            ->willReturn(false);
71
72
        $this->om->expects($this->at(1))
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Doctrine\Common\Persistence\ObjectManager.

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...
73
            ->method('merge')
74
            ->with($content)
75
            ->willReturn($managedContent);
76
77
        $this->om->expects($this->at(2))
78
            ->method('flush')
79
            ->with($managedContent);
80
81
        $this->om->expects($this->at(3))
82
            ->method('detach')
83
            ->with($managedContent);
84
85
        $manager->update($content);
86
    }
87
88
    public function testGetReturnsFoundContentByNameAndLocale()
89
    {
90
        $manager = new ContentManager($this->om);
91
92
        $expectedContent = new Content();
93
94
        $this->repository->method('findOneBy')
95
            ->with(['name' => 'name', 'locale' => 'en'])
96
            ->willReturn($expectedContent);
97
98
        $content = $manager->get('name', 'en', 'text');
99
100
        $this->assertSame($expectedContent, $content);
101
    }
102
103
    public function testGetCreatesNewContent()
104
    {
105
        $manager = new ContentManager($this->om);
106
107
        $expectedContent = new Content();
108
        $expectedContent->setName('name')
109
            ->setText('text')
110
            ->setLocale('en');
111
112
        $this->repository->method('findOneBy')
113
            ->with(['name' => 'name', 'locale' => 'en'])
114
            ->willReturn(null);
115
116
        $this->om->expects($this->once())
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Doctrine\Common\Persistence\ObjectManager.

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...
117
            ->method('persist')
118
            ->with(
119
                $this->callback(
120
                    function ($value) use ($expectedContent) {
121
                        return $value == $expectedContent;
122
                    }
123
                )
124
            );
125
126
        $this->om->expects($this->once())
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Doctrine\Common\Persistence\ObjectManager.

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...
127
            ->method('flush')
128
            ->with(
129
                $this->callback(
130
                    function ($value) use ($expectedContent) {
131
                        return $value == $expectedContent;
132
                    }
133
                )
134
            );
135
136
        $content = $manager->get('name', 'en', 'text');
137
138
        $this->assertEquals($expectedContent, $content);
139
    }
140
141
    public function testGetUsesNameForDefaultWhenNotSpecified()
142
    {
143
        $manager = new ContentManager($this->om);
144
145
        $expectedContent = new Content();
146
        $expectedContent->setName('name')
147
            ->setText('name')
148
            ->setLocale('en');
149
150
        $this->repository->method('findOneBy')
151
            ->with(['name' => 'name', 'locale' => 'en'])
152
            ->willReturn(null);
153
154
        $content = $manager->get('name', 'en');
155
156
        $this->assertEquals($expectedContent, $content);
157
    }
158
159
    public function testGetDetachesExistingObject()
160
    {
161
        $manager = new ContentManager($this->om);
162
163
        $content = new Content();
164
165
        $this->repository->method('findOneBy')
166
            ->with(['name' => 'name', 'locale' => 'en'])
167
            ->willReturn($content);
168
169
        $this->om->expects($this->once())
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Doctrine\Common\Persistence\ObjectManager.

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...
170
            ->method('detach')
171
            ->with($content);
172
173
        $manager->get('name', 'en', 'text');
174
    }
175
176
    public function testGetDetachesNewObject()
177
    {
178
        $manager = new ContentManager($this->om);
179
180
        $this->repository->method('findOneBy')
181
            ->with(['name' => 'name', 'locale' => 'en'])
182
            ->willReturn(null);
183
184
        $content = new Content();
185
        $content->setName('name')
186
            ->setText('text')
187
            ->setLocale('en');
188
189
        $this->om->expects($this->once())
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Doctrine\Common\Persistence\ObjectManager.

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...
190
            ->method('detach')
191
            ->with(
192
                $this->callback(
193
                    function ($value) use ($content) {
194
                        return $value == $content;
195
                    }
196
                )
197
            );
198
199
        $manager->get('name', 'en', 'text');
200
    }
201
202
    public function testGetGuessesLocaleFromRequest()
203
    {
204
        $requests = new RequestStack();
205
        $request = new Request();
206
        $request->setLocale('en');
207
        $requests->push($request);
208
209
        $manager = new ContentManager($this->om, $requests);
210
211
        $content = $manager->get('name');
212
213
        $this->assertSame('en', $content->getLocale());
214
    }
215
216
    public function testGetThrowsMissingLocaleException()
217
    {
218
        $this->setExpectedException(MissingLocaleException::class);
0 ignored issues
show
Deprecated Code introduced by
The method PHPUnit_Framework_TestCase::setExpectedException() has been deprecated with message: Method deprecated since Release 5.2.0

This method has been deprecated. The supplier of the class has supplied an explanatory message.

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

Loading history...
219
220
        $manager = new ContentManager($this->om);
221
        $manager->get('name');
222
    }
223
}
224