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