init(); } /** * @param $expectedPath * @param $actualUrl * @param array $options */ protected static function assertAssetUrl( $expectedPath, $actualUrl, $options = [] ) { $message = ArrayUtils::get($options, 'message'); $protocol = ArrayUtils::get($options, 'protocol', 'https'); $hostname = ArrayUtils::get($options, 'hostname', UrlConfig::DEFAULT_SHARED_HOST); $cloudName = ArrayUtils::get($options, 'cloud_name', self::CLOUD_NAME); $privateCdn = ArrayUtils::get($options, 'private_cdn', false); if (! $privateCdn) { $hostAndCloud = ArrayUtils::implodeUrl([$hostname, $cloudName]); } else { $hostAndCloud = ArrayUtils::implodeFiltered('-', [$cloudName, $hostname]); } $suffix = ArrayUtils::get($options, 'suffix', ''); $expectedUrl = "$protocol://$hostAndCloud/$expectedPath$suffix"; self::assertEquals($expectedUrl, (string)$actualUrl, $message); } /** * @param $expectedPath * @param $actualUrl * @param array $options */ protected static function assertImageUrl( $expectedPath, $actualUrl, $options = [] ) { $deliveryType = ArrayUtils::get($options, 'delivery_type', DeliveryType::UPLOAD); $expectedPath = AssetType::IMAGE . "/$deliveryType/$expectedPath"; self::assertAssetUrl($expectedPath, $actualUrl, $options); } /** * @param $expectedPath * @param $actualUrl * @param null $message * @param string $customDeliveryType */ protected static function assertVideoUrl( $expectedPath, $actualUrl, $message = null, $customDeliveryType = DeliveryType::UPLOAD ) { $expectedPath = AssetType::VIDEO . "/$customDeliveryType/$expectedPath"; self::assertAssetUrl($expectedPath, $actualUrl, $message); } /** * @param $expectedPath * @param $actualUrl * @param null $message * @param string $customDeliveryType */ protected static function assertFileUrl( $expectedPath, $actualUrl, $message = null, $customDeliveryType = DeliveryType::UPLOAD ) { $expectedPath = AssetType::RAW . "/$customDeliveryType/$expectedPath"; self::assertAssetUrl($expectedPath, $actualUrl, $message); } /** * @param $source * @param mixed $actualUrl * @param string $assetType * @param array $expectationOptions */ protected static function assertAssetFromParamsUrl( $source, $actualUrl, $assetType, $expectationOptions = [] ) { ArrayUtils::setDefaultValue($expectationOptions, 'protocol', 'http'); $path = ArrayUtils::get($expectationOptions, 'path'); $deliveryType = ArrayUtils::get($expectationOptions, 'delivery_type', DeliveryType::UPLOAD); $actualSource = ArrayUtils::get($expectationOptions, 'source', $source); // Expected full path overrides everything $expectedPath = ArrayUtils::get( $expectationOptions, 'full_path', "$assetType/$deliveryType/" . ArrayUtils::implodeUrl([$path, $actualSource]) ); self::assertAssetUrl($expectedPath, $actualUrl, $expectationOptions); } /** * @param $source * @param array $params * @param array $expectationOptions */ protected static function assertMediaFromParamsUrl( $source, $params = [], $expectationOptions = [] ) { $actualUrl = Media::fromParams($source, $params); $assetType = ArrayUtils::get($expectationOptions, 'asset_type', AssetType::IMAGE); self::assertAssetFromParamsUrl($source, $actualUrl, $assetType, $expectationOptions); } /** * @param $source * @param array $params * @param array $expectationOptions */ protected static function assertFileFromParamsUrl( $source, $params = [], $expectationOptions = [] ) { $actualUrl = File::fromParams($source, $params); $assetType = ArrayUtils::get($expectationOptions, 'asset_type', AssetType::RAW); self::assertAssetFromParamsUrl($source, $actualUrl, $assetType, $expectationOptions); } /** * @param callable $function */ protected static function assertErrorThrowing(callable $function) { $errorsThrown = 0; set_error_handler( static function () use (&$errorsThrown) { $errorsThrown++; return true; } ); try { $function(); } catch (Exception $e) { $errorsThrown++; } restore_error_handler(); self::assertEquals(1, $errorsThrown, 'Failed assert that error was thrown'); } }