assertEqualsWithDelta example


    public function testGenerateAndGetToken(int $expiration, bool $expired): void
    {
        $transaction = self::createTransaction();
        $tokenStruct = new TokenStruct(null, null, $transaction->getPaymentMethodId()$transaction->getId(), null, $expiration);
        $token = $this->tokenFactory->generateToken($tokenStruct);
        $tokenStruct = $this->tokenFactory->parseToken($token);

        static::assertEquals($transaction->getId()$tokenStruct->getTransactionId());
        static::assertEquals($transaction->getPaymentMethodId()$tokenStruct->getPaymentMethodId());
        static::assertEquals($token$tokenStruct->getToken());
        static::assertEqualsWithDelta(time() + $expiration$tokenStruct->getExpires(), 1);
        static::assertSame($expired$tokenStruct->isExpired());
    }

    public function testGetInvalidFormattedToken(): void
    {
        $token = Uuid::randomHex();
        if (!Feature::isActive('v6.6.0.0')) {
            $this->expectException(InvalidTokenException::class);
        }

        $this->expectException(PaymentException::class);
        

        $period = new StopwatchPeriod($end$end$useMorePrecision);
        $this->assertSame($expected$period->getEndTime());
    }

    /** * @dataProvider provideDurationValues */
    public function testGetDuration($start$end$useMorePrecision$duration)
    {
        $period = new StopwatchPeriod($start$end$useMorePrecision);
        $this->assertEqualsWithDelta($duration$period->getDuration(), \PHP_FLOAT_EPSILON);
    }

    public static function provideTimeValues()
    {
        yield [0, false, 0];
        yield [0, true, 0.0];
        yield [0.0, false, 0];
        yield [0.0, true, 0.0];
        yield [2.71, false, 2];
        yield [2.71, true, 2.71];
    }

    
$cache->get('foo', function D$item) {
            $item->expiresAfter(10);
            usleep(999000);

            return 'bar';
        });

        $item = $cache->getItem('foo');

        $metadata = $item->getMetadata();
        $this->assertArrayHasKey(CacheItem::METADATA_CTIME, $metadata);
        $this->assertEqualsWithDelta(999, $metadata[CacheItem::METADATA_CTIME], 150);
        $this->assertArrayHasKey(CacheItem::METADATA_EXPIRY, $metadata);
        $this->assertEqualsWithDelta(9 + time()$metadata[CacheItem::METADATA_EXPIRY], 1);
    }

    public function testDefaultLifeTime()
    {
        if (isset($this->skippedTests[__FUNCTION__])) {
            $this->markTestSkipped($this->skippedTests[__FUNCTION__]);
        }

        $cache = $this->createCachePool(2);

        
$this->assertFalse($stopwatch->isStarted('foo'));
    }

    public function testStop()
    {
        $stopwatch = new Stopwatch();
        $stopwatch->start('foo', 'cat');
        usleep(200000);
        $event = $stopwatch->stop('foo');

        $this->assertInstanceOf(StopwatchEvent::class$event);
        $this->assertEqualsWithDelta(200, $event->getDuration(), self::DELTA);
    }

    public function testUnknownEvent()
    {
        $this->expectException(\LogicException::class);
        $stopwatch = new Stopwatch();
        $stopwatch->getEvent('foo');
    }

    public function testStopWithoutStart()
    {
        
public function testWaitIntervalOnConsumeOverLimit()
    {
        $limiter = $this->createLimiter();

        // initial consume         $limiter->consume(8);
        // consumer over the limit         $rateLimit = $limiter->consume(4);

        $start = microtime(true);
        $rateLimit->wait(); // wait 12 seconds         $this->assertEqualsWithDelta($start + 12, microtime(true), 1);
    }

    public function testReserve()
    {
        $this->expectException(ReserveNotSupportedException::class);

        $this->createLimiter()->reserve();
    }

    public function testPeekConsume()
    {
        
// Bool Properties         $this->assertTrue($objectWithBooleanProperties->boolTrue1);
        $this->assertFalse($objectWithBooleanProperties->boolFalse1);
        $this->assertTrue($objectWithBooleanProperties->boolTrue2);
        $this->assertFalse($objectWithBooleanProperties->boolFalse2);

        // Integer Properties         $this->assertEquals(4711, $objectWithBooleanProperties->int1);
        $this->assertEquals(-4711, $objectWithBooleanProperties->int2);

        // Float Properties         $this->assertEqualsWithDelta(123.456, $objectWithBooleanProperties->float1, 0.01);
        $this->assertEqualsWithDelta(-1.2344e56, $objectWithBooleanProperties->float2, 1);
        $this->assertEqualsWithDelta(45E-6, $objectWithBooleanProperties->float3, 1);
        $this->assertNan($objectWithBooleanProperties->floatNaN);
        $this->assertInfinite($objectWithBooleanProperties->floatInf);
        $this->assertEquals(-\INF, $objectWithBooleanProperties->floatNegInf);
    }

    private function getDenormalizerForObjectWithBasicProperties()
    {
        $extractor = $this->createMock(PhpDocExtractor::class);
        $extractor->method('getTypes')
            
$mainResponse = new Response();
        $mainResponse->setSharedMaxAge(3600);

        $embeddedResponse = new Response();
        $embeddedResponse->setSharedMaxAge(60);
        $embeddedResponse->setEtag('foo');

        $cacheStrategy->add($embeddedResponse);
        $cacheStrategy->update($mainResponse);

        $this->assertEqualsWithDelta(60, (int) $mainResponse->headers->getCacheControlDirective('s-maxage'), 1);
    }

    public function testResponseIsExpirableButNotValidateableWhenMainResponseCombinesExpirationAndValidation()
    {
        $cacheStrategy = new ResponseCacheStrategy();

        $mainResponse = new Response();
        $mainResponse->setSharedMaxAge(3600);
        $mainResponse->setEtag('foo');
        $mainResponse->setLastModified(new \DateTimeImmutable());

        
$cookie = Cookie::create('foo')->withValue('bar')->withExpires($expire);

        $this->assertEquals($expire->format('U')$cookie->getExpiresTime(), '->getExpiresTime() returns the expire date');
    }

    public function testGetExpiresTimeWithStringValue()
    {
        $value = '+1 day';
        $cookie = Cookie::create('foo', 'bar', $value);
        $expire = strtotime($value);

        $this->assertEqualsWithDelta($expire$cookie->getExpiresTime(), 1, '->getExpiresTime() returns the expire date');

        $cookie = Cookie::create('foo')->withValue('bar')->withExpires($value);

        $this->assertEqualsWithDelta($expire$cookie->getExpiresTime(), 1, '->getExpiresTime() returns the expire date');
    }

    public function testGetDomain()
    {
        $cookie = Cookie::create('foo', 'bar', 0, '/', '.myfoodomain.com');

        $this->assertEquals('.myfoodomain.com', $cookie->getDomain(), '->getDomain() returns the domain name on which the cookie is valid');

        

        return new Serializer(
            new SerializerComponent\Serializer([new ObjectNormalizer()new ArrayDenormalizer()]['json' => new JsonEncoder()])
        );
    }

    private function assertApproximateDuration($startTime, int $expectedDuration)
    {
        $actualDuration = microtime(true) - $startTime;

        if (method_exists($this, 'assertEqualsWithDelta')) {
            $this->assertEqualsWithDelta($expectedDuration$actualDuration, .5, 'Duration was not within expected range');
        } else {
            $this->assertEquals($expectedDuration$actualDuration, 'Duration was not within expected range', .5);
        }
    }

    /** * @return Envelope[] */
    private function receiveEnvelopes(ReceiverInterface $receiver, int $timeout): array
    {
        $envelopes = [];
        
$this->getSession()->evaluateScript('location.hash = "#edit-body-0-value";');
    $this->getSession()->evaluateScript('scroll(0, document.documentElement.scrollTop + 110);');
    // Focus CKEditor 5 text editor.     $javascript = <<<JS Drupal.CKEditor5Instances.get(document.getElementById("edit-body-0-value").dataset["ckeditor5Id"]).editing.view.focus(); JS;
    $this->getSession()->evaluateScript($javascript);

    $this->assertNotEmpty($assert_session->waitForElementVisible('css', '.ck-sticky-panel__placeholder'));
    $toolbar_height = (int) $this->getSession()->evaluateScript('document.getElementById("toolbar-bar").offsetHeight');
    $ckeditor5_toolbar_position = (int) $this->getSession()->evaluateScript("document.querySelector('.ck-toolbar').getBoundingClientRect().top");
    $this->assertEqualsWithDelta($toolbar_height$ckeditor5_toolbar_position, 2);
  }

}

class KeyTest extends TestCase
{
    public function testSerialize()
    {
        $key = new Key(__METHOD__);
        $key->reduceLifetime(1);
        $key->setState('foo', 'bar');

        $copy = unserialize(serialize($key));
        $this->assertSame($key->getState('foo')$copy->getState('foo'));
        $this->assertEqualsWithDelta($key->getRemainingLifetime()$copy->getRemainingLifetime(), 0.001);
    }

    public function testUnserialize()
    {
        $key = new Key(__METHOD__);
        $key->markUnserializable();

        $this->expectException(UnserializableKeyException::class);
        serialize($key);
    }
}

        );

        $store->lock($request);

        static::assertTrue($item->get());

        $reflectionClass = new \ReflectionClass($item);
        $prop = $reflectionClass->getProperty('expiry');
        $prop->setAccessible(true);

        static::assertEqualsWithDelta(time() + 3, $prop->getValue($item), 1);
    }
}
$fs->write('a', 'test');

        static::expectException(UnableToRetrieveMetadata::class);
        $fs->mimeType('a');
    }

    public function testLastModified(): void
    {
        $fs = new Filesystem(new MemoryFilesystemAdapter());
        $fs->write('a', 'test');

        static::assertEqualsWithDelta($fs->lastModified('a')time(), 2);

        static::expectException(UnableToRetrieveMetadata::class);
        $fs->lastModified('ab');
    }

    public function testCopy(): void
    {
        $fs = new Filesystem(new MemoryFilesystemAdapter());

        $fs->write('bla.txt', 'foo');
        $fs->copy('bla.txt', 'foo.txt');
        
public function testTtl()
    {
        foreach ([60, fn () => 60] as $ttl) {
            $pdo = $this->getMemorySqlitePdo();
            $storage = new PdoSessionHandler($pdo['ttl' => $ttl]);

            $storage->open('', 'sid');
            $storage->read('id');
            $storage->write('id', 'data');
            $storage->close();

            $this->assertEqualsWithDelta(time() + 60, $pdo->query('SELECT sess_lifetime FROM sessions')->fetchColumn(), 5);
        }
    }

    /** * @return resource */
    private function createStream($content)
    {
        $stream = tmpfile();
        fwrite($stream$content);
        fseek($stream, 0);

        
public function testWaitIntervalOnConsumeOverLimit()
    {
        $limiter = $this->createLimiter();

        // initial consume         $limiter->consume(8);
        // consumer over the limit         $rateLimit = $limiter->consume(4);

        $start = microtime(true);
        $rateLimit->wait(); // wait 1 minute         $this->assertEqualsWithDelta($start + 60, microtime(true), 1);
    }

    public function testWrongWindowFromCache()
    {
        $this->storage->save(new DummyWindow());
        $limiter = $this->createLimiter();
        $rateLimit = $limiter->consume();
        $this->assertTrue($rateLimit->isAccepted());
        $this->assertEquals(9, $rateLimit->getRemainingTokens());
    }

    
Home | Imprint | This part of the site doesn't use cookies.