Skip to content

Commit 307e678

Browse files
committed
Get ImportProjectTaskTest closer to real life
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 307e678

File tree

1 file changed

+17
-45
lines changed

1 file changed

+17
-45
lines changed

src/AppBundle/Tests/ProjectImport/ImportProjectTaskTest.php

Lines changed: 17 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -10,23 +10,30 @@
1010
use Doctrine\Common\Collections\ArrayCollection;
1111
use Doctrine\ORM\EntityManagerInterface;
1212
use PHPUnit\Framework\MockObject\MockObject;
13-
use PHPUnit\Framework\TestCase;
1413
use Psr\Log\NullLogger;
14+
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
1515

16-
class ImportProjectTaskTest extends TestCase
16+
class ImportProjectTaskTest extends KernelTestCase
1717
{
1818
/**
1919
* @var ImportProjectTask
2020
*/
2121
private $importProjectTask;
2222

23+
private PackageVersionFetcher&MockObject $packageVersionFetcher;
24+
private VcsDriverFactory&MockObject $vcsDriverFactory;
25+
2326
protected function setUp(): void
2427
{
28+
parent::setUp();
29+
self::bootKernel();
30+
$this->vcsDriverFactory = $this->createMock(VcsDriverFactory::class);
31+
$this->packageVersionFetcher = $this->createMock(PackageVersionFetcher::class);
2532
$this->importProjectTask = new ImportProjectTask(
26-
$this->getEntityManagerMock(),
27-
$this->getProjectProviderMock(),
28-
$this->getPackageVersionFetcherMock(),
29-
$this->createMock(VcsDriverFactory::class),
33+
self::$container->get(EntityManagerInterface::class),
34+
self::$container->get(ProjectProviderInterface::class),
35+
$this->packageVersionFetcher,
36+
$this->vcsDriverFactory,
3037
new NullLogger()
3138
);
3239
}
@@ -36,45 +43,10 @@ protected function setUp(): void
3643
*/
3744
public function import()
3845
{
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());
46+
$this->packageVersionFetcher->method('fetch')->willReturn(
47+
new ArrayCollection()
48+
);
7749

78-
return $packageVersionFetcherMock;
50+
$this->assertTrue($this->importProjectTask->run('https://foo.git'));
7951
}
8052
}

0 commit comments

Comments
 (0)