Skip to content

Commit d152542

Browse files
committed
Added mock registers for better testing
1 parent b9a8ab5 commit d152542

File tree

3 files changed

+82
-6
lines changed

3 files changed

+82
-6
lines changed

src/PHPi/Board.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,32 +30,32 @@ abstract class Board implements BoardInterface {
3030
*
3131
* Register for gpio functions
3232
*/
33-
private $gpio_register;
33+
protected $gpio_register;
3434

3535
/**
3636
* @var Register\PWM
3737
*/
38-
private $pwm_register;
38+
protected $pwm_register;
3939

4040
/**
4141
* @var Register\Clock
4242
*/
43-
private $clock_register;
43+
protected $clock_register;
4444

4545
/**
4646
* @var Register\SPI
4747
*/
48-
private $spi_register;
48+
protected $spi_register;
4949

5050
/**
5151
* @var Register\Auxiliary
5252
*/
53-
private $aux_register;
53+
protected $aux_register;
5454

5555
/**
5656
* @var EdgeDetector\EdgeDetectorInterface
5757
*/
58-
private $edge_detector;
58+
protected $edge_detector;
5959

6060
/**
6161
* @var Pin[]

src/PHPi/Board/Mock.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,23 @@
88
namespace Calcinai\PHPi\Board;
99

1010
use Calcinai\PHPi\Board;
11+
use React\EventLoop\LoopInterface;
12+
use Calcinai\PHPi\Peripheral\Register;
1113

1214
class Mock extends Board {
1315

16+
public function __construct(LoopInterface $loop)
17+
{
18+
parent::__construct($loop);
19+
20+
$this->gpio_register = new Register\Mock($this);
21+
$this->spi_register = new Register\Mock($this);
22+
$this->pwm_register = new Register\Mock($this);
23+
$this->aux_register = new Register\Mock($this);
24+
$this->clock_register = new Register\Mock($this);
25+
26+
}
27+
1428
public static function getPeripheralBaseAddress() {
1529
return 0;
1630
}

src/PHPi/Peripheral/Register/Mock.php

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php
2+
/**
3+
* @package api
4+
* @author Michael Calcinai <[email protected]>
5+
*/
6+
7+
namespace Calcinai\PHPi\Peripheral\Register;
8+
use Calcinai\PHPi\Board\BoardInterface;
9+
10+
/**
11+
* Register to store and return data, so testing can be done on non-pi systems
12+
*
13+
* Class Mock
14+
* @package Calcinai\PHPi\Peripheral\Register
15+
*/
16+
class Mock extends AbstractRegister
17+
{
18+
19+
private $data;
20+
21+
/**
22+
* Overload the parent constructor so nothing is actually mapped
23+
*
24+
* AbstractRegister constructor.
25+
* @param BoardInterface $board
26+
*/
27+
/** @noinspection PhpMissingParentConstructorInspection */
28+
public function __construct(BoardInterface $board)
29+
{
30+
$this->data = [];
31+
}
32+
33+
/**
34+
* @param mixed $offset
35+
* @return bool
36+
*/
37+
38+
public function offsetExists($offset)
39+
{
40+
return isset($this->data[$offset]);
41+
}
42+
43+
public function offsetGet($offset)
44+
{
45+
return isset($this->data[$offset]) ? $this->data[$offset] : 0;
46+
}
47+
48+
public function offsetSet($offset, $value)
49+
{
50+
$this->data[$offset] = $value;
51+
}
52+
53+
public function offsetUnset($offset)
54+
{
55+
56+
}
57+
58+
public static function getOffset()
59+
{
60+
return 0;
61+
}
62+
}

0 commit comments

Comments
 (0)