|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Enqueue\Pheanstalk\Tests; |
| 4 | + |
| 5 | +use Enqueue\Null\NullQueue; |
| 6 | +use Enqueue\Pheanstalk\PheanstalkContext; |
| 7 | +use Enqueue\Psr\InvalidDestinationException; |
| 8 | +use Enqueue\Psr\PsrContext; |
| 9 | +use Enqueue\Test\ClassExtensionTrait; |
| 10 | +use Pheanstalk\Connection; |
| 11 | +use Pheanstalk\Pheanstalk; |
| 12 | +use PHPUnit\Framework\TestCase; |
| 13 | + |
| 14 | +class PheanstalkContextTest extends TestCase |
| 15 | +{ |
| 16 | + use ClassExtensionTrait; |
| 17 | + |
| 18 | + public function testShouldImplementPsrContextInterface() |
| 19 | + { |
| 20 | + $this->assertClassImplements(PsrContext::class, PheanstalkContext::class); |
| 21 | + } |
| 22 | + |
| 23 | + public function testCouldBeConstructedWithPheanstalkAsFirstArgument() |
| 24 | + { |
| 25 | + new PheanstalkContext($this->createPheanstalkMock()); |
| 26 | + } |
| 27 | + |
| 28 | + public function testThrowNotImplementedOnCreateTemporaryQueue() |
| 29 | + { |
| 30 | + $context = new PheanstalkContext($this->createPheanstalkMock()); |
| 31 | + |
| 32 | + $this->expectException(\LogicException::class); |
| 33 | + $this->expectExceptionMessage('Not implemented'); |
| 34 | + $context->createTemporaryQueue(); |
| 35 | + } |
| 36 | + |
| 37 | + public function testThrowInvalidDestinationIfInvalidDestinationGivenOnCreateConsumer() |
| 38 | + { |
| 39 | + $context = new PheanstalkContext($this->createPheanstalkMock()); |
| 40 | + |
| 41 | + $this->expectException(InvalidDestinationException::class); |
| 42 | + $context->createConsumer(new NullQueue('aQueue')); |
| 43 | + } |
| 44 | + |
| 45 | + public function testShouldAllowGetPheanstalkSetInConstructor() |
| 46 | + { |
| 47 | + $pheanstalk = $this->createPheanstalkMock(); |
| 48 | + |
| 49 | + $context = new PheanstalkContext($pheanstalk); |
| 50 | + |
| 51 | + $this->assertSame($pheanstalk, $context->getPheanstalk()); |
| 52 | + } |
| 53 | + |
| 54 | + public function testShouldDoConnectionDisconnectOnContextClose() |
| 55 | + { |
| 56 | + $connection = $this->createMock(Connection::class); |
| 57 | + $connection |
| 58 | + ->expects($this->once()) |
| 59 | + ->method('disconnect') |
| 60 | + ; |
| 61 | + |
| 62 | + $pheanstalk = $this->createPheanstalkMock(); |
| 63 | + $pheanstalk |
| 64 | + ->expects($this->once()) |
| 65 | + ->method('getConnection') |
| 66 | + ->willReturn($connection) |
| 67 | + ; |
| 68 | + |
| 69 | + $context = new PheanstalkContext($pheanstalk); |
| 70 | + |
| 71 | + $context->close(); |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * @return \PHPUnit_Framework_MockObject_MockObject|Pheanstalk |
| 76 | + */ |
| 77 | + private function createPheanstalkMock() |
| 78 | + { |
| 79 | + return $this->createMock(Pheanstalk::class); |
| 80 | + } |
| 81 | +} |
0 commit comments