. */ namespace SP\Tests\Services\Track; use DI\DependencyException; use DI\NotFoundException; use Exception; use SP\Core\Context\ContextException; use SP\Core\Exceptions\ConstraintException; use SP\Core\Exceptions\InvalidArgumentException; use SP\Core\Exceptions\QueryException; use SP\DataModel\TrackData; use SP\Repositories\NoSuchItemException; use SP\Repositories\Track\TrackRequest; use SP\Services\ServiceException; use SP\Services\Track\TrackService; use SP\Tests\DatabaseTestCase; use function SP\Tests\setupContext; /** * Class TrackServiceTest * * @package SP\Tests\Services\Track */ class TrackServiceTest extends DatabaseTestCase { /** * @var TrackService */ private static $service; /** * @throws NotFoundException * @throws ContextException * @throws DependencyException */ public static function setUpBeforeClass(): void { $dic = setupContext(); self::$loadFixtures = true; // Inicializar el servicio self::$service = $dic->get(TrackService::class); } /** * @throws ConstraintException * @throws QueryException * @throws NoSuchItemException */ public function testDelete() { self::$service->delete(1); $this->assertEquals(5, self::getRowCount('Track')); $this->expectException(NoSuchItemException::class); self::$service->delete(10); } /** * @throws NoSuchItemException * @throws ConstraintException * @throws InvalidArgumentException * @throws QueryException * @throws ServiceException */ public function testAdd() { $data = new TrackRequest(); $data->setTrackIp('192.168.0.1'); $data->userId = 1; $data->time = time(); $data->source = __METHOD__; $this->assertEquals(7, self::$service->add($data)); /** @var TrackData $resultData */ $resultData = self::$service->getById(7); $this->assertEquals(7, $resultData->getId()); $this->assertEquals($data->userId, $resultData->getUserId()); $this->assertEquals($data->time, $resultData->getTime()); $this->assertEquals($data->source, $resultData->getSource()); $this->assertEquals('192.168.0.1', $resultData->getIpv4()); } /** * @throws ServiceException * @throws ConstraintException * @throws QueryException */ public function testAddNoAddress() { $data = new TrackRequest(); $data->userId = 1; $data->time = time(); $data->source = __METHOD__; $this->expectException(ServiceException::class); self::$service->add($data); } /** * @throws ConstraintException * @throws InvalidArgumentException * @throws QueryException */ public function testGetAll() { $data = self::$service->getAll(); $this->assertCount(6, $data); $this->assertInstanceOf(TrackData::class, $data[0]); $this->assertEquals(1, $data[0]->getId()); $this->assertEquals(0, $data[0]->getUserId()); $this->assertEquals('1529145183', $data[0]->getTime()); $this->assertEquals('login', $data[0]->getSource()); $this->assertEquals('172.22.0.1', $data[0]->getIpv4()); $this->assertEquals('', $data[0]->getIpv6()); } /** * @throws NoSuchItemException * @throws ConstraintException * @throws InvalidArgumentException * @throws QueryException */ public function testGetById() { $data = self::$service->getById(1); $this->assertInstanceOf(TrackData::class, $data); $this->assertEquals(1, $data->getId()); $this->assertEquals(0, $data->getUserId()); $this->assertEquals('1529145183', $data->getTime()); $this->assertEquals('login', $data->getSource()); $this->assertEquals('172.22.0.1', $data->getIpv4()); $this->expectException(NoSuchItemException::class); self::$service->getById(10); } /** * @throws InvalidArgumentException * @throws Exception */ public function testCheckTracking() { $this->assertFalse(self::$service->checkTracking(self::$service->getTrackRequest(__CLASS__))); for ($i = 1; $i <= 10; $i++) { self::$service->add(self::$service->getTrackRequest(__CLASS__)); } $this->assertTrue(self::$service->checkTracking(self::$service->getTrackRequest(__CLASS__))); } /** * @throws ConstraintException * @throws InvalidArgumentException * @throws QueryException */ public function testGetTracksForClientFromTime() { $data = new TrackRequest(); $data->setTrackIp('172.22.0.1'); $data->time = 1529272367; $data->source = 'login'; $this->assertEquals(3, self::$service->getTracksForClientFromTime($data)); $data->time = time(); $this->assertEquals(0, self::$service->getTracksForClientFromTime($data)); } public function testClear() { $this->markTestIncomplete(); } public function testUnlock() { $this->markTestIncomplete(); } public function testSearch() { $this->markTestIncomplete(); } }