Completed
Push — in-memory-cache2 ( 8ddfe2...65ff47 )
by André
17:51
created

TransactionHandlerTest::getLanguageHandlerMock()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 0
dl 0
loc 8
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * File contains: eZ\Publish\Core\Persistence\Legacy\Tests\TransactionHandlerTest class.
5
 *
6
 * @copyright Copyright (C) eZ Systems AS. All rights reserved.
7
 * @license For full copyright and license information view LICENSE file distributed with this source code.
8
 */
9
namespace eZ\Publish\Core\Persistence\Legacy\Tests;
10
11
use eZ\Publish\Core\Persistence\Legacy\TransactionHandler;
12
use eZ\Publish\Core\Persistence\Legacy\Content\Language\CachingHandler;
13
use eZ\Publish\Core\Persistence\Legacy\Content\Type\MemoryCachingHandler;
14
use eZ\Publish\Core\Persistence\Database\DatabaseHandler;
15
use Exception;
16
17
/**
18
 * Test case for TransactionHandler.
19
 */
20
class TransactionHandlerTest extends \PHPUnit\Framework\TestCase
21
{
22
    /**
23
     * Transaction handler to test.
24
     *
25
     * @var \eZ\Publish\Core\Persistence\Legacy\TransactionHandler
26
     */
27
    protected $transactionHandler;
28
29
    /**
30
     * @var \eZ\Publish\Core\Persistence\Database\DatabaseHandler|\PHPUnit\Framework\MockObject\MockObject
31
     */
32
    protected $dbHandlerMock;
33
34
    /**
35
     * @var \eZ\Publish\SPI\Persistence\Content\Type\Handler|\PHPUnit\Framework\MockObject\MockObject
36
     */
37
    protected $contentTypeHandlerMock;
38
39
    /**
40
     * @var \eZ\Publish\SPI\Persistence\Content\Language\Handler|\PHPUnit\Framework\MockObject\MockObject
41
     */
42
    protected $languageHandlerMock;
43
44
    /**
45
     * @covers \eZ\Publish\Core\Persistence\Legacy\TransactionHandler::__construct
46
     */
47
    public function testConstruct()
48
    {
49
        $handler = $this->getTransactionHandler();
50
51
        $this->assertAttributeSame(
0 ignored issues
show
Deprecated Code introduced by
The method PHPUnit\Framework\Assert::assertAttributeSame() has been deprecated with message: https://github.com/sebastianbergmann/phpunit/issues/3338

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...
52
            $this->getDatabaseHandlerMock(),
53
            'dbHandler',
54
            $handler
55
        );
56
        $this->assertAttributeSame(
0 ignored issues
show
Deprecated Code introduced by
The method PHPUnit\Framework\Assert::assertAttributeSame() has been deprecated with message: https://github.com/sebastianbergmann/phpunit/issues/3338

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...
57
            $this->getContentTypeHandlerMock(),
58
            'contentTypeHandler',
59
            $handler
60
        );
61
        $this->assertAttributeSame(
0 ignored issues
show
Deprecated Code introduced by
The method PHPUnit\Framework\Assert::assertAttributeSame() has been deprecated with message: https://github.com/sebastianbergmann/phpunit/issues/3338

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...
62
            $this->getLanguageHandlerMock(),
63
            'languageHandler',
64
            $handler
65
        );
66
    }
67
68
    /**
69
     * @covers \eZ\Publish\Core\Persistence\Legacy\TransactionHandler::beginTransaction
70
     */
71
    public function testBeginTransaction()
72
    {
73
        $handler = $this->getTransactionHandler();
74
        $this->getDatabaseHandlerMock()
75
            ->expects($this->once())
76
            ->method('beginTransaction');
77
        $this->getContentTypeHandlerMock()
78
            ->expects($this->never())
79
            ->method($this->anything());
80
        $this->getLanguageHandlerMock()
81
            ->expects($this->never())
82
            ->method($this->anything());
83
84
        $handler->beginTransaction();
85
    }
86
87
    /**
88
     * @covers \eZ\Publish\Core\Persistence\Legacy\TransactionHandler::commit
89
     */
90
    public function testCommit()
91
    {
92
        $handler = $this->getTransactionHandler();
93
        $this->getDatabaseHandlerMock()
94
            ->expects($this->once())
95
            ->method('commit');
96
        $this->getContentTypeHandlerMock()
97
            ->expects($this->never())
98
            ->method($this->anything());
99
        $this->getLanguageHandlerMock()
100
            ->expects($this->never())
101
            ->method($this->anything());
102
103
        $handler->commit();
104
    }
105
106
    /**
107
     * @covers \eZ\Publish\Core\Persistence\Legacy\TransactionHandler::commit
108
     *
109
     * @expectedException \RuntimeException
110
     * @expectedExceptionMessage test
111
     */
112 View Code Duplication
    public function testCommitException()
113
    {
114
        $handler = $this->getTransactionHandler();
115
        $this->getDatabaseHandlerMock()
116
            ->expects($this->once())
117
            ->method('commit')
118
            ->will($this->throwException(new Exception('test')));
119
        $this->getContentTypeHandlerMock()
120
            ->expects($this->never())
121
            ->method($this->anything());
122
        $this->getLanguageHandlerMock()
123
            ->expects($this->never())
124
            ->method($this->anything());
125
126
        $handler->commit();
127
    }
128
129
    /**
130
     * @covers \eZ\Publish\Core\Persistence\Legacy\TransactionHandler::rollback
131
     */
132
    public function testRollback()
133
    {
134
        $handler = $this->getTransactionHandler();
135
        $this->getDatabaseHandlerMock()
136
            ->expects($this->once())
137
            ->method('rollback');
138
        $this->getContentTypeHandlerMock()
139
            ->expects($this->once())
140
            ->method('clearCache');
141
        $this->getLanguageHandlerMock()
142
            ->expects($this->once())
143
            ->method('clearCache');
144
145
        $handler->rollback();
146
    }
147
148
    /**
149
     * @covers \eZ\Publish\Core\Persistence\Legacy\TransactionHandler::rollback
150
     *
151
     * @expectedException \RuntimeException
152
     * @expectedExceptionMessage test
153
     */
154 View Code Duplication
    public function testRollbackException()
155
    {
156
        $handler = $this->getTransactionHandler();
157
        $this->getDatabaseHandlerMock()
158
            ->expects($this->once())
159
            ->method('rollback')
160
            ->will($this->throwException(new Exception('test')));
161
        $this->getContentTypeHandlerMock()
162
            ->expects($this->never())
163
            ->method($this->anything());
164
        $this->getLanguageHandlerMock()
165
            ->expects($this->never())
166
            ->method($this->anything());
167
168
        $handler->rollback();
169
    }
170
171
    /**
172
     * Returns a mock object for the Content Gateway.
173
     *
174
     * @return \eZ\Publish\Core\Persistence\Legacy\TransactionHandler
175
     */
176
    protected function getTransactionHandler()
177
    {
178
        if (!isset($this->transactionHandler)) {
179
            $this->transactionHandler = new TransactionHandler(
180
                $this->getDatabaseHandlerMock(),
0 ignored issues
show
Bug introduced by
It seems like $this->getDatabaseHandlerMock() targeting eZ\Publish\Core\Persiste...etDatabaseHandlerMock() can also be of type object<PHPUnit\Framework\MockObject\MockObject>; however, eZ\Publish\Core\Persiste...nHandler::__construct() does only seem to accept object<eZ\Publish\Core\P...tabase\DatabaseHandler>, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
181
                $this->getContentTypeHandlerMock(),
182
                $this->getLanguageHandlerMock()
183
            );
184
        }
185
186
        return $this->transactionHandler;
187
    }
188
189
    /**
190
     * Returns a mock object for the Content Gateway.
191
     *
192
     * @return \eZ\Publish\Core\Persistence\Database\DatabaseHandler|\PHPUnit\Framework\MockObject\MockObject
193
     */
194
    protected function getDatabaseHandlerMock()
195
    {
196
        if (!isset($this->dbHandlerMock)) {
197
            $this->dbHandlerMock = $this->getMockForAbstractClass(DatabaseHandler::class);
198
        }
199
200
        return $this->dbHandlerMock;
201
    }
202
203
    /**
204
     * Returns a mock object for the Content Type Handler.
205
     *
206
     * @return \eZ\Publish\Core\Persistence\Legacy\Content\Type\MemoryCachingHandler|\PHPUnit\Framework\MockObject\MockObject
207
     */
208
    protected function getContentTypeHandlerMock()
209
    {
210
        if (!isset($this->contentTypeHandlerMock)) {
211
            $this->contentTypeHandlerMock = $this->createMock(MemoryCachingHandler::class);
212
        }
213
214
        return $this->contentTypeHandlerMock;
215
    }
216
217
    /**
218
     * Returns a mock object for the Content Language Gateway.
219
     *
220
     * @return \eZ\Publish\Core\Persistence\Legacy\Content\Language\CachingHandler|\PHPUnit\Framework\MockObject\MockObject
221
     */
222
    protected function getLanguageHandlerMock()
223
    {
224
        if (!isset($this->languageHandlerMock)) {
225
            $this->languageHandlerMock = $this->createMock(CachingHandler::class);
226
        }
227
228
        return $this->languageHandlerMock;
229
    }
230
}
231