Skip to content

Commit 6c1e399

Browse files
authored
Get ImportProjectTaskTest closer to real life (Case 205891) (#59)
Until now, the test tests almost nothing: It doesn't verify results and acts rather as a smoke test. But it also mocks away all the dependencies, and only tests the few lines of the `import` method against empty mocks rather than real services. This commit shows that it is possible to test way more real-life interaction with even less code. Only PackageVersionFetcher and VcsDriverFactory are mocked now, to prevent real network requests. For more test result verification, we should use a tool like Foundry.
1 parent 21035a1 commit 6c1e399

File tree

1 file changed

+17
-46
lines changed

1 file changed

+17
-46
lines changed

src/AppBundle/Tests/ProjectImport/ImportProjectTaskTest.php

Lines changed: 17 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -2,31 +2,37 @@
22

33
namespace AppBundle\Tests\Task;
44

5-
use AppBundle\Entity\Project;
65
use AppBundle\Factory\VcsDriverFactory;
76
use AppBundle\ProjectImport\ImportProjectTask;
87
use AppBundle\ProjectImport\PackageVersionFetcher;
98
use AppBundle\ProjectImport\ProjectProviderInterface;
109
use Doctrine\Common\Collections\ArrayCollection;
1110
use Doctrine\ORM\EntityManagerInterface;
1211
use PHPUnit\Framework\MockObject\MockObject;
13-
use PHPUnit\Framework\TestCase;
1412
use Psr\Log\NullLogger;
13+
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
1514

16-
class ImportProjectTaskTest extends TestCase
15+
class ImportProjectTaskTest extends KernelTestCase
1716
{
1817
/**
1918
* @var ImportProjectTask
2019
*/
2120
private $importProjectTask;
2221

22+
private PackageVersionFetcher&MockObject $packageVersionFetcher;
23+
private VcsDriverFactory&MockObject $vcsDriverFactory;
24+
2325
protected function setUp(): void
2426
{
27+
parent::setUp();
28+
self::bootKernel();
29+
$this->vcsDriverFactory = $this->createMock(VcsDriverFactory::class);
30+
$this->packageVersionFetcher = $this->createMock(PackageVersionFetcher::class);
2531
$this->importProjectTask = new ImportProjectTask(
26-
$this->getEntityManagerMock(),
27-
$this->getProjectProviderMock(),
28-
$this->getPackageVersionFetcherMock(),
29-
$this->createMock(VcsDriverFactory::class),
32+
self::$container->get(EntityManagerInterface::class),
33+
self::$container->get(ProjectProviderInterface::class),
34+
$this->packageVersionFetcher,
35+
$this->vcsDriverFactory,
3036
new NullLogger()
3137
);
3238
}
@@ -36,45 +42,10 @@ protected function setUp(): void
3642
*/
3743
public function import()
3844
{
39-
$this->assertTrue($this->importProjectTask->run('https://foo.git'));
40-
}
41-
42-
/**
43-
* @return EntityManagerInterface|MockObject
44-
*/
45-
private function getEntityManagerMock()
46-
{
47-
$entityManagerMock = $this->createMock(EntityManagerInterface::class);
48-
$entityManagerMock
49-
->method('flush')
50-
->will($this->returnValue(null));
51-
52-
return $entityManagerMock;
53-
}
54-
55-
/**
56-
* @return ProjectProviderInterface|MockObject
57-
*/
58-
private function getProjectProviderMock()
59-
{
60-
$projectProviderMock = $this->createMock(ProjectProviderInterface::class);
61-
$projectProviderMock->expects($this->once())
62-
->method('provideProject')
63-
->willReturn(new Project('Foo'));
64-
65-
return $projectProviderMock;
66-
}
67-
68-
/**
69-
* @return PackageVersionFetcher|MockObject
70-
*/
71-
private function getPackageVersionFetcherMock()
72-
{
73-
$packageVersionFetcherMock = $this->createMock(PackageVersionFetcher::class);
74-
$packageVersionFetcherMock->expects($this->once())
75-
->method('fetch')
76-
->willReturn(new ArrayCollection());
45+
$this->packageVersionFetcher->method('fetch')->willReturn(
46+
new ArrayCollection()
47+
);
7748

78-
return $packageVersionFetcherMock;
49+
$this->assertTrue($this->importProjectTask->run('https://foo.git'));
7950
}
8051
}

0 commit comments

Comments
 (0)