‪TYPO3CMS  9.5
CleanerTaskTest.php
Go to the documentation of this file.
1 <?php
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;
27 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
28 
32 class ‪CleanerTaskTest extends UnitTestCase
33 {
37  protected ‪$subject;
38 
42  protected function ‪setUp()
43  {
44  $this->subject = $this->getMockBuilder(CleanerTask::class)
45  ->setMethods(['dummy'])
46  ->disableOriginalConstructor()
47  ->getMock();
48  }
49 
53  public function ‪getPeriodCanBeSet()
54  {
55  $period = 14;
56  $this->subject->setPeriod($period);
57 
58  $this->assertEquals($period, $this->subject->getPeriod());
59  }
60 
64  public function ‪getTcaTablesCanBeSet()
65  {
66  $tables = ['pages', 'tt_content'];
67  $this->subject->setTcaTables($tables);
68 
69  $this->assertEquals($tables, $this->subject->getTcaTables());
70  }
71 
75  public function ‪taskBuildsCorrectQuery()
76  {
77  ‪$GLOBALS['TCA']['pages']['ctrl']['delete'] = 'deleted';
78  ‪$GLOBALS['TCA']['pages']['ctrl']['tstamp'] = 'tstamp';
79 
81  ‪$subject = $this->getMockBuilder(CleanerTask::class)
82  ->setMethods(['getPeriodAsTimestamp'])
83  ->disableOriginalConstructor()
84  ->getMock();
85  ‪$subject->‪setTcaTables(['pages']);
86  ‪$subject->expects($this->once())->method('getPeriodAsTimestamp')->willReturn(400);
87 
89  $connection = $this->prophesize(Connection::class);
90  $connection->getDatabasePlatform()->willReturn(new ‪MockPlatform());
91  $connection->getExpressionBuilder()->willReturn(new ‪ExpressionBuilder($connection->reveal()));
92  $connection->quoteIdentifier(Argument::cetera())->willReturnArgument(0);
93 
94  // TODO: This should rather be a functional test if we need a query builder
95  // or we should clean up the code itself to not need to mock internal behavior here
96 
97  $statementProphet = $this->prophesize(\Doctrine\DBAL\Driver\Statement::class);
98 
99  $restrictionProphet = $this->prophesize(DefaultRestrictionContainer::class);
100  $restrictionProphet->removeAll()->willReturn($restrictionProphet->reveal());
101 
102  $queryBuilderProphet = $this->prophesize(QueryBuilder::class);
103  $queryBuilderProphet->expr()->willReturn(
104  GeneralUtility::makeInstance(ExpressionBuilder::class, $connection->reveal())
105  );
106  $queryBuilderProphet->getRestrictions()->willReturn($restrictionProphet->reveal());
107  $queryBuilderProphet->createNamedParameter(Argument::cetera())->willReturnArgument(0);
108  $queryBuilderProphet->delete(Argument::cetera())->willReturn($queryBuilderProphet->reveal());
109  $queryBuilderProphet->where(Argument::cetera())->willReturn($queryBuilderProphet->reveal());
110  $queryBuilderProphet->execute()->willReturn($statementProphet->reveal());
111 
112  $connectionPool = $this->prophesize(ConnectionPool::class);
113  $connectionPool->getQueryBuilderForTable('pages')->willReturn($queryBuilderProphet->reveal());
114  GeneralUtility::addInstance(ConnectionPool::class, $connectionPool->reveal());
115 
116  $this->assertTrue(‪$subject->‪execute());
117  }
118 
122  public function ‪taskFailsOnError()
123  {
124  ‪$GLOBALS['TCA']['pages']['ctrl']['delete'] = 'deleted';
125  ‪$GLOBALS['TCA']['pages']['ctrl']['tstamp'] = 'tstamp';
126 
127  $this->subject->setTcaTables(['pages']);
128 
130  $connection = $this->prophesize(Connection::class);
131  $connection->getDatabasePlatform()->willReturn(new ‪MockPlatform());
132  $connection->getExpressionBuilder()->willReturn(new ‪ExpressionBuilder($connection->reveal()));
133  $connection->quoteIdentifier(Argument::cetera())->willReturnArgument(0);
134 
135  // TODO: This should rather be a functional test if we need a query builder
136  // or we should clean up the code itself to not need to mock internal behavior here
137  $queryBuilder = new ‪QueryBuilder(
138  $connection->reveal(),
139  null,
140  new \Doctrine\DBAL\Query\‪QueryBuilder($connection->reveal())
141  );
142 
143  $connectionPool = $this->prophesize(ConnectionPool::class);
144  $connectionPool->getQueryBuilderForTable('pages')->willReturn($queryBuilder);
145  GeneralUtility::addInstance(ConnectionPool::class, $connectionPool->reveal());
146 
147  $connection->executeUpdate(Argument::cetera())
148  ->shouldBeCalled()
149  ->willThrow(new \Doctrine\DBAL\DBALException('testing', 1476122315));
150 
151  $this->assertFalse($this->subject->execute());
152  }
153 }
‪TYPO3\CMS\Core\Database\Query\Expression\ExpressionBuilder
Definition: ExpressionBuilder.php:33
‪TYPO3\CMS\Recycler\Tests\Unit\Task\CleanerTaskTest\getTcaTablesCanBeSet
‪getTcaTablesCanBeSet()
Definition: CleanerTaskTest.php:63
‪TYPO3\CMS\Recycler\Tests\Unit\Task\CleanerTaskTest\getPeriodCanBeSet
‪getPeriodCanBeSet()
Definition: CleanerTaskTest.php:52
‪TYPO3\CMS\Recycler\Task\CleanerTask\setTcaTables
‪setTcaTables($tcaTables=[])
Definition: CleanerTask.php:151
‪TYPO3\CMS\Recycler\Tests\Unit\Task\CleanerTaskTest\taskFailsOnError
‪taskFailsOnError()
Definition: CleanerTaskTest.php:121
‪TYPO3\CMS\Recycler\Task\CleanerTask\execute
‪bool execute()
Definition: CleanerTask.php:42
‪TYPO3\CMS\Recycler\Tests\Unit\Task\CleanerTaskTest\taskBuildsCorrectQuery
‪taskBuildsCorrectQuery()
Definition: CleanerTaskTest.php:74
‪TYPO3\CMS\Core\Database\Query\QueryBuilder
Definition: QueryBuilder.php:47
‪TYPO3\CMS\Recycler\Tests\Unit\Task
Definition: CleanerFieldProviderTest.php:2
‪TYPO3\CMS\Recycler\Task\CleanerTask
Definition: CleanerTask.php:27
‪TYPO3\CMS\Core\Tests\Unit\Database\Mocks\MockPlatform
Definition: MockPlatform.php:21
‪TYPO3\CMS\Recycler\Tests\Unit\Task\CleanerTaskTest\$subject
‪PHPUnit_Framework_MockObject_MockObject CleanerTask $subject
Definition: CleanerTaskTest.php:36
‪TYPO3\CMS\Recycler\Tests\Unit\Task\CleanerTaskTest
Definition: CleanerTaskTest.php:33
‪TYPO3\CMS\Core\Database\Connection
Definition: Connection.php:31
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:5
‪TYPO3\CMS\Core\Database\ConnectionPool
Definition: ConnectionPool.php:44
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:45
‪TYPO3\CMS\Recycler\Tests\Unit\Task\CleanerTaskTest\setUp
‪setUp()
Definition: CleanerTaskTest.php:41
‪TYPO3\CMS\Core\Database\Query\Restriction\DefaultRestrictionContainer
Definition: DefaultRestrictionContainer.php:22