예제 #1
0
 /**
  * Verify value getting/setting is handled correctly:
  * - get/set only if pin is enabled
  * - set only if in input mode
  * - set only boolean values
  * - get returns the same that was set
  *
  * @test
  */
 public function testValue()
 {
     $pin = new PinEmulation(12);
     $pin->disable();
     $this->assertFalse($pin->isEnabled());
     try {
         $pin->getValue();
         $this->fail('Value must not be readable if pin is disabled.');
     } catch (Exception $e) {
     }
     $pin = new PinEmulation(13);
     $pin->disable();
     $this->assertFalse($pin->isEnabled());
     try {
         $pin->setValue(false);
         $this->fail('Value must not be settable if pin is disabled.');
     } catch (Exception $e) {
     }
     $pin = new PinEmulation(14);
     $pin->enable();
     $this->assertTrue($pin->isEnabled());
     $pin->setDirection(Pin::DIRECTION_IN);
     $this->assertSame(Pin::DIRECTION_IN, $pin->getDirection());
     try {
         $pin->setValue(false);
         $this->fail('Value must not be settable if pin is in input mode.');
     } catch (Exception $e) {
     }
     $pin = new PinEmulation(15);
     $pin->enable();
     $this->assertTrue($pin->isEnabled());
     $pin->setDirection(Pin::DIRECTION_OUT);
     $this->assertSame(Pin::DIRECTION_OUT, $pin->getDirection());
     $pin->setValue(true);
     $this->assertTrue($pin->getValue());
     $pin->setValue(false);
     $this->assertFalse($pin->getValue());
     $pin->setValue(true);
     $this->assertTrue($pin->getValue());
     try {
         $pin->setValue((int) false);
         $this->fail('Only boolean values allowed.');
     } catch (Exception $e) {
     }
     try {
         $pin->setValue(null);
         $this->fail('Only boolean values allowed.');
     } catch (Exception $e) {
     }
 }
예제 #2
0
파일: Emulator.php 프로젝트: iqb/gpio
 /**
  * Write an action to the log and compare it to the expected actions
  *
  * @param PinEmulation $pin
  * @param int $action
  * @param mixed $data
  */
 protected function logAction(PinEmulation $pin, $action, $data = null)
 {
     $gpio = $pin->getNumber();
     // Log data
     if (($action & $this->logMask) !== 0) {
         $log = [$gpio, $action, $data];
         // Same event as before, duplicate events are ignored
         if (\count($this->log) === 0 || $this->log[\count($this->log) - 1] != $log) {
             $this->log[] = $log;
         }
     }
     if (($action & $this->assertMask) !== 0) {
         if (\count($this->assert) === 0) {
             if (($this->assertMode & self::ASSERT_IGNORE_MISSING) === 0) {
                 throw new Exception('Unexpected: ' . $this->formatEntry($gpio, $action, $data));
             }
         } else {
             @(list($assert_gpio, $assert_action, $assert_data) = \array_shift($this->assert));
             if ($gpio !== $assert_gpio || $action !== $assert_action || $data !== $assert_data) {
                 throw new Exception("\nExpected: " . $this->formatEntry($assert_gpio, $assert_action, $assert_data) . "\n" . "Actual:   " . $this->formatEntry($gpio, $action, $data));
             }
         }
     }
 }