Completed
Push — master ( 6a9705...322f11 )
by
unknown
27:56
created

TransactionHandlerTest::testConstruct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 20
rs 9.6
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::beginTransaction
46
     */
47
    public function testBeginTransaction()
48
    {
49
        $handler = $this->getTransactionHandler();
50
        $this->getDatabaseHandlerMock()
51
            ->expects($this->once())
52
            ->method('beginTransaction');
53
        $this->getContentTypeHandlerMock()
54
            ->expects($this->never())
55
            ->method($this->anything());
56
        $this->getLanguageHandlerMock()
57
            ->expects($this->never())
58
            ->method($this->anything());
59
60
        $handler->beginTransaction();
61
    }
62
63
    /**
64
     * @covers \eZ\Publish\Core\Persistence\Legacy\TransactionHandler::commit
65
     */
66
    public function testCommit()
67
    {
68
        $handler = $this->getTransactionHandler();
69
        $this->getDatabaseHandlerMock()
70
            ->expects($this->once())
71
            ->method('commit');
72
        $this->getContentTypeHandlerMock()
73
            ->expects($this->never())
74
            ->method($this->anything());
75
        $this->getLanguageHandlerMock()
76
            ->expects($this->never())
77
            ->method($this->anything());
78
79
        $handler->commit();
80
    }
81
82
    /**
83
     * @covers \eZ\Publish\Core\Persistence\Legacy\TransactionHandler::commit
84
     */
85 View Code Duplication
    public function testCommitException()
86
    {
87
        $this->expectException(\RuntimeException::class);
88
        $this->expectExceptionMessage('test');
89
90
        $handler = $this->getTransactionHandler();
91
        $this->getDatabaseHandlerMock()
92
            ->expects($this->once())
93
            ->method('commit')
94
            ->will($this->throwException(new Exception('test')));
95
        $this->getContentTypeHandlerMock()
96
            ->expects($this->never())
97
            ->method($this->anything());
98
        $this->getLanguageHandlerMock()
99
            ->expects($this->never())
100
            ->method($this->anything());
101
102
        $handler->commit();
103
    }
104
105
    /**
106
     * @covers \eZ\Publish\Core\Persistence\Legacy\TransactionHandler::rollback
107
     */
108
    public function testRollback()
109
    {
110
        $handler = $this->getTransactionHandler();
111
        $this->getDatabaseHandlerMock()
112
            ->expects($this->once())
113
            ->method('rollback');
114
        $this->getContentTypeHandlerMock()
115
            ->expects($this->once())
116
            ->method('clearCache');
117
        $this->getLanguageHandlerMock()
118
            ->expects($this->once())
119
            ->method('clearCache');
120
121
        $handler->rollback();
122
    }
123
124
    /**
125
     * @covers \eZ\Publish\Core\Persistence\Legacy\TransactionHandler::rollback
126
     */
127 View Code Duplication
    public function testRollbackException()
128
    {
129
        $this->expectException(\RuntimeException::class);
130
        $this->expectExceptionMessage('test');
131
132
        $handler = $this->getTransactionHandler();
133
        $this->getDatabaseHandlerMock()
134
            ->expects($this->once())
135
            ->method('rollback')
136
            ->will($this->throwException(new Exception('test')));
137
        $this->getContentTypeHandlerMock()
138
            ->expects($this->never())
139
            ->method($this->anything());
140
        $this->getLanguageHandlerMock()
141
            ->expects($this->never())
142
            ->method($this->anything());
143
144
        $handler->rollback();
145
    }
146
147
    /**
148
     * Returns a mock object for the Content Gateway.
149
     *
150
     * @return \eZ\Publish\Core\Persistence\Legacy\TransactionHandler
151
     */
152
    protected function getTransactionHandler()
153
    {
154
        if (!isset($this->transactionHandler)) {
155
            $this->transactionHandler = new TransactionHandler(
156
                $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...
157
                $this->getContentTypeHandlerMock(),
158
                $this->getLanguageHandlerMock()
159
            );
160
        }
161
162
        return $this->transactionHandler;
163
    }
164
165
    /**
166
     * Returns a mock object for the Content Gateway.
167
     *
168
     * @return \eZ\Publish\Core\Persistence\Database\DatabaseHandler|\PHPUnit\Framework\MockObject\MockObject
169
     */
170
    protected function getDatabaseHandlerMock()
171
    {
172
        if (!isset($this->dbHandlerMock)) {
173
            $this->dbHandlerMock = $this->getMockForAbstractClass(DatabaseHandler::class);
174
        }
175
176
        return $this->dbHandlerMock;
177
    }
178
179
    /**
180
     * Returns a mock object for the Content Type Handler.
181
     *
182
     * @return \eZ\Publish\Core\Persistence\Legacy\Content\Type\MemoryCachingHandler|\PHPUnit\Framework\MockObject\MockObject
183
     */
184
    protected function getContentTypeHandlerMock()
185
    {
186
        if (!isset($this->contentTypeHandlerMock)) {
187
            $this->contentTypeHandlerMock = $this->createMock(MemoryCachingHandler::class);
188
        }
189
190
        return $this->contentTypeHandlerMock;
191
    }
192
193
    /**
194
     * Returns a mock object for the Content Language Gateway.
195
     *
196
     * @return \eZ\Publish\Core\Persistence\Legacy\Content\Language\CachingHandler|\PHPUnit\Framework\MockObject\MockObject
197
     */
198
    protected function getLanguageHandlerMock()
199
    {
200
        if (!isset($this->languageHandlerMock)) {
201
            $this->languageHandlerMock = $this->createMock(CachingHandler::class);
202
        }
203
204
        return $this->languageHandlerMock;
205
    }
206
}
207