[TASK] Doctrine: migrate ext:recycler/CleanerTask
[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->getMock(CleanerTask::class, array('dummy'), array(), '', false);
43 }
44
45 /**
46 * @test
47 */
48 public function getPeriodCanBeSet()
49 {
50 $period = 14;
51 $this->subject->setPeriod($period);
52
53 $this->assertEquals($period, $this->subject->getPeriod());
54 }
55
56 /**
57 * @test
58 */
59 public function getTcaTablesCanBeSet()
60 {
61 $tables = array('pages', 'tt_content');
62 $this->subject->setTcaTables($tables);
63
64 $this->assertEquals($tables, $this->subject->getTcaTables());
65 }
66
67 /**
68 * @test
69 */
70 public function taskBuildsCorrectQuery()
71 {
72 $GLOBALS['TCA']['pages']['ctrl']['delete'] = 'deleted';
73 $GLOBALS['TCA']['pages']['ctrl']['tstamp'] = 'tstamp';
74
75 /** @var \PHPUnit_Framework_MockObject_MockObject|CleanerTask $subject */
76 $subject = $this->getMock(CleanerTask::class, array('getPeriodAsTimestamp'), array(), '', false);
77 $subject->setTcaTables(['pages']);
78 $subject->expects($this->once())->method('getPeriodAsTimestamp')->willReturn(400);
79
80 /** @var Connection|ObjectProphecy $connection */
81 $connection = $this->prophesize(Connection::class);
82 $connection->getDatabasePlatform()->willReturn(new MockPlatform());
83 $connection->getExpressionBuilder()->willReturn(new ExpressionBuilder($connection->reveal()));
84 $connection->quoteIdentifier(Argument::cetera())->willReturnArgument(0);
85
86 $queryBuilder = GeneralUtility::makeInstance(
87 QueryBuilder::class,
88 $connection->reveal(),
89 null,
90 new \Doctrine\DBAL\Query\QueryBuilder($connection->reveal())
91 );
92
93 $connectionPool = $this->prophesize(ConnectionPool::class);
94 $connectionPool->getQueryBuilderForTable('pages')->willReturn($queryBuilder);
95 GeneralUtility::addInstance(ConnectionPool::class, $connectionPool->reveal());
96
97 $connection->executeUpdate('DELETE FROM pages WHERE (deleted = 1) AND (tstamp < 400)', Argument::cetera())
98 ->shouldBeCalled()
99 ->willReturn(1);
100 $this->assertTrue($subject->execute());
101 }
102
103 /**
104 * @test
105 */
106 public function taskFailsOnError()
107 {
108 $GLOBALS['TCA']['pages']['ctrl']['delete'] = 'deleted';
109 $GLOBALS['TCA']['pages']['ctrl']['tstamp'] = 'tstamp';
110
111 $this->subject->setTcaTables(['pages']);
112
113 /** @var Connection|ObjectProphecy $connection */
114 $connection = $this->prophesize(Connection::class);
115 $connection->getDatabasePlatform()->willReturn(new MockPlatform());
116 $connection->getExpressionBuilder()->willReturn(new ExpressionBuilder($connection->reveal()));
117 $connection->quoteIdentifier(Argument::cetera())->willReturnArgument(0);
118
119 $queryBuilder = GeneralUtility::makeInstance(
120 QueryBuilder::class,
121 $connection->reveal(),
122 null,
123 new \Doctrine\DBAL\Query\QueryBuilder($connection->reveal())
124 );
125
126 $connectionPool = $this->prophesize(ConnectionPool::class);
127 $connectionPool->getQueryBuilderForTable('pages')->willReturn($queryBuilder);
128 GeneralUtility::addInstance(ConnectionPool::class, $connectionPool->reveal());
129
130 $connection->executeUpdate(Argument::cetera())
131 ->shouldBeCalled()
132 ->willThrow(new \Doctrine\DBAL\DBALException());
133
134 $this->assertFalse($this->subject->execute());
135 }
136 }