From 1899ad5e8ac2ca710fcbfd4aa56d23da685bc159 Mon Sep 17 00:00:00 2001 From: Ladislav Palider Date: Fri, 17 Feb 2023 14:28:58 +0100 Subject: [PATCH] #64 IpAddressV4Service: Build text IpAddressV4Service - VIP --- src/libs/Dashboard/Tester.php | 14 ++-- .../Service/ipAddressV4ServiceTest.php | 80 +++++++++++++++++++ 2 files changed, 87 insertions(+), 7 deletions(-) create mode 100644 tests/BetterLocation/Service/ipAddressV4ServiceTest.php diff --git a/src/libs/Dashboard/Tester.php b/src/libs/Dashboard/Tester.php index 464c7c1e..bf909840 100644 --- a/src/libs/Dashboard/Tester.php +++ b/src/libs/Dashboard/Tester.php @@ -43,13 +43,13 @@ public function handleInput(): void { $entities = TelegramHelper::generateEntities($this->getInput()); $collection = BetterLocationCollection::fromTelegramMessage($this->getInput(), $entities); - if ($collection->count() === 0 && mb_strlen($this->getInput()) >= Config::GOOGLE_SEARCH_MIN_LENGTH && is_null(Config::GOOGLE_PLACE_API_KEY) === false) { - try { - $collection->add(GooglePlaceApi::search($this->getInput())); - } catch (\Exception $exception) { - Debugger::log($exception, Debugger::EXCEPTION); - } - } +// if ($collection->count() === 0 && mb_strlen($this->getInput()) >= Config::GOOGLE_SEARCH_MIN_LENGTH && is_null(Config::GOOGLE_PLACE_API_KEY) === false) { +// try { +// $collection->add(GooglePlaceApi::search($this->getInput())); +// } catch (\Exception $exception) { +// Debugger::log($exception, Debugger::EXCEPTION); +// } +// } $processedCollection = new ProcessedMessageResult($collection, new BetterLocationMessageSettings()); $processedCollection->process(true); diff --git a/tests/BetterLocation/Service/ipAddressV4ServiceTest.php b/tests/BetterLocation/Service/ipAddressV4ServiceTest.php new file mode 100644 index 00000000..87090899 --- /dev/null +++ b/tests/BetterLocation/Service/ipAddressV4ServiceTest.php @@ -0,0 +1,80 @@ +getCollection(); + $this->assertCount(1, $collection); + $location = $collection->getFirst(); + $this->assertEqualsWithDelta($lat, $location->getLat(), 0.000001); + $this->assertEqualsWithDelta($lon, $location->getLon(), 0.000001); + } + + public function testGenerateShareLink(): void + { + $this->assertSame('https://bannergress.com/map?lat=50.087451&lng=14.420671&zoom=15', IpAddressV4Service::getLink(50.087451, 14.420671)); + $this->assertSame('https://bannergress.com/map?lat=50.100000&lng=14.500000&zoom=15', IpAddressV4Service::getLink(50.1, 14.5)); + $this->assertSame('https://bannergress.com/map?lat=-50.200000&lng=14.600000&zoom=15', IpAddressV4Service::getLink(-50.2, 14.6000001)); // round down + $this->assertSame('https://bannergress.com/map?lat=50.300000&lng=-14.700001&zoom=15', IpAddressV4Service::getLink(50.3, -14.7000009)); // round up + $this->assertSame('https://bannergress.com/map?lat=-50.400000&lng=-14.800008&zoom=15', IpAddressV4Service::getLink(-50.4, -14.800008)); + } + + public function testGenerateDriveLink(): void + { + $this->expectException(NotSupportedException::class); + IpAddressV4Service::getLink(50.087451, 14.420671, true); + } + + public function testIsValid(): void + { + $this->assertTrue(IpAddressV4Service::isValidStatic('https://bannergress.com/banner/czech-cubism-and-its-representative-ce4b')); + $this->assertTrue(IpAddressV4Service::isValidStatic('http://bannergress.com/banner/czech-cubism-and-its-representative-ce4b')); + $this->assertTrue(IpAddressV4Service::isValidStatic('https://bannergress.com/banner/barrie-skyline-f935')); + $this->assertTrue(IpAddressV4Service::isValidStatic('https://bannergress.com/banner/hist%C3%B3rica-catedral-de-san-lorenzo-55dd')); + $this->assertTrue(IpAddressV4Service::isValidStatic('https://bannergress.com/banner/histórica-catedral-de-san-lorenzo-55dd')); + $this->assertTrue(IpAddressV4Service::isValidStatic('https://bannergress.com/banner/長良川鉄道-乗りつぶし-観光編-adea')); + $this->assertTrue(IpAddressV4Service::isValidStatic('https://bannergress.com/banner/%E9%95%B7%E8%89%AF%E5%B7%9D%E9%89%84%E9%81%93-%E4%B9%97%E3%82%8A%E3%81%A4%E3%81%B6%E3%81%97-%E8%A6%B3%E5%85%89%E7%B7%A8-adea')); + + // Invalid + $this->assertFalse(IpAddressV4Service::isValidStatic('some invalid url')); + $this->assertFalse(IpAddressV4Service::isValidStatic('https://bannergress.com')); + $this->assertFalse(IpAddressV4Service::isValidStatic('http://bannergress.com')); + $this->assertFalse(IpAddressV4Service::isValidStatic('https://bannergress.com/banner/')); + $this->assertFalse(IpAddressV4Service::isValidStatic('http://www.some-domain.cz/')); + $this->assertFalse(IpAddressV4Service::isValidStatic('http://www.some-domain.cz/some-path')); + } + + /** + * @group request + */ + public function testProcessPlace(): void + { + $this->assertLocation('https://bannergress.com/banner/czech-cubism-and-its-representative-ce4b', 50.087213, 14.425674); + // This is failing only in unit tests but works if processed manually (via tester in browser or Telegram) + // $this->assertLocation('https://bannergress.com/banner/長良川鉄道-乗りつぶし-観光編-adea', 35.445393, 137.019408); + $this->assertLocation('https://bannergress.com/banner/%E9%95%B7%E8%89%AF%E5%B7%9D%E9%89%84%E9%81%93-%E4%B9%97%E3%82%8A%E3%81%A4%E3%81%B6%E3%81%97-%E8%A6%B3%E5%85%89%E7%B7%A8-adea', 35.445393, 137.019408); + $this->assertLocation('https://bannergress.com/banner/a-visit-to-te-papa-dffa', -41.287008, 174.778374); + $this->assertLocation('https://bannergress.com/banner/hist%C3%B3rica-catedral-de-san-lorenzo-55dd', -25.3414, -57.508801); + $this->assertLocation('https://bannergress.com/banner/histórica-catedral-de-san-lorenzo-55dd', -25.3414, -57.508801); + } + + /** + * Pages, that do not have any location + * @group request + */ + public function testInvalid(): void + { + $this->expectException(InvalidResponseException::class); + $this->expectExceptionCode(404); + $this->expectExceptionMessage('Invalid response code "404" but required "200" for URL "https://api.bannergress.com/bnrs/aaaa-bbbb"'); + IpAddressV4Service::processStatic('https://bannergress.com/banner/aaaa-bbbb')->getCollection(); + } +}