diff --git a/module/Common/test/ConfigProviderTest.php b/module/Common/test/ConfigProviderTest.php new file mode 100644 index 00000000..54c8c65f --- /dev/null +++ b/module/Common/test/ConfigProviderTest.php @@ -0,0 +1,31 @@ +configProvider = new ConfigProvider(); + } + + /** + * @test + */ + public function configIsReturned() + { + $config = $this->configProvider->__invoke(); + + $this->assertArrayHasKey('error_handler', $config); + $this->assertArrayHasKey('middleware_pipeline', $config); + $this->assertArrayHasKey('services', $config); + $this->assertArrayHasKey('twig', $config); + } +} diff --git a/module/Common/test/Paginator/PaginableRepositoryAdapterTest.php b/module/Common/test/Paginator/PaginableRepositoryAdapterTest.php new file mode 100644 index 00000000..1135682f --- /dev/null +++ b/module/Common/test/Paginator/PaginableRepositoryAdapterTest.php @@ -0,0 +1,43 @@ +repo = $this->prophesize(PaginableRepositoryInterface::class); + $this->adapter = new PaginableRepositoryAdapter($this->repo->reveal(), 'search', 'order'); + } + + /** + * @test + */ + public function getItemsFallbacksToFindList() + { + $this->repo->findList(10, 5, 'search', 'order')->shouldBeCalledTimes(1); + $this->adapter->getItems(5, 10); + } + + /** + * @test + */ + public function countFallbacksToCountList() + { + $this->repo->countList('search')->shouldBeCalledTimes(1); + $this->adapter->count(); + } +} diff --git a/module/Common/test/Service/IpLocationResolverTest.php b/module/Common/test/Service/IpLocationResolverTest.php new file mode 100644 index 00000000..e6130569 --- /dev/null +++ b/module/Common/test/Service/IpLocationResolverTest.php @@ -0,0 +1,56 @@ +client = $this->prophesize(Client::class); + $this->ipResolver = new IpLocationResolver($this->client->reveal()); + } + + /** + * @test + */ + public function correctIpReturnsDecodedInfo() + { + $expected = [ + 'foo' => 'bar', + 'baz' => 'foo', + ]; + $response = new Response(); + $response->getBody()->write(json_encode($expected)); + $response->getBody()->rewind(); + + $this->client->get('http://freegeoip.net/json/1.2.3.4')->willReturn($response) + ->shouldBeCalledTimes(1); + $this->assertEquals($expected, $this->ipResolver->resolveIpLocation('1.2.3.4')); + } + + /** + * @test + * @expectedException \Shlinkio\Shlink\Common\Exception\WrongIpException + */ + public function guzzleExceptionThrowsShlinkException() + { + $this->client->get('http://freegeoip.net/json/1.2.3.4')->willThrow(new TransferException()) + ->shouldBeCalledTimes(1); + $this->ipResolver->resolveIpLocation('1.2.3.4'); + } +} diff --git a/module/Common/test/Twig/Extension/TranslatorExtensionTest.php b/module/Common/test/Twig/Extension/TranslatorExtensionTest.php new file mode 100644 index 00000000..06b5a584 --- /dev/null +++ b/module/Common/test/Twig/Extension/TranslatorExtensionTest.php @@ -0,0 +1,70 @@ +translator = $this->prophesize(Translator::class); + $this->extension = new TranslatorExtension($this->translator->reveal()); + } + + /** + * @test + */ + public function extensionNameIsClassName() + { + $this->assertEquals(TranslatorExtension::class, $this->extension->getName()); + } + + /** + * @test + */ + public function properFunctionsAreReturned() + { + $funcs = $this->extension->getFunctions(); + $this->assertCount(2, $funcs); + foreach ($funcs as $func) { + $this->assertInstanceOf(\Twig_SimpleFunction::class, $func); + } + } + + /** + * @test + */ + public function translateFallbacksToTranslator() + { + $this->translator->translate('foo', 'default', null)->shouldBeCalledTimes(1); + $this->extension->translate('foo'); + + $this->translator->translate('bar', 'baz', 'en')->shouldBeCalledTimes(1); + $this->extension->translate('bar', 'baz', 'en'); + } + + /** + * @test + */ + public function translatePluralFallbacksToTranslator() + { + $this->translator->translatePlural('foo', 'bar', 'baz', 'default', null)->shouldBeCalledTimes(1); + $this->extension->translatePlural('foo', 'bar', 'baz'); + + $this->translator->translatePlural('foo', 'bar', 'baz', 'another', 'en')->shouldBeCalledTimes(1); + $this->extension->translatePlural('foo', 'bar', 'baz', 'another', 'en'); + } +} diff --git a/module/Common/test/Util/DateRangeTest.php b/module/Common/test/Util/DateRangeTest.php new file mode 100644 index 00000000..4ed1586e --- /dev/null +++ b/module/Common/test/Util/DateRangeTest.php @@ -0,0 +1,32 @@ +assertNull($range->getStartDate()); + $this->assertNull($range->getEndDate()); + $this->assertTrue($range->isEmpty()); + } + + /** + * @test + */ + public function providedDatesAreSet() + { + $startDate = new \DateTime(); + $endDate = new \DateTime(); + $range = new DateRange($startDate, $endDate); + $this->assertSame($startDate, $range->getStartDate()); + $this->assertSame($endDate, $range->getEndDate()); + $this->assertFalse($range->isEmpty()); + } +}