<commit_before><?php

namespace PiPHP\Test\GPIO;

use PiPHP\GPIO\FileSystem\FileSystemInterface;
use PiPHP\GPIO\Pin;
use PiPHP\GPIO\PinFactory;

class PinTest extends \PHPUnit_Framework_TestCase
{
    const TEST_PIN_NUMBER = 5;

    public function testGetNumber()
    {
        $pin = $this->getPin();
        $this->assertEquals(self::TEST_PIN_NUMBER, $pin->getNumber());
    }

    public function testDirection()
    {
        $pin = $this->getPin('/sys/class/gpio/gpio' . self::TEST_PIN_NUMBER . '/direction');
        $pin->setDirection(Pin::DIRECTION_IN);
        $this->assertEquals(Pin::DIRECTION_IN, $pin->getDirection());
    }

    public function testValue()
    {
        $pin = $this->getPin('/sys/class/gpio/gpio' . self::TEST_PIN_NUMBER . '/value');
        $pin->setValue(Pin::VALUE_HIGH);
        $this->assertEquals(Pin::VALUE_HIGH, $pin->getValue());
    }

    public function testEdge()
    {
        $pin = $this->getPin('/sys/class/gpio/gpio' . self::TEST_PIN_NUMBER . '/edge');
        $pin->setEdge(Pin::EDGE_RISING);
        $this->assertEquals(Pin::EDGE_RISING, $pin->getEdge());
    }

    private function getPin($expectedFile = null)
    {
        $mockFileSystem = new FileSystemMock($expectedFile);
        return (new PinFactory($mockFileSystem))->getPin(self::TEST_PIN_NUMBER);
    }

    public function testExport()
    {
        $exportFile = '/sys/class/gpio/export';

        $mockFileSystem = new FileSystemMock($exportFile);
        $pin = (new PinFactory($mockFileSystem))->getPin(self::TEST_PIN_NUMBER);
        $pin->export();

        $exportStream = $mockFileSystem->open($exportFile, 'r');
        $this->assertEquals(self::TEST_PIN_NUMBER, $exportStream->read(1024));
    }

    public function testUnexport()
    {
        $unexportFile = '/sys/class/gpio/unexport';

        $mockFileSystem = new FileSystemMock($unexportFile);
        $pin = (new PinFactory($mockFileSystem))->getPin(self::TEST_PIN_NUMBER);
        $pin->unexport();

        $unexportStream = $mockFileSystem->open($unexportFile, 'r');
        $this->assertEquals(self::TEST_PIN_NUMBER, $unexportStream->read(1024));
    }
}

class FileSystemMock implements FileSystemInterface<commit_msg>Update PinTest.php<commit_after><?php

namespace PiPHP\Test\GPIO;

use PiPHP\GPIO\FileSystem\FileSystemInterface;
use PiPHP\GPIO\Pin;
use PiPHP\GPIO\PinFactory;

class PinTest extends \PHPUnit_Framework_TestCase
{
    const TEST_PIN_NUMBER = 5;

    public function testGetNumber()
    {
        $pin = $this->getPin();
        $this->assertEquals(self::TEST_PIN_NUMBER, $pin->getNumber());
    }

    public function testDirection()
    {
        $pin = $this->getPin('/sys/class/gpio/gpio' . self::TEST_PIN_NUMBER . '/direction');
        $pin->setDirection(Pin::DIRECTION_IN);
        $this->assertEquals(Pin::DIRECTION_IN, $pin->getDirection());
    }

    public function testValue()
    {
        $pin = $this->getPin('/sys/class/gpio/gpio' . self::TEST_PIN_NUMBER . '/value');
        $pin->setValue(Pin::VALUE_HIGH);
        $this->assertEquals(Pin::VALUE_HIGH, $pin->getValue());
    }

    public function testEdge()
    {
        $pin = $this->getPin('/sys/class/gpio/gpio' . self::TEST_PIN_NUMBER . '/edge');
        $pin->setEdge(Pin::EDGE_RISING);
        $this->assertEquals(Pin::EDGE_RISING, $pin->getEdge());
    }

    private function getPin($expectedFile = null)
    {
        $mockFileSystem = new FileSystemMock($expectedFile);
        return (new PinFactory($mockFileSystem))->getPin(self::TEST_PIN_NUMBER);
    }

    public function testExport()
    {
        $exportFile = '/sys/class/gpio/export';

        $mockFileSystem = new FileSystemMock($exportFile);
        $pin = (new PinFactory($mockFileSystem))->getPin(self::TEST_PIN_NUMBER);
        $pin->export();

        $this->assertEquals(self::TEST_PIN_NUMBER, $mockFileSystem->getContents($exportFile));
    }

    public function testUnexport()
    {
        $unexportFile = '/sys/class/gpio/unexport';

        $mockFileSystem = new FileSystemMock($unexportFile);
        $pin = (new PinFactory($mockFileSystem))->getPin(self::TEST_PIN_NUMBER);
        $pin->unexport();

        $this->assertEquals(self::TEST_PIN_NUMBER, $mockFileSystem->getContents($unexportFile));
    }
}

class FileSystemMock implements FileSystemInterface
{'fraction_non_alphanumeric': 0.1542910016192459, 'fraction_numerical': 0.002313208420078649, 'mean_word_length': 3.380952380952381, 'pattern_counts': {'":': 0, '<': 5, '<?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': '1498889', 'n_tokens_mistral': 1353, 'n_tokens_neox': 1330, 'n_words': 196}