Completed
Pull Request — master (#60)
by Eric
41:48 queued 34:12
created

TranslatableFactoryTest::createLocaleContextMock()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
/*
4
 * This file is part of the Lug package.
5
 *
6
 * (c) Eric GELOEN <[email protected]>
7
 *
8
 * For the full copyright and license information, please read the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Lug\Component\Translation\Tests\Factory;
13
14
use Lug\Component\Resource\Factory\Factory;
15
use Lug\Component\Resource\Factory\FactoryInterface;
16
use Lug\Component\Resource\Model\ResourceInterface;
17
use Lug\Component\Translation\Context\LocaleContextInterface;
18
use Lug\Component\Translation\Factory\TranslatableFactory;
19
use Lug\Component\Translation\Model\TranslatableInterface;
20
use Lug\Component\Translation\Model\TranslatableTrait;
21
use Lug\Component\Translation\Model\TranslationInterface;
22
use Lug\Component\Translation\Model\TranslationTrait;
23
use Symfony\Component\PropertyAccess\PropertyAccess;
24
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
25
26
/**
27
 * @author GeLo <[email protected]>
28
 */
29
class TranslatableFactoryTest extends \PHPUnit_Framework_TestCase
30
{
31
    /**
32
     * @var TranslatableFactory
33
     */
34
    private $factory;
35
36
    /**
37
     * @var \PHPUnit_Framework_MockObject_MockObject|ResourceInterface
38
     */
39
    private $resource;
40
41
    /**
42
     * @var \PHPUnit_Framework_MockObject_MockObject|PropertyAccessorInterface
43
     */
44
    private $propertyAccessor;
45
46
    /**
47
     * @var \PHPUnit_Framework_MockObject_MockObject|LocaleContextInterface
48
     */
49
    private $localeContext;
50
51
    /**
52
     * @var \PHPUnit_Framework_MockObject_MockObject|FactoryInterface
53
     */
54
    private $translationFactory;
55
56
    /**
57
     * {@inheritdoc}
58
     */
59
    protected function setUp()
60
    {
61
        $this->resource = $this->createResourceMock();
62
        $this->propertyAccessor = PropertyAccess::createPropertyAccessor();
63
        $this->localeContext = $this->createLocaleContextMock();
64
        $this->translationFactory = $this->createFactoryMock();
65
66
        $this->factory = new TranslatableFactory(
67
            $this->resource,
68
            $this->propertyAccessor,
69
            $this->localeContext,
70
            $this->translationFactory
71
        );
72
    }
73
74
    public function testInheritance()
75
    {
76
        $this->assertInstanceOf(Factory::class, $this->factory);
77
    }
78
79
    public function testCreateWithOptions()
80
    {
81
        $this->resource
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Lug\Component\Resource\Model\ResourceInterface.

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...
82
            ->expects($this->once())
83
            ->method('getModel')
84
            ->will($this->returnValue($translatableClass = TranslatableTest::class));
85
86
        $this->localeContext
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Lug\Component\Translatio...\LocaleContextInterface.

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...
87
            ->expects($this->once())
88
            ->method('getLocales')
89
            ->will($this->returnValue($locales = ['fr', 'es']));
90
91
        $this->localeContext
92
            ->expects($this->once())
93
            ->method('getFallbackLocale')
94
            ->will($this->returnValue($fallbackLocale = 'en'));
95
96
        $translatable = $this->factory->create(['name' => $name = 'foo']);
97
98
        $this->assertInstanceOf($translatableClass, $translatable);
99
        $this->assertSame($name, $translatable->getName());
100
        $this->assertSame($locales, $translatable->getLocales());
101
        $this->assertSame($fallbackLocale, $translatable->getFallbackLocale());
102
        $this->assertSame($this->translationFactory, $translatable->getTranslationFactory());
103
    }
104
105
    public function testCreateWithoutOptions()
106
    {
107
        $this->resource
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Lug\Component\Resource\Model\ResourceInterface.

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...
108
            ->expects($this->once())
109
            ->method('getModel')
110
            ->will($this->returnValue($translatableClass = TranslatableTest::class));
111
112
        $this->localeContext
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Lug\Component\Translatio...\LocaleContextInterface.

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...
113
            ->expects($this->once())
114
            ->method('getLocales')
115
            ->will($this->returnValue($locales = ['fr', 'es']));
116
117
        $this->localeContext
118
            ->expects($this->once())
119
            ->method('getFallbackLocale')
120
            ->will($this->returnValue($fallbackLocale = 'en'));
121
122
        $translatable = $this->factory->create();
123
124
        $this->assertInstanceOf($translatableClass, $translatable);
125
        $this->assertSame($locales, $translatable->getLocales());
126
        $this->assertSame($fallbackLocale, $translatable->getFallbackLocale());
127
        $this->assertSame($this->translationFactory, $translatable->getTranslationFactory());
128
    }
129
130
    /**
131
     * @return \PHPUnit_Framework_MockObject_MockObject|ResourceInterface
132
     */
133
    private function createResourceMock()
134
    {
135
        return $this->createMock(ResourceInterface::class);
136
    }
137
138
    /**
139
     * @return \PHPUnit_Framework_MockObject_MockObject|LocaleContextInterface
140
     */
141
    private function createLocaleContextMock()
142
    {
143
        return $this->createMock(LocaleContextInterface::class);
144
    }
145
146
    /**
147
     * @return \PHPUnit_Framework_MockObject_MockObject|FactoryInterface
148
     */
149
    private function createFactoryMock()
150
    {
151
        return $this->createMock(FactoryInterface::class);
152
    }
153
}
154
155
/**
156
 * @author GeLo <[email protected]>
157
 */
158
class TranslatableTest implements TranslatableInterface
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
159
{
160
    use TranslatableTrait;
161
162
    /**
163
     * @var string
164
     */
165
    private $name;
166
167
    /**
168
     * @return string
169
     */
170
    public function getName()
171
    {
172
        return $this->name;
173
    }
174
175
    /**
176
     * @param string $name
177
     */
178
    public function setName($name)
179
    {
180
        $this->name = $name;
181
    }
182
}
183
184
/**
185
 * @author GeLo <[email protected]>
186
 */
187
class TranslationTest implements TranslationInterface
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
188
{
189
    use TranslationTrait;
190
}
191