Attempt to get dep injection working in tests

This commit is contained in:
Brabli
2022-07-20 21:50:33 +01:00
parent 3b0a5ddde7
commit bebb5f09e2
4 changed files with 59 additions and 12 deletions

4
config/services.yml Normal file
View File

@@ -0,0 +1,4 @@
services:
Pcm\GeocodeBundle\Service\Geocoder:
public: true
class: Pcm\GeocodeBundle\Service\Geocoder

36
tests/AppKernel.php Normal file
View File

@@ -0,0 +1,36 @@
<?php
declare(strict_types=1);
namespace Pcm\GeocodeBundle\Tests;
use Pcm\GeocodeBundle\PcmGeocodeBundle;
use Symfony\Bundle\FrameworkBundle\FrameworkBundle;
use Symfony\Component\Config\Loader\LoaderInterface;
use Symfony\Component\HttpKernel\Kernel;
class AppKernel extends Kernel
{
/**
* @return array
*/
public function registerBundles(): array
{
$bundles = array();
if (in_array($this->getEnvironment(), array('test'))) {
$bundles[] = new FrameworkBundle();
$bundles[] = new PcmGeocodeBundle();
}
return $bundles;
}
/**
* @param LoaderInterface $loader
*/
public function registerContainerConfiguration(LoaderInterface $loader)
{
$loader->load(__DIR__.'/config_'.$this->getEnvironment().'.yml');
}
}

View File

@@ -2,32 +2,32 @@
declare(strict_types=1); declare(strict_types=1);
namespace app\tests\Service; namespace Pcm\GeocodeBundle\Tests;
use Pcm\GeocodeBundle\Entity\GeocodeData;
use Pcm\GeocodeBundle\Geocoder;
use Pcm\GeocodeBundle\Interface\MappableInterface; use Pcm\GeocodeBundle\Interface\MappableInterface;
use Pcm\GeocodeBundle\Service\Geocoder;
use Pcm\GeocodeBundle\Tests\AppKernel;
use Pcm\GeocodeBundle\Trait\MappableTrait; use Pcm\GeocodeBundle\Trait\MappableTrait;
use PHPUnit\Framework\TestCase; use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
class GeocodeTest extends TestCase class GeocodeTest extends KernelTestCase
{ {
// Buckingham Palace // Buckingham Palace
private const POSTCODE = 'SW1A 1AA'; private const POSTCODE = 'SW1A 1AA';
/**
* @var \Pcm\GeocodeBundle\Geocoder
*/
private Geocoder $geocoder; private Geocoder $geocoder;
protected function setUp(): void protected function setUp(): void
{ {
$this->geocoder = new Geocoder(); $kernel = new AppKernel('test', false);
$kernel->boot();
$this->geocoder = $kernel->getContainer()->get(Geocoder::class);
} }
public function testGeocodeInstance(): void public function testGeocodeInstance(): void
{ {
$this->assertInstanceOf(\Pcm\GeocodeBundle\Geocoder::class, $this->geocoder); $this->assertInstanceOf(\Pcm\GeocodeBundle\Service\Geocoder::class, $this->geocoder);
} }
public function testGeocodePostcodeThrowsOnInvalidInput(): void public function testGeocodePostcodeThrowsOnInvalidInput(): void
@@ -37,11 +37,11 @@ class GeocodeTest extends TestCase
$this->geocoder->geocodePostcode('aaaaaaaa'); $this->geocoder->geocodePostcode('aaaaaaaa');
} }
public function testGeocodePostcodeReturnsGeocodeObject(): void public function testGeocodePostcodeReturnsLatLonObject(): void
{ {
sleep(1); sleep(1);
$result = $this->geocoder->geocodePostcode(self::POSTCODE); $result = $this->geocoder->geocodePostcode(self::POSTCODE);
$this->assertInstanceOf(GeocodeData::class, $result); $this->assertInstanceOf(LatLon::class, $result);
} }
private function getMappableEntity(): MappableInterface private function getMappableEntity(): MappableInterface

7
tests/config_test.yml Normal file
View File

@@ -0,0 +1,7 @@
imports:
- { resource: "@PcmGeocodeBundle/config/services.yml" }
framework:
http_method_override: false
secret: "( ͡° ͜ʖ ͡°)╭∩╮"
test: ~