This repository has been archived by the owner on Mar 8, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(clickcounter-iframe): add command for updating the iframe config
- Loading branch information
1 parent
6b89929
commit a8185b8
Showing
8 changed files
with
196 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
src/Dothiv/BusinessBundle/Listener/ClickCounterIframeConfigListener.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
<?php | ||
|
||
namespace Dothiv\BusinessBundle\Listener; | ||
|
||
use Dothiv\BusinessBundle\Event\ClickCounterConfigurationEvent; | ||
use Dothiv\ValueObject\URLValue; | ||
use Guzzle\Http\Client; | ||
|
||
/** | ||
* This listener updates the iframe configuration on the CNAME instance. | ||
*/ | ||
class ClickCounterIframeConfigListener | ||
{ | ||
|
||
/** | ||
* @var Client | ||
*/ | ||
private $client; | ||
|
||
/** | ||
* @var URLValue | ||
*/ | ||
private $serviceUrl; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
private $username; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
private $password; | ||
|
||
/** | ||
* @param Client $client | ||
* @param string $password | ||
* @param string $serviceUrl | ||
* @param string $username | ||
*/ | ||
public function __construct(Client $client, $serviceUrl, $username, $password) | ||
{ | ||
$this->client = $client; | ||
$this->serviceUrl = new URLValue($serviceUrl); | ||
$this->username = $username; | ||
$this->password = $password; | ||
} | ||
|
||
/** | ||
* @param ClickCounterConfigurationEvent $event | ||
*/ | ||
public function onClickCounterConfiguration(ClickCounterConfigurationEvent $event) | ||
{ | ||
$domain = $event->getDomain(); | ||
$config = $event->getConfig(); | ||
|
||
if (!isset($config['redirect_url'])) { | ||
$this->client->delete( | ||
$this->serviceUrl->toScalar() . 'domain/' . $domain->getName(), | ||
null, | ||
null, | ||
array( | ||
'auth' => array($this->username, $this->password) | ||
) | ||
)->send(); | ||
} else { | ||
$this->client->put( | ||
$this->serviceUrl->toScalar() . 'domain/' . $domain->getName(), | ||
array('Content-Type' => 'application/json'), | ||
json_encode(array('redirect' => $config['redirect_url']), JSON_UNESCAPED_SLASHES), | ||
array( | ||
'auth' => array($this->username, $this->password) | ||
) | ||
)->send(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 84 additions & 0 deletions
84
src/Dothiv/BusinessBundle/Tests/Listener/ClickCounterIframeConfigListenerTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
<?php | ||
|
||
|
||
namespace Dothiv\BusinessBundle\Test\Listener; | ||
|
||
use Dothiv\BusinessBundle\Entity\Banner; | ||
use Dothiv\BusinessBundle\Entity\Domain; | ||
use Dothiv\BusinessBundle\Event\ClickCounterConfigurationEvent; | ||
use Dothiv\BusinessBundle\Listener\ClickCounterIframeConfigListener; | ||
use Guzzle\Http\Client; | ||
use Guzzle\Http\Message\EntityEnclosingRequestInterface; | ||
use Guzzle\Plugin\History\HistoryPlugin; | ||
use Guzzle\Plugin\Mock\MockPlugin; | ||
|
||
class ClickCounterIframeConfigListenerTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
|
||
/** | ||
* @var Client|\PHPUnit_Framework_MockObject_MockObject | ||
*/ | ||
private $client; | ||
|
||
/** | ||
* @test | ||
* @group BusinessBundle | ||
* @group Listener | ||
*/ | ||
public function itShouldBeInstantiable() | ||
{ | ||
$this->assertInstanceOf('\Dothiv\BusinessBundle\Listener\ClickCounterIframeConfigListener', $this->createTestObject()); | ||
} | ||
|
||
/** | ||
* @test | ||
* @group BusinessBundle | ||
* @group Listener | ||
* @depends itShouldBeInstantiable | ||
*/ | ||
public function itShouldUpdateTheIframeConfig() | ||
{ | ||
$domain = new Domain(); | ||
$domain->setName('thjnk.hiv'); | ||
$banner = new Banner(); | ||
$banner->setRedirectUrl('http://www.thjnk.de/'); | ||
$domain->setActiveBanner($banner); | ||
|
||
// Mock response | ||
$plugin = new MockPlugin(); | ||
$plugin->addResponse(__DIR__ . '/data/domain-created.data'); | ||
$this->client->addSubscriber($plugin); | ||
|
||
$history = new HistoryPlugin(); | ||
$this->client->addSubscriber($history); | ||
|
||
$event = new ClickCounterConfigurationEvent($domain, array('redirect_url' => $banner->getRedirectUrl())); | ||
$this->createTestObject()->onClickCounterConfiguration($event); | ||
|
||
/** @var EntityEnclosingRequestInterface $request */ | ||
$request = $history->getLastRequest(); | ||
$this->assertEquals($request->getUrl(), 'http://iframe.clickcounter.hiv/domain/thjnk.hiv'); | ||
$this->assertEquals($request->getMethod(), 'PUT'); | ||
$this->assertEquals($request->getHeader('Content-Type'), 'application/json'); | ||
$this->assertEquals($request->getUsername(), 'iframe-admin'); | ||
$this->assertEquals($request->getPassword(), 'some-password'); | ||
$this->assertEquals((string)$request->getBody(), '{"redirect":"http://www.thjnk.de/"}'); | ||
} | ||
|
||
/** | ||
* @return ClickCounterIframeConfigListener | ||
*/ | ||
protected function createTestObject() | ||
{ | ||
return new ClickCounterIframeConfigListener($this->client, 'http://iframe.clickcounter.hiv', 'iframe-admin', 'some-password'); | ||
} | ||
|
||
/** | ||
* Test setup | ||
*/ | ||
protected function setUp() | ||
{ | ||
parent::setUp(); | ||
$this->client = new Client(); | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
src/Dothiv/BusinessBundle/Tests/Listener/data/domain-created.data
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
HTTP/1.1 201 Created | ||
Location: /domain/thjnk.hiv | ||
Date: Tue, 09 Dec 2014 09:53:14 GMT | ||
Content-Length: 0 | ||
Content-Type: text/plain; charset=utf-8 |