key = $key; $item->isHit = false; return $item; }, $this, CacheItem::class ); $cacheAdapter = $this->createMock(TagAwareAdapterInterface::class); $cacheAdapter->expects($this->atLeastOnce()) ->method('getItem') ->withAnyParameters() ->willReturn($createCacheItem('test')); $cacheAdapter->expects($this->atLeastOnce()) ->method('save') ->willReturn(true); $coreParametersHelper = $this->createMock(CoreParametersHelper::class); $coreParametersHelper->expects($this->once()) ->method('get') ->with($this->equalTo('cache_adapter')) ->willReturn('mautic.cache.adapter.filesystem'); $container = $this->createMock(ContainerInterface::class); $container ->expects($this->once()) ->method('has') ->with($this->equalTo('mautic.cache.adapter.filesystem')) ->willReturn(true); $container ->expects($this->once()) ->method('get') ->with($this->equalTo('mautic.cache.adapter.filesystem')) ->willReturn($cacheAdapter); $cacheProvider = new CacheProvider($coreParametersHelper, $container); $this->deviceDetectorFactory = new DeviceDetectorFactory($cacheProvider); $this->deviceCreatorService = new DeviceCreatorService(); $this->deviceTrackingService = $this->getMockBuilder(DeviceTrackingServiceInterface::class) ->disableOriginalConstructor() ->getMock(); $this->logger = $this->getMockBuilder(Logger::class) ->disableOriginalConstructor() ->getMock(); } public function testDeviceCreatedByUserAgent(): void { $lead = new Lead(); $device = new LeadDevice(); $device->setDeviceBrand('apple'); $this->deviceTrackingService->expects($this->once()) ->method('trackCurrentDevice') ->willReturn($device); $tracker = new DeviceTracker($this->deviceCreatorService, $this->deviceDetectorFactory, $this->deviceTrackingService, $this->logger); $device = $tracker->createDeviceFromUserAgent($lead, $this->userAgent); $this->assertEquals('3dfc9e6dff07948058df37455718cb98', $device->getSignature()); // Subsequent calls should not create a new tracking ID $device2 = $tracker->createDeviceFromUserAgent($lead, $this->userAgent); $this->assertEquals($device->getTrackingId(), $device2->getTrackingId()); $this->assertEquals('apple', $device2->getDeviceBrand()); $this->assertEquals($device->getSignature(), $device2->getSignature()); } }