<commit_before><?php
/**
 * Zend Framework (http://framework.zend.com/)
 *
 * @link           http://github.com/zendframework/zf2 for the canonical source repository
 * @copyright      Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
 * @license        http://framework.zend.com/license/new-bsd New BSD License
 */

namespace ZendTest\Stdlib;

use Zend\Stdlib\Hydrator\Filter\FilterComposite;

class FilterCompositeTest extends \PHPUnit_Framework_TestCase
{
    protected $filterComposite;

    public function setUp()
    {
        $this->filterComposite = new FilterComposite();
    }

    public function testValidationAdd()
    {
        $this->assertTrue($this->filterComposite->filter("foo"));
        $this->filterComposite->addFilter("has",
            function ($property) {
                return false;
            }
        );
        $this->assertFalse($this->filterComposite->filter("foo"));
    }

    public function testValidationRemove()
    {
        $this->filterComposite->addFilter("has",
            function ($property) {
                return false;
            }
        );
        $this->assertFalse($this->filterComposite->filter("foo"));
        $this->filterComposite->removeFilter("has");
        $this->assertTrue($this->filterComposite->filter("foo"));
    }

    public function testValidationHas()
    {
        $this->filterComposite->addFilter("has",
            function ($property) {
                return false;
            }
        );
        $this->assertFalse($this->filterComposite->filter("foo"));
        $this->assertTrue($this->filterComposite->hasFilter("has"));
    }

    public function testComplexValidation()
    {
        $this->filterComposite->addFilter("has", new \Zend\Stdlib\Hydrator\Filter\HasFilter());
        $this->filterComposite->addFilter("get", new \Zend\Stdlib\Hydrator\Filter\GetFilter());
        $this->filterComposite->addFilter("is", new \Zend\Stdlib\Hydrator\Filter\IsFilter());

        $this->filterComposite->addFilter("exclude",
            function ($property) {
                $method = substr($property, strpos($property, '::'));

                if ($method === 'getServiceLocator') {
                    return false;
                }

                return true;
            }, FilterComposite::CONDITION_AND
        );

        $this->assertTrue($this->filterComposite->filter('getFooBar'));
        $this->assertFalse($this->filterComposite->filter('getServiceLocator'));
    }

    public function testConstructorInjection()
    {
        $andCondition = array(
            'servicelocator' => function ($property) {
                if ($property === 'getServiceLocator') {
                    return false;
                }
                return true;
            },
            'foobar' => function ($property) {
                if ($property === 'getFooBar') {
                    return false;
                }
                return true;
            }
        );
        $orCondition = array(
            'has' => new \Zend\Stdlib\Hydrator\Filter\HasFilter(),
            'get' => new \Zend\Stdlib\Hydrator\Filter\GetFilter()
        );
        $filterComposite = new FilterComposite($orCondition, $andCondition);

        $this->assertFalse($filterComposite->filter('getFooBar'));
        $this->assertFalse($filterComposite->filter('geTFooBar'));
        $this->assertFalse($filterComposite->filter('getServiceLocator'));
        $this->assertTrue($filterComposite->filter('getFoo'));
        $this->assertTrue($filterComposite->filter('hasFoo'));
    }

    public function testWithOnlyAndFiltersAdded()
    {
        $filter = new FilterComposite();
        $filter->addFilter("foobarbaz", function ($property) {
                return true;
            }, FilterComposite::CONDITION_AND);
        $filter->addFilter("foobar", function ($property) {
                return true;
            }, FilterComposite::CONDITION_AND);
        $this->assertTrue($filter->filter("foo"));
    }

    public function testWithOnlyOrFiltersAdded()
    {
        $filter = new FilterComposite();
        $filter->addFilter("foobarbaz", function ($property) {
                return true;
            });
        $filter->addFilter("foobar", function ($property) {
                return false;
            });
        $this->assertTrue($filter->filter("foo"));
    }

    public function testWithComplexCompositeAdded()
    {
        $filter1 = new FilterComposite();
        $filter1->addFilter("foobarbaz", function ($property) {
                return true;
            });
        $filter1->addFilter("foobar", function ($property) {
                return false;
            });
        $filter2 = new FilterComposite();
        $filter2->addFilter("bar", function ($property) {
                return true;
            }, FilterComposite::CONDITION_AND);
        $filter2->addFilter("barblubb", function ($property) {
                return true;
            }, FilterComposite::CONDITION_AND);
        $this->assertTrue($filter1->filter("foo"));
        $this->assertTrue($filter2->filter("foo"));
        $filter1->addFilter("bar", $filter2);
        $this->assertTrue($filter1->filter("blubb"));

        $filter1->addFilter("blubb", function ($property) { return false; }, FilterComposite::CONDITION_AND);
        $this->assertFalse($filter1->filter("test"));
    }

    /**
     * @expectedException Zend\Stdlib\Exception\InvalidArgumentException
     * @expectedExceptionMessage The value of test should be either a callable<commit_msg>Ensure substr receives an integer 2nd parameter<commit_after><?php
/**
 * Zend Framework (http://framework.zend.com/)
 *
 * @link           http://github.com/zendframework/zf2 for the canonical source repository
 * @copyright      Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
 * @license        http://framework.zend.com/license/new-bsd New BSD License
 */

namespace ZendTest\Stdlib;

use Zend\Stdlib\Hydrator\Filter\FilterComposite;

class FilterCompositeTest extends \PHPUnit_Framework_TestCase
{
    protected $filterComposite;

    public function setUp()
    {
        $this->filterComposite = new FilterComposite();
    }

    public function testValidationAdd()
    {
        $this->assertTrue($this->filterComposite->filter("foo"));
        $this->filterComposite->addFilter(
            'has',
            function ($property) {
                return false;
            }
        );
        $this->assertFalse($this->filterComposite->filter("foo"));
    }

    public function testValidationRemove()
    {
        $this->filterComposite->addFilter(
            'has',
            function ($property) {
                return false;
            }
        );
        $this->assertFalse($this->filterComposite->filter("foo"));
        $this->filterComposite->removeFilter("has");
        $this->assertTrue($this->filterComposite->filter("foo"));
    }

    public function testValidationHas()
    {
        $this->filterComposite->addFilter(
            'has',
            function ($property) {
                return false;
            }
        );
        $this->assertFalse($this->filterComposite->filter("foo"));
        $this->assertTrue($this->filterComposite->hasFilter("has"));
    }

    public function testComplexValidation()
    {
        $this->filterComposite->addFilter("has", new \Zend\Stdlib\Hydrator\Filter\HasFilter());
        $this->filterComposite->addFilter("get", new \Zend\Stdlib\Hydrator\Filter\GetFilter());
        $this->filterComposite->addFilter("is", new \Zend\Stdlib\Hydrator\Filter\IsFilter());

        $this->filterComposite->addFilter(
            'exclude',
            function ($property) {
                $separatorPos = strpos($property, '::') ?: 0;
                $method = substr($property, $separatorPos);

                if ($method === 'getServiceLocator') {
                    return false;
                }

                return true;
            },
            FilterComposite::CONDITION_AND
        );

        $this->assertTrue($this->filterComposite->filter('getFooBar'));
        $this->assertFalse($this->filterComposite->filter('getServiceLocator'));
    }

    public function testConstructorInjection()
    {
        $andCondition = array(
            'servicelocator' => function ($property) {
                if ($property === 'getServiceLocator') {
                    return false;
                }
                return true;
            },
            'foobar' => function ($property) {
                if ($property === 'getFooBar') {
                    return false;
                }
                return true;
            }
        );
        $orCondition = array(
            'has' => new \Zend\Stdlib\Hydrator\Filter\HasFilter(),
            'get' => new \Zend\Stdlib\Hydrator\Filter\GetFilter()
        );
        $filterComposite = new FilterComposite($orCondition, $andCondition);

        $this->assertFalse($filterComposite->filter('getFooBar'));
        $this->assertFalse($filterComposite->filter('geTFooBar'));
        $this->assertFalse($filterComposite->filter('getServiceLocator'));
        $this->assertTrue($filterComposite->filter('getFoo'));
        $this->assertTrue($filterComposite->filter('hasFoo'));
    }

    public function testWithOnlyAndFiltersAdded()
    {
        $filter = new FilterComposite();
        $filter->addFilter("foobarbaz", function ($property) {
            return true;
        }, FilterComposite::CONDITION_AND);
        $filter->addFilter("foobar", function ($property) {
            return true;
        }, FilterComposite::CONDITION_AND);
        $this->assertTrue($filter->filter("foo"));
    }

    public function testWithOnlyOrFiltersAdded()
    {
        $filter = new FilterComposite();
        $filter->addFilter("foobarbaz", function ($property) {
            return true;
        });
        $filter->addFilter("foobar", function ($property) {
            return false;
        });
        $this->assertTrue($filter->filter("foo"));
    }

    public function testWithComplexCompositeAdded()
    {
        $filter1 = new FilterComposite();
        $filter1->addFilter("foobarbaz", function ($property) {
            return true;
        });
        $filter1->addFilter("foobar", function ($property) {
            return false;
        });
        $filter2 = new FilterComposite();
        $filter2->addFilter("bar", function ($property) {
            return true;
        }, FilterComposite::CONDITION_AND);
        $filter2->addFilter("barblubb", function ($property) {
            return true;
        }, FilterComposite::CONDITION_AND);
        $this->assertTrue($filter1->filter("foo"));
        $this->assertTrue($filter2->filter("foo"));
        $filter1->addFilter("bar", $filter2);
        $this->assertTrue($filter1->filter("blubb"));

        $filter1->addFilter("blubb", function ($property) {
            return false;
        }, FilterComposite::CONDITION_AND);
        $this->assertFalse($filter1->filter("test"));
    }

    /**
     * @expectedException Zend\Stdlib\Exception\InvalidArgumentException
     * @expectedExceptionMessage The value of test should be either a callable
{'fraction_non_alphanumeric': 0.14346742613029548, 'fraction_numerical': 0.004093983624065504, 'mean_word_length': 2.270372526193248, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 157, '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': '7240582', 'n_tokens_mistral': 3040, 'n_tokens_neox': 2933, 'n_words': 478}