From bebb5f09e2230fec140df24d57c19ff66d32abc4 Mon Sep 17 00:00:00 2001 From: Brabli <67018167+Brabli@users.noreply.github.com> Date: Wed, 20 Jul 2022 21:50:33 +0100 Subject: [PATCH] Attempt to get dep injection working in tests --- config/services.yml | 4 ++++ tests/AppKernel.php | 36 +++++++++++++++++++++++++++++++++++ tests/Service/GeocodeTest.php | 24 +++++++++++------------ tests/config_test.yml | 7 +++++++ 4 files changed, 59 insertions(+), 12 deletions(-) create mode 100644 config/services.yml create mode 100644 tests/AppKernel.php create mode 100644 tests/config_test.yml diff --git a/config/services.yml b/config/services.yml new file mode 100644 index 0000000..5deb4a0 --- /dev/null +++ b/config/services.yml @@ -0,0 +1,4 @@ +services: + Pcm\GeocodeBundle\Service\Geocoder: + public: true + class: Pcm\GeocodeBundle\Service\Geocoder \ No newline at end of file diff --git a/tests/AppKernel.php b/tests/AppKernel.php new file mode 100644 index 0000000..3e0dc37 --- /dev/null +++ b/tests/AppKernel.php @@ -0,0 +1,36 @@ +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'); + } +} \ No newline at end of file diff --git a/tests/Service/GeocodeTest.php b/tests/Service/GeocodeTest.php index 3de4ad9..c880bf4 100644 --- a/tests/Service/GeocodeTest.php +++ b/tests/Service/GeocodeTest.php @@ -2,32 +2,32 @@ 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\Service\Geocoder; +use Pcm\GeocodeBundle\Tests\AppKernel; 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 private const POSTCODE = 'SW1A 1AA'; - /** - * @var \Pcm\GeocodeBundle\Geocoder - */ private Geocoder $geocoder; 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 { - $this->assertInstanceOf(\Pcm\GeocodeBundle\Geocoder::class, $this->geocoder); + $this->assertInstanceOf(\Pcm\GeocodeBundle\Service\Geocoder::class, $this->geocoder); } public function testGeocodePostcodeThrowsOnInvalidInput(): void @@ -37,11 +37,11 @@ class GeocodeTest extends TestCase $this->geocoder->geocodePostcode('aaaaaaaa'); } - public function testGeocodePostcodeReturnsGeocodeObject(): void + public function testGeocodePostcodeReturnsLatLonObject(): void { sleep(1); $result = $this->geocoder->geocodePostcode(self::POSTCODE); - $this->assertInstanceOf(GeocodeData::class, $result); + $this->assertInstanceOf(LatLon::class, $result); } private function getMappableEntity(): MappableInterface diff --git a/tests/config_test.yml b/tests/config_test.yml new file mode 100644 index 0000000..aede7d3 --- /dev/null +++ b/tests/config_test.yml @@ -0,0 +1,7 @@ +imports: + - { resource: "@PcmGeocodeBundle/config/services.yml" } + +framework: + http_method_override: false + secret: "( ͡° ͜ʖ ͡°)╭∩╮" + test: ~