Skip to content
This repository has been archived by the owner on Nov 22, 2020. It is now read-only.

Commit

Permalink
Add custom connection name.
Browse files Browse the repository at this point in the history
  • Loading branch information
perevoshchikov committed Jan 23, 2019
1 parent bd84f74 commit 7247a88
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 8 deletions.
24 changes: 21 additions & 3 deletions src/Connection/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,30 @@ class Connection implements ConnectionInterface, CollectorConnectionInterface
*/
protected $connection;

/**
* @var string|null
*/
protected $name;

/**
* @param ConnectionInterface $connection
* @param string|null $name
*/
public function __construct(ConnectionInterface $connection)
public function __construct(ConnectionInterface $connection, string $name = null)
{
$this->connection = $connection;
$this->name = $name;
}

/**
* @param string|null $name
* @return Connection
*/
public function setName(?string $name): self
{
$this->name = $name;

return $this;
}

/**
Expand All @@ -43,9 +61,9 @@ public function getSourceConnection(): ConnectionInterface
/**
* @inheritDoc
*/
public function getConnectionId(): string
public function getName(): string
{
return (string) $this->connection;
return (string) ($this->name ?? $this->connection);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Connection/ConnectionInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,5 @@ public function getProfiles(): array;
/**
* @return string
*/
public function getConnectionId(): string;
public function getName(): string;
}
2 changes: 1 addition & 1 deletion src/PredisCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ protected function collectProfiles(ConnectionInterface $connection): array
'memory_str' => $this->getDataFormatter()->formatBytes($profile->getMemoryUsage()),
'is_success' => $profile->isSuccess(),
'error_message' => (string) $profile->getError(),
'connection_id' => $connection->getConnectionId(),
'connection_id' => $connection->getName(),
];
}
}
Expand Down
24 changes: 23 additions & 1 deletion tests/Connection/ConnectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,28 @@ public function testGetSourceConnection()
$this->assertEquals($mock, $connection->getSourceConnection());
}

public function testDefaultName()
{
$mock = $this->createMock(ConnectionMock::class);
$mock->expects($this->once())
->method('__toString')
->willReturn('id');

$connection = new Connection($mock);
$this->assertEquals('id', $connection->getName());
}

public function testCustomName()
{
$mock = $this->createMock(ConnectionInterface::class);

$connection = new Connection($mock, 'custom1');
$this->assertEquals('custom1', $connection->getName());

$connection->setName('custom2');
$this->assertEquals('custom2', $connection->getName());
}

public function testWrappedMethods()
{
$command = $this->createMock(CommandInterface::class);
Expand Down Expand Up @@ -149,7 +171,7 @@ public function testMagicMethods()
$connection->foo = 'bar';
$connection->foo;
$connection->foo('bar');
$this->assertEquals('id', $connection->getConnectionId());
$this->assertEquals('id', $connection->getName());
}

protected function getProfile($response, \Exception $exception = null)
Expand Down
4 changes: 2 additions & 2 deletions tests/PredisCollectorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ public function assertCollect(
->will($this->returnValue([$profile]));

$connection->expects($this->atLeastOnce())
->method('getConnectionId')
->method('getName')
->will($this->returnValue('connectionMock'));

$this->collector->addConnection($connection);
Expand All @@ -195,7 +195,7 @@ public function assertCollect(
->formatBytes($profile->getMemoryUsage()),
'is_success' => $profile->isSuccess(),
'error_message' => $profile->getError(),
'connection_id' => $connection->getConnectionId(),
'connection_id' => $connection->getName(),
]
],
'duration_str' => $this->collector
Expand Down

0 comments on commit 7247a88

Please sign in to comment.