Skip to content

Commit

Permalink
Ensure Twitter contract resolves as singleton (fixes #349) (#352)
Browse files Browse the repository at this point in the history
  • Loading branch information
reliq authored Apr 18, 2021
1 parent 54a87de commit b45e4bc
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 27 deletions.
26 changes: 19 additions & 7 deletions src/ApiV1/Service/Twitter.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,7 @@ class Twitter implements TwitterContract

public function __construct(Querier $querier)
{
$config = $querier->getConfiguration();
$this->config = $config;
$this->querier = $querier;
$this->debug = $config->isDebugMode();
$this->setQuerier($querier);
}

/**
Expand All @@ -67,8 +64,13 @@ public function usingCredentials(
?string $consumerKey = null,
?string $consumerSecret = null
): self {
return new self(
$this->querier->usingCredentials($accessToken, $accessTokenSecret, $consumerKey, $consumerSecret)
return $this->setQuerier(
$this->querier->usingCredentials(
$accessToken,
$accessTokenSecret,
$consumerKey,
$consumerSecret
)
);
}

Expand All @@ -77,7 +79,7 @@ public function usingCredentials(
*/
public function usingConfiguration(Configuration $configuration): self
{
return new self($this->querier->usingConfiguration($configuration));
return $this->setQuerier($this->querier->usingConfiguration($configuration));
}

/**
Expand Down Expand Up @@ -136,4 +138,14 @@ public function delete(string $endpoint, array $parameters = [])
{
return $this->query($endpoint, self::REQUEST_METHOD_DELETE, $parameters);
}

private function setQuerier(Querier $querier): self
{
$config = $querier->getConfiguration();
$this->config = $config;
$this->querier = $querier;
$this->debug = $config->isDebugMode();

return $this;
}
}
2 changes: 1 addition & 1 deletion src/Concern/ApiV2Behavior.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

trait ApiV2Behavior
{
abstract protected function getQuerier(): Querier;
abstract public function getQuerier(): Querier;

protected function implodeParamValues(array $paramValues): string
{
Expand Down
3 changes: 3 additions & 0 deletions src/Contract/Twitter.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Atymic\Twitter\Contract;

use Atymic\Twitter\Contract\Querier as QuerierContract;
use Atymic\Twitter\Exception\ClientException;
use Atymic\Twitter\Twitter as BaseTwitterContract;

Expand Down Expand Up @@ -130,4 +131,6 @@ public function unfollow(string $sourceUserId, string $targetUserId);
* @see https://developer.twitter.com/en/docs/twitter-api/tweets/hide-replies/api-reference/put-tweets-id-hidden
*/
public function hideTweet(string $tweetId, bool $hidden = true);

public function getQuerier(): QuerierContract;
}
18 changes: 9 additions & 9 deletions src/Service/Accessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,10 @@ public function usingCredentials(
?string $consumerKey = null,
?string $consumerSecret = null
): self {
return new self(
$this->getQuerier()
->usingCredentials($accessToken, $accessTokenSecret, $consumerKey, $consumerSecret)
);
$this->querier = $this->getQuerier()
->usingCredentials($accessToken, $accessTokenSecret, $consumerKey, $consumerSecret);

return $this;
}

/**
Expand All @@ -57,13 +57,13 @@ public function usingCredentials(
*/
public function usingConfiguration(Configuration $configuration): self
{
return new self(
$this->getQuerier()
->usingConfiguration($configuration)
);
$this->querier = $this->getQuerier()
->usingConfiguration($configuration);

return $this;
}

protected function getQuerier(): QuerierContract
public function getQuerier(): QuerierContract
{
return $this->querier;
}
Expand Down
10 changes: 10 additions & 0 deletions tests/Unit/AccessorTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Atymic\Twitter\Tests\Unit;

use Atymic\Twitter\Contract\Configuration;
use Atymic\Twitter\Contract\Querier;
use Atymic\Twitter\Tests\Integration\Laravel\TestCase;
use Exception;
Expand All @@ -19,6 +20,11 @@ abstract class AccessorTestCase extends TestCase
protected const ARBITRARY_PARAMS = ['foo' => 'bar', 'response_format' => 'json'];
protected const ARBITRARY_RESPONSE = ['response'];

/**
* @var ObjectProphecy|Configuration
*/
protected ObjectProphecy $config;

/**
* @var ObjectProphecy|Querier
*/
Expand All @@ -29,6 +35,7 @@ abstract class AccessorTestCase extends TestCase
*/
protected function setUp(): void
{
$this->config = $this->prophesize(Configuration::class);
$this->querier = $this->prophesize(Querier::class);

$this->querier
Expand All @@ -43,5 +50,8 @@ protected function setUp(): void
$this->querier
->withOAuth2Client(Argument::cetera())
->willReturn($this->querier);
$this->querier
->getConfiguration()
->willReturn($this->config->reveal());
}
}
38 changes: 28 additions & 10 deletions tests/Unit/Service/AccessorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

namespace Atymic\Twitter\Tests\Unit\Service;

use Atymic\Twitter\Contract\Configuration;
use Atymic\Twitter\Contract\Twitter;
use Atymic\Twitter\Service\Accessor;
use Atymic\Twitter\Tests\Unit\AccessorTestCase;
Expand Down Expand Up @@ -43,12 +42,29 @@ public function testUsingCredentials(): void
{
$accessToken = 'token';
$accessTokenSecret = 'secret';
$consumerKey = 'consumer-key';
$consumerSecret = 'consumer-secret';

$this->config->getAccessToken()
->willReturn($accessToken);
$this->config->getAccessTokenSecret()
->willReturn($accessTokenSecret);
$this->config->getConsumerKey()
->willReturn($consumerKey);
$this->config->getConsumerSecret()
->willReturn($consumerSecret);

$result = $this->subject
->usingCredentials($accessToken, $accessTokenSecret);
->usingCredentials($accessToken, $accessTokenSecret, $consumerKey, $consumerSecret);
$resultConfig = $result->getQuerier()
->getConfiguration();

self::assertInstanceOf(Twitter::class, $result);
self::assertNotSame($result, $this->subject);
self::assertSame($result, $this->subject);
self::assertSame($accessToken, $resultConfig->getAccessToken());
self::assertSame($accessTokenSecret, $resultConfig->getAccessTokenSecret());
self::assertSame($consumerKey, $resultConfig->getConsumerKey());
self::assertSame($consumerSecret, $resultConfig->getConsumerSecret());
}

/**
Expand All @@ -59,16 +75,18 @@ public function testUsingCredentials(): void
*/
public function testUsingConfiguration(): void
{
/**
* @var Configuration $config
*/
$config = $this->prophesize(Configuration::class)
->reveal();
$accessToken = 'access-token';

$this->config->getAccessToken()
->willReturn($accessToken);

$result = $this->subject
->usingConfiguration($config);
->usingConfiguration($this->config->reveal());
$resultConfig = $result->getQuerier()
->getConfiguration();

self::assertInstanceOf(Twitter::class, $result);
self::assertNotSame($result, $this->subject);
self::assertSame($result, $this->subject);
self::assertSame($resultConfig->getAccessToken(), $accessToken);
}
}

0 comments on commit b45e4bc

Please sign in to comment.