<commit_before><?php

namespace BeaucalLongThrottleTest\Adapter;

use BeaucalLongThrottle\Service\Throttle;
use BeaucalLongThrottle\Adapter\Apc as ApcAdapter;
use BeaucalLongThrottle\Apc\Apc as ApcWrapper;
use BeaucalLongThrottle\Options\ApcAdapter as ApcOptions;
use BeaucalLongThrottle\Options\Throttle as ThrottleOptions;
use BeaucalLongThrottle\Factory\LockHandleFactory;
use BeaucalLongThrottle\Lock;
use BeaucalLongThrottle\Term\DateTimeUnit;
use DateTime;

/**
 * @group beaucal_throttle
 */
class ApcTest extends \PHPUnit_Framework_TestCase {

    /**
     * @var ApcAdapter
     */
    protected $apcAdapter;

    /**
     * @var Throttle
     */
    protected $throttle;

    public function setUp() {
        parent::setUp();

        $this->apcAdapter = new ApcAdapter(
        new ApcWrapper, new ApcOptions, new LockHandleFactory
        );

        $this->throttle = new Throttle(
        $this->apcAdapter, new ThrottleOptions
        );
    }

    public function testVacuous() {
        $vacuous = ['beginTransaction', 'commit', 'rollback', 'clearExpiredLock'];
        foreach ($vacuous as $method) {
            $this->apcAdapter->$method();
        }
    }

    public function testCreateLockHandleLooped() {
        $factoryMock = $this->getMock('BeaucalLongThrottle\Factory\LockHandleFactory');
        $factoryMock->expects($this->any())
        ->method('createHandle')->will($this->returnValue(new Lock\Handle));

        $apcAdapter = new ApcAdapter(new ApcWrapper, new ApcOptions,
        $factoryMock);

        $throttle = new Throttle($apcAdapter, new ThrottleOptions);

        /**
         * First lock works, second tries to get a new handle but can't.
         */
        $ttl = new DateTimeUnit(80, 'seconds');
        $throttle->takeLock('handleOk', $ttl);
        $this->assertFalse($throttle->takeLock('handleRepeats', $ttl));
    }

    public function testCannotAdd() {
        $apcMock = $this->getMock('BeaucalLongThrottle\Apc\Apc');
        $apcMock->expects($this->any())
        ->method('add')->will($this->returnValue(false));
        $apcAdapter = new ApcAdapter(
        $apcMock, new ApcOptions, new LockHandleFactory
        );
        $throttle = new Throttle($apcAdapter, new ThrottleOptions);

        $ttl = new DateTimeUnit(80, 'seconds');
        for ($i = 0; $i < 4; $i++) {
            $this->assertFalse($throttle->takeLock(__FUNCTION__, $ttl));
        }
    }

    public function testSetLockPast() {
        $result = $this->apcAdapter->setLock('past', new DateTime('2000-01-01'));
    }

}<commit_msg>Travis CI setup.<commit_after><?php

namespace BeaucalLongThrottleTest\Adapter;

use BeaucalLongThrottle\Service\Throttle;
use BeaucalLongThrottle\Adapter\Apc as ApcAdapter;
use BeaucalLongThrottle\Apc\Apc as ApcWrapper;
use BeaucalLongThrottle\Options\ApcAdapter as ApcOptions;
use BeaucalLongThrottle\Options\Throttle as ThrottleOptions;
use BeaucalLongThrottle\Factory\LockHandleFactory;
use BeaucalLongThrottle\Lock;
use BeaucalLongThrottle\Term\DateTimeUnit;
use DateTime;
use Zend\Math\Rand;

/**
 * @group beaucal_throttle
 */
class ApcTest extends \PHPUnit_Framework_TestCase {

    /**
     * @var ApcAdapter
     */
    protected $apcAdapter;

    /**
     * @var Throttle
     */
    protected $throttle;

    public function setUp() {
        parent::setUp();

        $this->apcAdapter = new ApcAdapter(
        new ApcWrapper, new ApcOptions, new LockHandleFactory
        );

        $this->throttle = new Throttle(
        $this->apcAdapter, new ThrottleOptions
        );
    }

    public function testVacuous() {
        $vacuous = ['beginTransaction', 'commit', 'rollback', 'clearExpiredLock'];
        foreach ($vacuous as $method) {
            $this->apcAdapter->$method();
        }
    }

    public function testCreateLockHandleLooped() {
        $factoryMock = $this->getMock('BeaucalLongThrottle\Factory\LockHandleFactory');
        $factoryMock->expects($this->any())
        ->method('createHandle')->will($this->returnValue(new Lock\Handle));

        $apcAdapter = new ApcAdapter(new ApcWrapper, new ApcOptions,
        $factoryMock);

        $throttle = new Throttle($apcAdapter, new ThrottleOptions);

        /**
         * First lock works, second tries to get a new handle but can't.
         */
        $ttl = new DateTimeUnit(80, 'seconds');
        $throttle->takeLock('handleOk', $ttl);
        $this->assertFalse($throttle->takeLock('handleRepeats', $ttl));
    }

    public function testCannotAdd() {
        $apcMock = $this->getMock('BeaucalLongThrottle\Apc\Apc');
        $apcMock->expects($this->any())
        ->method('add')->will($this->returnValue(false));
        $apcAdapter = new ApcAdapter(
        $apcMock, new ApcOptions, new LockHandleFactory
        );
        $throttle = new Throttle($apcAdapter, new ThrottleOptions);

        $ttl = new DateTimeUnit(80, 'seconds');
        for ($i = 0; $i < 4; $i++) {
            $this->assertFalse($throttle->takeLock(__FUNCTION__, $ttl));
        }
    }

    public function testSetLockPast() {
        $result = $this->apcAdapter->setLock('past', new DateTime('2000-01-01'));
    }

    public function testApcExtensionWorking() {
        $rand = Rand::getString(10, 'asdf');
        apc_clear_cache();
        $this->assertTrue(apc_add($rand, true, 100));
        $this->assertFalse(apc_add($rand, true, 100));
    }

}
{'fraction_non_alphanumeric': 0.13299944351697274, 'fraction_numerical': 0.00667779632721202, 'mean_word_length': 3.0029695619896066, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 53, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '1784004', 'n_tokens_mistral': 1771, 'n_tokens_neox': 1749, 'n_words': 325}