Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stop using deprecated assertAttribute* methods of PHPUnit #780

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
178 changes: 86 additions & 92 deletions tests/ClientBuilderTest.php
Expand Up @@ -4,19 +4,22 @@

namespace Sentry\Tests;

use Http\Client\Common\Plugin;
use Http\Client\Common\PluginClient;
use Http\Client\HttpAsyncClient;
use Http\Message\MessageFactory;
use Http\Message\UriFactory;
use Http\Promise\Promise;
use Http\Client\Common\Plugin as PluginInterface;
use Http\Client\HttpAsyncClient as HttpAsyncClientInterface;
use Http\Discovery\MessageFactoryDiscovery;
use Http\Discovery\UriFactoryDiscovery;
use Http\Message\MessageFactory as MessageFactoryInterface;
use Http\Message\UriFactory as UriFactoryInterface;
use Http\Promise\FulfilledPromise;
use Http\Promise\Promise as PromiseInterface;
use Jean85\PrettyVersions;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Psr\Http\Message\RequestInterface;
use Sentry\Client;
use Sentry\ClientBuilder;
use Sentry\Event;
use Sentry\FlushableClientInterface;
use Sentry\Integration\ErrorListenerIntegration;
use Sentry\Integration\ExceptionListenerIntegration;
use Sentry\Integration\FatalErrorListenerIntegration;
Expand Down Expand Up @@ -60,13 +63,17 @@ public function testNullTransportIsUsedWhenNoServerIsConfigured(): void
*/
public function testSetUriFactory(): void
{
/** @var UriFactory|MockObject $uriFactory */
$uriFactory = $this->createMock(UriFactory::class);
/** @var UriFactoryInterface&MockObject $uriFactory */
$uriFactory = $this->createMock(UriFactoryInterface::class);
$uriFactory->expects($this->once())
->method('createUri')
->willReturn(UriFactoryDiscovery::find()->createUri('http://www.example.com'));

$clientBuilder = ClientBuilder::create(['dsn' => 'http://public:secret@example.com/sentry/1']);
$clientBuilder->setUriFactory($uriFactory);
$client = ClientBuilder::create(['dsn' => 'http://public@example.com/sentry/1'])
->setUriFactory($uriFactory)
->getClient();

$this->assertAttributeSame($uriFactory, 'uriFactory', $clientBuilder);
$client->captureMessage('foo');
}

/**
Expand All @@ -76,17 +83,17 @@ public function testSetUriFactory(): void
*/
public function testSetMessageFactory(): void
{
/** @var MessageFactory|MockObject $messageFactory */
$messageFactory = $this->createMock(MessageFactory::class);

$clientBuilder = ClientBuilder::create(['dsn' => 'http://public:secret@example.com/sentry/1']);
$clientBuilder->setMessageFactory($messageFactory);
/** @var MessageFactoryInterface&MockObject $messageFactory */
$messageFactory = $this->createMock(MessageFactoryInterface::class);
$messageFactory->expects($this->once())
->method('createRequest')
->willReturn(MessageFactoryDiscovery::find()->createRequest('POST', 'http://www.example.com'));

$this->assertAttributeSame($messageFactory, 'messageFactory', $clientBuilder);

$transport = $this->getObjectAttribute($clientBuilder->getClient(), 'transport');
$client = ClientBuilder::create(['dsn' => 'http://public@example.com/sentry/1'])
->setMessageFactory($messageFactory)
->getClient();

$this->assertAttributeSame($messageFactory, 'requestFactory', $transport);
$client->captureMessage('foo');
}

/**
Expand All @@ -96,14 +103,17 @@ public function testSetMessageFactory(): void
*/
public function testSetTransport(): void
{
/** @var TransportInterface|MockObject $transport */
/** @var TransportInterface&MockObject $transport */
$transport = $this->createMock(TransportInterface::class);
$transport->expects($this->once())
->method('send')
->willReturn('ddb4a0b9ab1941bf92bd2520063663e3');

$clientBuilder = ClientBuilder::create(['dsn' => 'http://public:secret@example.com/sentry/1']);
$clientBuilder->setTransport($transport);
$client = ClientBuilder::create(['dsn' => 'http://public@example.com/sentry/1'])
->setTransport($transport)
->getClient();

$this->assertAttributeSame($transport, 'transport', $clientBuilder);
$this->assertAttributeSame($transport, 'transport', $clientBuilder->getClient());
$this->assertSame('ddb4a0b9ab1941bf92bd2520063663e3', $client->captureMessage('foo'));
}

/**
Expand All @@ -113,17 +123,19 @@ public function testSetTransport(): void
*/
public function testSetHttpClient(): void
{
/** @var HttpAsyncClient|MockObject $httpClient */
$httpClient = $this->createMock(HttpAsyncClient::class);

$clientBuilder = ClientBuilder::create(['dsn' => 'http://public:secret@example.com/sentry/1']);
$clientBuilder->setHttpClient($httpClient);

$this->assertAttributeSame($httpClient, 'httpClient', $clientBuilder);

$transport = $this->getObjectAttribute($clientBuilder->getClient(), 'transport');

$this->assertAttributeSame($httpClient, 'client', $this->getObjectAttribute($transport, 'httpClient'));
/** @var HttpAsyncClientInterface&MockObject $httpClient */
$httpClient = $this->createMock(HttpAsyncClientInterface::class);
$httpClient->expects($this->once())
->method('sendAsyncRequest')
->willReturn(new FulfilledPromise(true));

/** @var FlushableClientInterface $client */
$client = ClientBuilder::create(['dsn' => 'http://public@example.com/sentry/1'])
->setHttpClient($httpClient)
->getClient();

$client->captureMessage('foo');
$client->flush();
}

/**
Expand All @@ -133,16 +145,19 @@ public function testSetHttpClient(): void
*/
public function testAddHttpClientPlugin(): void
{
/** @var Plugin|MockObject $plugin */
$plugin = $this->createMock(Plugin::class);

$clientBuilder = new ClientBuilder();
$clientBuilder->addHttpClientPlugin($plugin);

$plugins = $this->getObjectAttribute($clientBuilder, 'httpClientPlugins');

$this->assertCount(1, $plugins);
$this->assertSame($plugin, $plugins[0]);
/** @var PluginInterface&MockObject $plugin */
$plugin = $this->createMock(PluginInterface::class);
$plugin->expects($this->once())
->method('handleRequest')
->willReturn(new FulfilledPromise(true));

/** @var FlushableClientInterface $client */
$client = ClientBuilder::create(['dsn' => 'http://public@example.com/sentry/1'])
->addHttpClientPlugin($plugin)
->getClient();

$client->captureMessage('foo');
$client->flush();
}

/**
Expand All @@ -153,36 +168,30 @@ public function testAddHttpClientPlugin(): void
*/
public function testRemoveHttpClientPlugin(): void
{
$plugin = new PluginStub1();
$plugin2 = new PluginStub2();

$clientBuilder = new ClientBuilder();
$clientBuilder->addHttpClientPlugin($plugin);
$clientBuilder->addHttpClientPlugin($plugin);
$clientBuilder->addHttpClientPlugin($plugin2);

$this->assertAttributeCount(3, 'httpClientPlugins', $clientBuilder);

$clientBuilder->removeHttpClientPlugin(PluginStub1::class);

$plugins = $this->getObjectAttribute($clientBuilder, 'httpClientPlugins');

$this->assertCount(1, $plugins);
$this->assertSame($plugin2, reset($plugins));
}

public function testGetClient(): void
{
$clientBuilder = ClientBuilder::create(['dsn' => 'http://public:secret@example.com/sentry/1']);
$client = $clientBuilder->getClient();

$this->assertInstanceOf(Client::class, $client);
$this->assertAttributeInstanceOf(HttpTransport::class, 'transport', $client);

$transport = $this->getObjectAttribute($client, 'transport');

$this->assertAttributeSame($this->getObjectAttribute($clientBuilder, 'messageFactory'), 'requestFactory', $transport);
$this->assertAttributeInstanceOf(PluginClient::class, 'httpClient', $transport);
$plugin = new class() implements PluginInterface {
public function handleRequest(RequestInterface $request, callable $next, callable $first): PromiseInterface
{
return new FulfilledPromise(true);
}
};

$plugin2 = new class() implements PluginInterface {
public function handleRequest(RequestInterface $request, callable $next, callable $first): PromiseInterface
{
return new FulfilledPromise(true);
}
};

/** @var FlushableClientInterface $client */
$client = ClientBuilder::create()
->addHttpClientPlugin($plugin)
->addHttpClientPlugin($plugin)
->addHttpClientPlugin($plugin2)
->removeHttpClientPlugin(\get_class($plugin2))
->getClient();

$client->captureMessage('foo');
$client->flush();
}

/**
Expand All @@ -194,8 +203,7 @@ public function testIntegrationsAreAddedToClientCorrectly(bool $defaultIntegrati
$options->setDefaultIntegrations($defaultIntegrations);
$options->setIntegrations($integrations);

$clientBuilder = new ClientBuilder($options);
$client = $clientBuilder->getClient();
$client = (new ClientBuilder($options))->getClient();

$actualIntegrationsClassNames = array_map('\get_class', $client->getOptions()->getIntegrations());

Expand Down Expand Up @@ -299,17 +307,3 @@ public function setupOnce(): void
{
}
}

final class PluginStub1 implements Plugin
{
public function handleRequest(RequestInterface $request, callable $next, callable $first): Promise
{
}
}

final class PluginStub2 implements Plugin
{
public function handleRequest(RequestInterface $request, callable $next, callable $first): Promise
{
}
}
3 changes: 2 additions & 1 deletion tests/Context/AbstractContextTest.php
Expand Up @@ -100,7 +100,8 @@ public function testOffsetSet(string $key, $value, ?string $expectedExceptionCla
$context = $this->createContext();
$context[$key] = $value;

$this->assertArraySubset([$key => $value], $context->toArray());
$this->assertArrayHasKey($key, $context);
$this->assertSame($value, $context[$key]);
}

/**
Expand Down
55 changes: 22 additions & 33 deletions tests/Context/ContextTest.php
Expand Up @@ -9,14 +9,14 @@

class ContextTest extends TestCase
{
public function testConstructor()
public function testConstructor(): void
{
$context = new Context(['foo' => 'bar']);

$this->assertAttributeEquals(['foo' => 'bar'], 'data', $context);
$this->assertSame(['foo' => 'bar'], $context->toArray());
}

public function testMerge()
public function testMerge(): void
{
$context = new Context([
'foo' => 'bar',
Expand All @@ -27,45 +27,45 @@ public function testMerge()

$context->merge(['bar' => ['barfoo' => 'foobar']], true);

$this->assertAttributeEquals(['foo' => 'bar', 'bar' => ['foobar' => 'barfoo', 'barfoo' => 'foobar']], 'data', $context);
$this->assertSame(['foo' => 'bar', 'bar' => ['foobar' => 'barfoo', 'barfoo' => 'foobar']], $context->toArray());

$context->merge(['bar' => 'foo']);

$this->assertAttributeEquals(['foo' => 'bar', 'bar' => 'foo'], 'data', $context);
$this->assertSame(['foo' => 'bar', 'bar' => 'foo'], $context->toArray());
}

public function testSetData()
public function testSetData(): void
{
$context = new Context(['foo' => 'bar']);
$context->setData(['bar' => 'foo']);

$this->assertAttributeEquals(['foo' => 'bar', 'bar' => 'foo'], 'data', $context);
$this->assertSame(['foo' => 'bar', 'bar' => 'foo'], $context->toArray());

$context->setData(['foo' => ['bar' => 'baz']]);

$this->assertAttributeEquals(['foo' => ['bar' => 'baz'], 'bar' => 'foo'], 'data', $context);
$this->assertSame(['foo' => ['bar' => 'baz'], 'bar' => 'foo'], $context->toArray());
}

public function testReplaceData()
public function testReplaceData(): void
{
$context = new Context(['foo' => 'bar']);
$context->replaceData(['bar' => 'foo']);

$this->assertAttributeEquals(['bar' => 'foo'], 'data', $context);
$this->assertSame(['bar' => 'foo'], $context->toArray());
}

public function testClear()
public function testClear(): void
{
$context = new Context(['foo' => 'bar']);

$this->assertAttributeEquals(['foo' => 'bar'], 'data', $context);
$this->assertSame(['foo' => 'bar'], $context->toArray());

$context->clear();

$this->assertAttributeEmpty('data', $context);
$this->assertSame([], $context->toArray());
}

public function testIsEmpty()
public function testIsEmpty(): void
{
$context = new Context();

Expand All @@ -76,51 +76,40 @@ public function testIsEmpty()
$this->assertFalse($context->isEmpty());
}

public function testToArray()
public function testJsonSerialize(): void
{
$context = new Context(['foo' => 'bar']);

$this->assertEquals(['foo' => 'bar'], $context->toArray());
$this->assertSame('{"foo":"bar"}', json_encode($context));
}

public function testJsonSerialize()
{
$context = new Context(['foo' => 'bar']);

$this->assertEquals('{"foo":"bar"}', json_encode($context));
}

public function testArrayLikeBehaviour()
public function testArrayLikeBehaviour(): void
{
$context = new Context();

$this->assertAttributeEquals([], 'data', $context);
$this->assertArrayNotHasKey('foo', $context);

// Accessing a key that does not exists in the data object should behave
// like accessing a non-existent key of an array
@$context['foo'];

$error = error_get_last();

$this->assertInternalType('array', $error);
$this->assertEquals('Undefined index: foo', $error['message']);
$this->assertIsArray($error);
$this->assertSame('Undefined index: foo', $error['message']);

$context['foo'] = 'bar';

$this->assertAttributeEquals(['foo' => 'bar'], 'data', $context);
$this->assertTrue(isset($context['foo']));
$this->assertEquals('bar', $context['foo']);
$this->assertSame('bar', $context['foo']);

unset($context['foo']);

$this->assertArrayNotHasKey('foo', $context);
}

public function testGetIterator()
public function testGetIterator(): void
{
$context = new Context(['foo' => 'bar', 'bar' => 'foo']);

$this->assertEquals($context->toArray(), iterator_to_array($context));
$this->assertSame($context->toArray(), iterator_to_array($context));
}
}