<commit_before><?php
/**
 * TestProviders.php
 *
 * @package Tests
 * @author <NAME> <<EMAIL>>
 * @link   http://www.michael-pratt.com/
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Embera;

use Embera\Embera;
use Embera\ProviderCollection\DefaultProviderCollection;
use Embera\Http\OembedClient;
use Embera\Http\HttpClient;

use PHPUnit\Framework\TestCase;
use ReflectionClass;

class ProviderTester extends TestCase
{
    /** @var array with urls */
    protected $tasks;

    /**
     * Returns an array with random data from an array
     *
     * @param array $data
     * @param int $count
     *
     * @return array
     *
     */
    protected function getRandomDataFrom(array $data, $count = 1)
    {
        if ($count >= count($data)) {
            return $data;
        }

        shuffle($data);
        return array_slice($data, 0, $count);
    }

    /**
     * This is where it gets nasty.
     *
     * This method is used to validate the behaviour of a  Provider.
     * All the other methods found down here, are used to test different
     * parts of the provider
     *
     * Now, every provider has a test file which extends this class
     * and uses this method to validate everything.
     *
     * Why? I had so much duplicated code...
     * I know it looks ugly, but, its more convenient.
     *
     * @param string $p The provider Name
     * @param array $config Additional config options
     * @return void
     *
     * @large
     */
    protected function validateProvider($p, array $config = [])
    {
        // Use all available valid urls?
        $rounds = (defined('FULL_TEST') && FULL_TEST ? 1000 : 1);

        if (empty($this->tasks)) {
            throw new Exception(sprintf('The Provider %s doesnt have tasks', $p));
        }

        $this->validateUrlDetection($p, $this->tasks, $config);
        $this->validateCollectionDetection($p, $this->tasks, $config);
        if (!empty($this->tasks['normalize_urls'])) {
            $this->validateUrlNormalization($p, $this->tasks['normalize_urls'], $config);
        }

        if (!empty($this->tasks['fake_response'])) {
            $this->validateFakeResponse($p, $this->tasks, $rounds, $config);
        }

        $this->validateRealResponse($p, $this->tasks, $rounds, $config);
    }

    /**
     * Checks if the main library detects this provider
     *
     * @param string $provider Provider Name
     * @param array $tasks
     * @param array $config
     * @return void
     *
     * @medium
     */
    protected function validateCollectionDetection($providerName, array $tasks, array $config = [])
    {
        $config = array_merge([
            'https_only' => false,
            'fake_responses' => Embera::ALLOW_FAKE_RESPONSES,
            'maxwidth' => 430,
            'maxheight' => 270,
        ], $config);

        $collection = new DefaultProviderCollection($config);
        $providers = $collection->findProviders($tasks['valid_urls']);

        foreach ($providers as $p) {
            $this->assertEquals($p->getProviderName(), $providerName);
        }
    }

    /**
     * Checks if all valid urls are correctly detected
     *
     * @param string $provider Provider Name
     * @param array $tasks
     * @param array $config
     * @return void
     *
     * @medium
     */
    protected function validateUrlDetection($providerName, array $tasks, array $config = [])
    {
        $validCount = 0;
        $validUrlCount = count($tasks['valid_urls']);

        foreach ($tasks['valid_urls'] as $valid) {
            $reflection = new ReflectionClass('Embera\Provider\\' . $providerName);
            $provider = $reflection->newInstance($valid, $config);
            if ($provider->validateUrl($provider->getUrl(false))) {
                $validCount++;
            } else {
                $this->fail(sprintf('The url %s was not recognized as valid', $valid));
            }

        }<commit_msg>Really test that the collection returns providers<commit_after><?php
/**
 * TestProviders.php
 *
 * @package Tests
 * @author <NAME> <<EMAIL>>
 * @link   http://www.michael-pratt.com/
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Embera;

use Embera\Embera;
use Embera\ProviderCollection\DefaultProviderCollection;
use Embera\Http\OembedClient;
use Embera\Http\HttpClient;

use PHPUnit\Framework\TestCase;
use ReflectionClass;

class ProviderTester extends TestCase
{
    /** @var array with urls */
    protected $tasks;

    /**
     * Returns an array with random data from an array
     *
     * @param array $data
     * @param int $count
     *
     * @return array
     *
     */
    protected function getRandomDataFrom(array $data, $count = 1)
    {
        if ($count >= count($data)) {
            return $data;
        }

        shuffle($data);
        return array_slice($data, 0, $count);
    }

    /**
     * This is where it gets nasty.
     *
     * This method is used to validate the behaviour of a  Provider.
     * All the other methods found down here, are used to test different
     * parts of the provider
     *
     * Now, every provider has a test file which extends this class
     * and uses this method to validate everything.
     *
     * Why? I had so much duplicated code...
     * I know it looks ugly, but, its more convenient.
     *
     * @param string $p The provider Name
     * @param array $config Additional config options
     * @return void
     *
     * @large
     */
    protected function validateProvider($p, array $config = [])
    {
        // Use all available valid urls?
        $rounds = (defined('FULL_TEST') && FULL_TEST ? 1000 : 1);

        if (empty($this->tasks)) {
            throw new Exception(sprintf('The Provider %s doesnt have tasks', $p));
        }

        $this->validateUrlDetection($p, $this->tasks, $config);
        $this->validateCollectionDetection($p, $this->tasks, $config);
        if (!empty($this->tasks['normalize_urls'])) {
            $this->validateUrlNormalization($p, $this->tasks['normalize_urls'], $config);
        }

        if (!empty($this->tasks['fake_response'])) {
            $this->validateFakeResponse($p, $this->tasks, $rounds, $config);
        }

        $this->validateRealResponse($p, $this->tasks, $rounds, $config);
    }

    /**
     * Checks if the main library detects this provider
     *
     * @param string $provider Provider Name
     * @param array $tasks
     * @param array $config
     * @return void
     *
     * @medium
     */
    protected function validateCollectionDetection($providerName, array $tasks, array $config = [])
    {
        $config = array_merge([
            'https_only' => false,
            'fake_responses' => Embera::ALLOW_FAKE_RESPONSES,
            'maxwidth' => 430,
            'maxheight' => 270,
        ], $config);

        $collection = new DefaultProviderCollection($config);
        $providers = $collection->findProviders($tasks['valid_urls']);

        $this->assertFalse(empty($providers));
        foreach ($providers as $p) {
            $this->assertEquals($p->getProviderName(), $providerName);
        }
    }

    /**
     * Checks if all valid urls are correctly detected
     *
     * @param string $provider Provider Name
     * @param array $tasks
     * @param array $config
     * @return void
     *
     * @medium
     */
    protected function validateUrlDetection($providerName, array $tasks, array $config = [])
    {
        $validCount = 0;
        $validUrlCount = count($tasks['valid_urls']);

        foreach ($tasks['valid_urls'] as $valid) {
            $reflection = new ReflectionClass('Embera\Provider\\' . $providerName);
            $provider = $reflection->newInstance($valid, $config);
            if ($provider->validateUrl($provider->getUrl(false))) {
                $validCount++;
            } else {
                $this->fail(sprintf('The url %s was not recognized as valid', $valid));
            }

        }
{'fraction_non_alphanumeric': 0.12602773346422874, 'fraction_numerical': 0.0034360044177199657, 'mean_word_length': 2.3442757488715635, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 60, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5028245', 'n_tokens_mistral': 2343, 'n_tokens_neox': 2274, 'n_words': 651}