Skip to content

Commit 5d3d0b5

Browse files
authored
[TASK] Trait for database test, base class for web tests (#264)
The trait DatabaseTestTrait and the class abstractWebTest now are available to be used in other packages as well, reducing code duplication. Also whitelist two more static calls in the PHPMD configuration and break a long line in the PHPMD configuration file.
1 parent a503b54 commit 5d3d0b5

File tree

12 files changed

+101
-43
lines changed

12 files changed

+101
-43
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ This project adheres to [Semantic Versioning](https://semver.org/).
77
## x.y.z
88

99
### Added
10+
- Trait for database tests and abstract web test (#264)
1011
- Repository methods for querying subscriptions (#253)
1112
- Bidirectional m:n association Subscribers/SubscriberLists (#254)
1213

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace PhpList\PhpList4\TestingSupport;
5+
6+
use PhpList\PhpList4\Core\Bootstrap;
7+
use PhpList\PhpList4\Core\Environment;
8+
use Symfony\Bundle\FrameworkBundle\Client;
9+
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
10+
11+
/**
12+
* Base class for integration tests for controllers.
13+
*
14+
* If you have your own setUp method, make sure to call $this->setUpWebTest() first thing in your setUp method.
15+
*
16+
* @author Oliver Klee <[email protected]>
17+
*/
18+
abstract class AbstractWebTest extends WebTestCase
19+
{
20+
/**
21+
* @var Client
22+
*/
23+
protected $client = null;
24+
25+
protected function setUp()
26+
{
27+
$this->setUpWebTest();
28+
}
29+
30+
protected function setUpWebTest()
31+
{
32+
// This makes sure that all DateTime instances use the same time zone, thus making the dates in the
33+
// JSON provided by the REST API easier to test.
34+
date_default_timezone_set('UTC');
35+
36+
Bootstrap::getInstance()->setEnvironment(Environment::TESTING)->configure();
37+
38+
$this->client = static::createClient(['environment' => Environment::TESTING]);
39+
}
40+
}

Tests/Integration/AbstractDatabaseTest.php renamed to Classes/TestingSupport/Traits/DatabaseTestTrait.php

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22
declare(strict_types=1);
33

4-
namespace PhpList\PhpList4\Tests\Integration;
4+
namespace PhpList\PhpList4\TestingSupport\Traits;
55

66
use Doctrine\ORM\EntityManagerInterface;
77
use PhpList\PhpList4\Core\Bootstrap;
@@ -11,17 +11,18 @@
1111
use PHPUnit\DbUnit\Operation\Factory;
1212
use PHPUnit\DbUnit\Operation\Operation;
1313
use PHPUnit\DbUnit\TestCaseTrait;
14-
use PHPUnit\Framework\TestCase;
1514
use Symfony\Component\DependencyInjection\ContainerInterface;
1615

1716
/**
18-
* This is the base class for integration tests that use database records.
17+
* This is a trait for integration tests that use database records.
1918
*
20-
* Make sure to call parent::setUp() first thing in your setUp method.
19+
* If you have your own setUp method, make sure to call $this->setUpDatabaseTest() first thing in your setUp method.
20+
*
21+
* And if you have your own tearDown method, call $this->tearDownDatabaseTest() first thing in your tearDown method.
2122
*
2223
* @author Oliver Klee <[email protected]>
2324
*/
24-
abstract class AbstractDatabaseTest extends TestCase
25+
trait DatabaseTestTrait
2526
{
2627
use TestCaseTrait;
2728

@@ -56,6 +57,11 @@ abstract class AbstractDatabaseTest extends TestCase
5657
protected $container = null;
5758

5859
protected function setUp()
60+
{
61+
$this->setUpDatabaseTest();
62+
}
63+
64+
protected function setUpDatabaseTest()
5965
{
6066
$this->initializeDatabaseTester();
6167
$this->bootstrap = Bootstrap::getInstance()->setEnvironment(Environment::TESTING)->configure();
@@ -78,6 +84,11 @@ protected function initializeDatabaseTester()
7884
}
7985

8086
protected function tearDown()
87+
{
88+
$this->tearDownDatabaseTest();
89+
}
90+
91+
protected function tearDownDatabaseTest()
8192
{
8293
$this->entityManager->close();
8394
$this->getDatabaseTester()->setTearDownOperation($this->getTearDownOperation());
@@ -159,6 +170,6 @@ protected function applyDatabaseChanges()
159170
*/
160171
protected function touchDatabaseTable(string $tableName)
161172
{
162-
$this->getDataSet()->addTable($tableName, __DIR__ . '/Fixtures/TouchTable.csv');
173+
$this->getDataSet()->addTable($tableName, __DIR__ . '/../Fixtures/TouchTable.csv');
163174
}
164175
}

Configuration/PHPMD/rules.xml

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,16 @@
88
<rule ref="rulesets/cleancode.xml/BooleanArgumentFlag"/>
99
<rule ref="rulesets/cleancode.xml/StaticAccess">
1010
<properties>
11-
<property name="exceptions"
12-
value="\Doctrine\ORM\EntityManager,\Doctrine\ORM\Tools\Setup,\Symfony\Component\Debug\Debug,PhpList\PhpList4\Core\Environment,Symfony\Component\Yaml\Yaml,\FOS\RestBundle\View\View"/>
11+
<property name="exceptions" value="
12+
\Doctrine\ORM\EntityManager,
13+
\Doctrine\ORM\Tools\Setup,
14+
\FOS\RestBundle\View\View,
15+
\PhpList\PhpList4\Core\Bootstrap,
16+
\PhpList\PhpList4\Core\Environment,
17+
\PHPUnit\DbUnit\Operation\Factory,
18+
\Symfony\Component\Debug\Debug,
19+
\Symfony\Component\Yaml\Yaml,
20+
"/>
1321
</properties>
1422
</rule>
1523

Tests/Integration/Domain/Repository/Identity/AdministratorRepositoryTest.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,18 @@
55

66
use PhpList\PhpList4\Domain\Model\Identity\Administrator;
77
use PhpList\PhpList4\Domain\Repository\Identity\AdministratorRepository;
8+
use PhpList\PhpList4\TestingSupport\Traits\DatabaseTestTrait;
89
use PhpList\PhpList4\TestingSupport\Traits\SimilarDatesAssertionTrait;
9-
use PhpList\PhpList4\Tests\Integration\AbstractDatabaseTest;
10+
use PHPUnit\Framework\TestCase;
1011

1112
/**
1213
* Testcase.
1314
*
1415
* @author Oliver Klee <[email protected]>
1516
*/
16-
class AdministratorRepositoryTest extends AbstractDatabaseTest
17+
class AdministratorRepositoryTest extends TestCase
1718
{
19+
use DatabaseTestTrait;
1820
use SimilarDatesAssertionTrait;
1921

2022
/**
@@ -29,7 +31,7 @@ class AdministratorRepositoryTest extends AbstractDatabaseTest
2931

3032
protected function setUp()
3133
{
32-
parent::setUp();
34+
$this->setUpDatabaseTest();
3335

3436
$this->subject = $this->container->get(AdministratorRepository::class);
3537
}

Tests/Integration/Domain/Repository/Identity/AdministratorTokenRepositoryTest.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,18 @@
88
use PhpList\PhpList4\Domain\Model\Identity\AdministratorToken;
99
use PhpList\PhpList4\Domain\Repository\Identity\AdministratorRepository;
1010
use PhpList\PhpList4\Domain\Repository\Identity\AdministratorTokenRepository;
11+
use PhpList\PhpList4\TestingSupport\Traits\DatabaseTestTrait;
1112
use PhpList\PhpList4\TestingSupport\Traits\SimilarDatesAssertionTrait;
12-
use PhpList\PhpList4\Tests\Integration\AbstractDatabaseTest;
13+
use PHPUnit\Framework\TestCase;
1314

1415
/**
1516
* Testcase.
1617
*
1718
* @author Oliver Klee <[email protected]>
1819
*/
19-
class AdministratorTokenRepositoryTest extends AbstractDatabaseTest
20+
class AdministratorTokenRepositoryTest extends TestCase
2021
{
22+
use DatabaseTestTrait;
2123
use SimilarDatesAssertionTrait;
2224

2325
/**
@@ -37,7 +39,7 @@ class AdministratorTokenRepositoryTest extends AbstractDatabaseTest
3739

3840
protected function setUp()
3941
{
40-
parent::setUp();
42+
$this->setUpDatabaseTest();
4143

4244
$this->subject = $this->container->get(AdministratorTokenRepository::class);
4345
}

Tests/Integration/Domain/Repository/Messaging/SubscriberListRepositoryTest.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,18 @@
1010
use PhpList\PhpList4\Domain\Repository\Identity\AdministratorRepository;
1111
use PhpList\PhpList4\Domain\Repository\Messaging\SubscriberListRepository;
1212
use PhpList\PhpList4\Domain\Repository\Subscription\SubscriberRepository;
13+
use PhpList\PhpList4\TestingSupport\Traits\DatabaseTestTrait;
1314
use PhpList\PhpList4\TestingSupport\Traits\SimilarDatesAssertionTrait;
14-
use PhpList\PhpList4\Tests\Integration\AbstractDatabaseTest;
15+
use PHPUnit\Framework\TestCase;
1516

1617
/**
1718
* Testcase.
1819
*
1920
* @author Oliver Klee <[email protected]>
2021
*/
21-
class SubscriberListRepositoryTest extends AbstractDatabaseTest
22+
class SubscriberListRepositoryTest extends TestCase
2223
{
24+
use DatabaseTestTrait;
2325
use SimilarDatesAssertionTrait;
2426

2527
/**
@@ -59,7 +61,7 @@ class SubscriberListRepositoryTest extends AbstractDatabaseTest
5961

6062
protected function setUp()
6163
{
62-
parent::setUp();
64+
$this->setUpDatabaseTest();
6365

6466
$this->subject = $this->container->get(SubscriberListRepository::class);
6567
$this->administratorRepository = $this->container->get(AdministratorRepository::class);

Tests/Integration/Domain/Repository/Subscription/SubscriberRepositoryTest.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,18 @@
88
use PhpList\PhpList4\Domain\Model\Subscription\Subscription;
99
use PhpList\PhpList4\Domain\Repository\Messaging\SubscriberListRepository;
1010
use PhpList\PhpList4\Domain\Repository\Subscription\SubscriberRepository;
11+
use PhpList\PhpList4\TestingSupport\Traits\DatabaseTestTrait;
1112
use PhpList\PhpList4\TestingSupport\Traits\SimilarDatesAssertionTrait;
12-
use PhpList\PhpList4\Tests\Integration\AbstractDatabaseTest;
13+
use PHPUnit\Framework\TestCase;
1314

1415
/**
1516
* Testcase.
1617
*
1718
* @author Oliver Klee <[email protected]>
1819
*/
19-
class SubscriberRepositoryTest extends AbstractDatabaseTest
20+
class SubscriberRepositoryTest extends TestCase
2021
{
22+
use DatabaseTestTrait;
2123
use SimilarDatesAssertionTrait;
2224

2325
/**
@@ -52,7 +54,7 @@ class SubscriberRepositoryTest extends AbstractDatabaseTest
5254

5355
protected function setUp()
5456
{
55-
parent::setUp();
57+
$this->setUpDatabaseTest();
5658

5759
$this->subject = $this->container->get(SubscriberRepository::class);
5860
$this->subscriberListRepository = $this->container->get(SubscriberListRepository::class);

Tests/Integration/Domain/Repository/Subscription/SubscriptionRepositoryTest.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,18 @@
1010
use PhpList\PhpList4\Domain\Repository\Messaging\SubscriberListRepository;
1111
use PhpList\PhpList4\Domain\Repository\Subscription\SubscriberRepository;
1212
use PhpList\PhpList4\Domain\Repository\Subscription\SubscriptionRepository;
13+
use PhpList\PhpList4\TestingSupport\Traits\DatabaseTestTrait;
1314
use PhpList\PhpList4\TestingSupport\Traits\SimilarDatesAssertionTrait;
14-
use PhpList\PhpList4\Tests\Integration\AbstractDatabaseTest;
15+
use PHPUnit\Framework\TestCase;
1516

1617
/**
1718
* Testcase.
1819
*
1920
* @author Oliver Klee <[email protected]>
2021
*/
21-
class SubscriptionRepositoryTest extends AbstractDatabaseTest
22+
class SubscriptionRepositoryTest extends TestCase
2223
{
24+
use DatabaseTestTrait;
2325
use SimilarDatesAssertionTrait;
2426

2527
/**
@@ -59,7 +61,7 @@ class SubscriptionRepositoryTest extends AbstractDatabaseTest
5961

6062
protected function setUp()
6163
{
62-
parent::setUp();
64+
$this->setUpDatabaseTest();
6365

6466
$this->subject = $this->container->get(SubscriptionRepository::class);
6567

0 commit comments

Comments
 (0)