[TASK] Improve duplicate exception code check
[Packages/TYPO3.CMS.git] / typo3 / sysext / recycler / Tests / Unit / Task / CleanerTaskTest.php
1 <?php
2 namespace TYPO3\CMS\Recycler\Tests\Unit\Task;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 use Prophecy\Argument;
18 use Prophecy\Prophecy\ObjectProphecy;
19 use TYPO3\CMS\Core\Database\Connection;
20 use TYPO3\CMS\Core\Database\ConnectionPool;
21 use TYPO3\CMS\Core\Database\Query\Expression\ExpressionBuilder;
22 use TYPO3\CMS\Core\Database\Query\QueryBuilder;
23 use TYPO3\CMS\Core\Tests\Unit\Database\Mocks\MockPlatform;
24 use TYPO3\CMS\Core\Utility\GeneralUtility;
25 use TYPO3\CMS\Recycler\Task\CleanerTask;
26
27 /**
28 * Testcase
29 */
30 class CleanerTaskTest extends \TYPO3\CMS\Core\Tests\UnitTestCase
31 {
32 /**
33 * @var \PHPUnit_Framework_MockObject_MockObject|CleanerTask
34 */
35 protected $subject = null;
36
37 /**
38 * sets up an instance of \TYPO3\CMS\Recycler\Task\CleanerTask
39 */
40 protected function setUp()
41 {
42 $this->subject = $this->getMockBuilder(CleanerTask::class)
43 ->setMethods(['dummy'])
44 ->disableOriginalConstructor()
45 ->getMock();
46 }
47
48 /**
49 * @test
50 */
51 public function getPeriodCanBeSet()
52 {
53 $period = 14;
54 $this->subject->setPeriod($period);
55
56 $this->assertEquals($period, $this->subject->getPeriod());
57 }
58
59 /**
60 * @test
61 */
62 public function getTcaTablesCanBeSet()
63 {
64 $tables = ['pages', 'tt_content'];
65 $this->subject->setTcaTables($tables);
66
67 $this->assertEquals($tables, $this->subject->getTcaTables());
68 }
69
70 /**
71 * @test
72 */
73 public function taskBuildsCorrectQuery()
74 {
75 $GLOBALS['TCA']['pages']['ctrl']['delete'] = 'deleted';
76 $GLOBALS['TCA']['pages']['ctrl']['tstamp'] = 'tstamp';
77
78 /** @var \PHPUnit_Framework_MockObject_MockObject|CleanerTask $subject */
79 $subject = $this->getMockBuilder(CleanerTask::class)
80 ->setMethods(['getPeriodAsTimestamp'])
81 ->disableOriginalConstructor()
82 ->getMock();
83 $subject->setTcaTables(['pages']);
84 $subject->expects($this->once())->method('getPeriodAsTimestamp')->willReturn(400);
85
86 /** @var Connection|ObjectProphecy $connection */
87 $connection = $this->prophesize(Connection::class);
88 $connection->getDatabasePlatform()->willReturn(new MockPlatform());
89 $connection->getExpressionBuilder()->willReturn(new ExpressionBuilder($connection->reveal()));
90 $connection->quoteIdentifier(Argument::cetera())->willReturnArgument(0);
91
92 // TODO: This should rather be a functional test if we need a query builder
93 // or we should clean up the code itself to not need to mock internal behavior here
94 $queryBuilder = new QueryBuilder(
95 $connection->reveal(),
96 null,
97 new \Doctrine\DBAL\Query\QueryBuilder($connection->reveal())
98 );
99
100 $connectionPool = $this->prophesize(ConnectionPool::class);
101 $connectionPool->getQueryBuilderForTable('pages')->willReturn($queryBuilder);
102 GeneralUtility::addInstance(ConnectionPool::class, $connectionPool->reveal());
103
104 $connection->executeUpdate('DELETE FROM pages WHERE (deleted = 1) AND (tstamp < 400)', Argument::cetera())
105 ->shouldBeCalled()
106 ->willReturn(1);
107 $this->assertTrue($subject->execute());
108 }
109
110 /**
111 * @test
112 */
113 public function taskFailsOnError()
114 {
115 $GLOBALS['TCA']['pages']['ctrl']['delete'] = 'deleted';
116 $GLOBALS['TCA']['pages']['ctrl']['tstamp'] = 'tstamp';
117
118 $this->subject->setTcaTables(['pages']);
119
120 /** @var Connection|ObjectProphecy $connection */
121 $connection = $this->prophesize(Connection::class);
122 $connection->getDatabasePlatform()->willReturn(new MockPlatform());
123 $connection->getExpressionBuilder()->willReturn(new ExpressionBuilder($connection->reveal()));
124 $connection->quoteIdentifier(Argument::cetera())->willReturnArgument(0);
125
126 // TODO: This should rather be a functional test if we need a query builder
127 // or we should clean up the code itself to not need to mock internal behavior here
128 $queryBuilder = new QueryBuilder(
129 $connection->reveal(),
130 null,
131 new \Doctrine\DBAL\Query\QueryBuilder($connection->reveal())
132 );
133
134 $connectionPool = $this->prophesize(ConnectionPool::class);
135 $connectionPool->getQueryBuilderForTable('pages')->willReturn($queryBuilder);
136 GeneralUtility::addInstance(ConnectionPool::class, $connectionPool->reveal());
137
138 $connection->executeUpdate(Argument::cetera())
139 ->shouldBeCalled()
140 ->willThrow(new \Doctrine\DBAL\DBALException('testing', 1476122315));
141
142 $this->assertFalse($this->subject->execute());
143 }
144 }