Permalink
Browse files

Adding some unit tests that were missing and integrating the project …

…with Travis CI
  • Loading branch information...
klaussilveira committed Mar 28, 2012
1 parent 7ba2de0 commit 002f8beecc884c025d32326fa3510314f1c76c90
Showing with 224 additions and 160 deletions.
  1. +5 −0 .travis.yml
  2. +1 −0 README.markdown
  3. +0 −159 SimpleSHM.class.php
  4. +182 −0 SimpleSHM.php
  5. +1 −1 demo.php
  6. +35 −0 tests/SimpleSHMTest.php
View
@@ -0,0 +1,5 @@
+language: php
+php:
+ - 5.3
+ - 5.4
+script: phpunit tests
View
@@ -1,4 +1,5 @@
# SimpleSHM
+[![Build Status](https://secure.travis-ci.org/klaussilveira/SimpleSHM.png)](http://travis-ci.org/klaussilveira/SimpleSHM)
SimpleSHM is a simple and small abstraction layer for shared memory manipulation using PHP. It makes use of the SHMOP functions, built into most PHP packages.
## Authors and contributors
View
@@ -1,159 +0,0 @@
-<?php
-/**
- * SimpleSHM
- *
- * A simple and small abstraction layer for Shared Memory manipulation using PHP
- *
- * @author Klaus Silveira <contact@klaussilveira.com>
- * @package simpleshm
- * @license http://www.opensource.org/licenses/bsd-license.php BSD License
- * @version 0.1
- */
-class SimpleSHM {
-
- /**
- * Holds the system id for the shared memory block
- *
- * @var int
- * @access private
- */
- private $id;
-
- /**
- * Holds the shared memory block id returned by shmop_open
- *
- * @var int
- * @access private
- */
- private $shmid;
-
- /**
- * Holds the default permission (octal) that will be used in created memory blocks
- *
- * @var int
- * @access private
- */
- private $perms = 0644;
-
-
- /**
- * Shared memory block instantiation
- *
- * In the constructor we'll check if the block we're going to manipulate
- * already exists or needs to be created. If it exists, let's open it.
- *
- * @access public
- * @param string $id (optional) ID of the shared memory block you want to manipulate
- */
- public function __construct($id = null) {
- if($id === null) {
- $this->id = $this->generateID();
- } else {
- $this->id = $id;
-
- if($this->exists($this->id)) {
- $this->shmid = shmop_open($this->id, "w", 0, 0);
- }
- }
- }
-
- /**
- * Generates a random ID for a shared memory block
- *
- * @access private
- * @return int Randomly generated ID, between 1 and 65535
- */
- private function generateID() {
- $id = mt_rand(1, 65535);
- return $id;
- }
-
- /**
- * Checks if a shared memory block with the provided exists or not
- *
- * In order to check for shared memory existance, we have to open it with
- * reading access. If it doesn't exist, warnings will be cast, therefore we
- * suppress those with the @ operator.
- *
- * @access private
- * @param string $id ID of the shared memory block you want to check
- * @return boolean True if the block exists, false if it doesn't
- */
- private function exists($id) {
- $status = @shmop_open($id, "a", 0, 0);
-
- if($status) {
- $status = true;
- } else {
- $status = false;
- }
-
- return $status;
- }
-
- /**
- * Sets the default permission (octal) that will be used in created memory blocks
- *
- * @access public
- * @param string $perms Permission, in octal form, that will be used in created memory blocks
- */
- public function setPermissions($perms) {
- $this->perms = $perms;
- }
-
- /**
- * Writes on a shared memory block
- *
- * First we check for the block existance, and if it doesn't, we'll create it. Now, if the
- * block already exists, we need to delete it and create it again with a new byte allocation that
- * matches the size of the data that we want to write there. We mark for deletion, close the semaphore
- * and create it again.
- *
- * @access public
- * @param string $data The data that you wan't to write into the shared memory block
- */
- public function write($data) {
- $size = mb_strlen($data, 'UTF-8');
-
- if($this->exists($this->id)) {
- shmop_delete($this->shmid);
- shmop_close($this->shmid);
- $this->shmid = shmop_open($this->id, "c", $this->perms, $size);
- shmop_write($this->shmid, $data, 0);
- } else {
- $this->shmid = shmop_open($this->id, "c", $this->perms, $size);
- shmop_write($this->shmid, $data, 0);
- }
- }
-
- /**
- * Reads from a shared memory block
- *
- * @access public
- * @return string The data read from the shared memory block
- */
- public function read() {
- $size = shmop_size($this->shmid);
- $data = shmop_read($this->shmid, 0, $size);
-
- return $data;
- }
-
- /**
- * Mark a shared memory block for deletion
- *
- * @access public
- */
- public function delete() {
- shmop_delete($this->shmid);
- }
-
- /**
- * Closes the shared memory block and stops manipulation
- *
- * @access public
- */
- public function __destruct() {
- shmop_close($this->shmid);
- }
-}
View
@@ -0,0 +1,182 @@
+<?php
+
+/**
+ * SimpleSHM
+ *
+ * A simple and small abstraction layer for Shared Memory manipulation using PHP
+ *
+ * @author Klaus Silveira <contact@klaussilveira.com>
+ * @package simpleshm
+ * @license http://www.opensource.org/licenses/bsd-license.php BSD License
+ * @version 0.1
+ */
+class SimpleSHM
+{
+ /**
+ * Holds the system id for the shared memory block
+ *
+ * @var int
+ * @access protected
+ */
+ protected $id;
+
+ /**
+ * Holds the shared memory block id returned by shmop_open
+ *
+ * @var int
+ * @access protected
+ */
+ protected $shmid;
+
+ /**
+ * Holds the default permission (octal) that will be used in created memory blocks
+ *
+ * @var int
+ * @access protected
+ */
+ protected $perms = 0644;
+
+
+ /**
+ * Shared memory block instantiation
+ *
+ * In the constructor we'll check if the block we're going to manipulate
+ * already exists or needs to be created. If it exists, let's open it.
+ *
+ * @access public
+ * @param string $id (optional) ID of the shared memory block you want to manipulate
+ */
+ public function __construct($id = null)
+ {
+ if($id === null) {
+ $this->id = $this->generateID();
+ } else {
+ $this->id = $id;
+
+ if($this->exists($this->id)) {
+ $this->shmid = shmop_open($this->id, "w", 0, 0);
+ }
+ }
+ }
+
+ /**
+ * Generates a random ID for a shared memory block
+ *
+ * @access protected
+ * @return int Randomly generated ID, between 1 and 65535
+ */
+ protected function generateID()
+ {
+ $id = mt_rand(1, 65535);
+ return $id;
+ }
+
+ /**
+ * Checks if a shared memory block with the provided id exists or not
+ *
+ * In order to check for shared memory existance, we have to open it with
+ * reading access. If it doesn't exist, warnings will be cast, therefore we
+ * suppress those with the @ operator.
+ *
+ * @access public
+ * @param string $id ID of the shared memory block you want to check
+ * @return boolean True if the block exists, false if it doesn't
+ */
+ public function exists($id)
+ {
+ $status = @shmop_open($id, "a", 0, 0);
+
+ return $status;
+ }
+
+ /**
+ * Writes on a shared memory block
+ *
+ * First we check for the block existance, and if it doesn't, we'll create it. Now, if the
+ * block already exists, we need to delete it and create it again with a new byte allocation that
+ * matches the size of the data that we want to write there. We mark for deletion, close the semaphore
+ * and create it again.
+ *
+ * @access public
+ * @param string $data The data that you wan't to write into the shared memory block
+ */
+ public function write($data)
+ {
+ $size = mb_strlen($data, 'UTF-8');
+
+ if($this->exists($this->id)) {
+ shmop_delete($this->shmid);
+ shmop_close($this->shmid);
+ $this->shmid = shmop_open($this->id, "c", $this->perms, $size);
+ shmop_write($this->shmid, $data, 0);
+ } else {
+ $this->shmid = shmop_open($this->id, "c", $this->perms, $size);
+ shmop_write($this->shmid, $data, 0);
+ }
+ }
+
+ /**
+ * Reads from a shared memory block
+ *
+ * @access public
+ * @return string The data read from the shared memory block
+ */
+ public function read()
+ {
+ $size = shmop_size($this->shmid);
+ $data = shmop_read($this->shmid, 0, $size);
+
+ return $data;
+ }
+
+ /**
+ * Mark a shared memory block for deletion
+ *
+ * @access public
+ */
+ public function delete()
+ {
+ shmop_delete($this->shmid);
+ }
+
+ /**
+ * Gets the current shared memory block id
+ *
+ * @access public
+ */
+ public function getId()
+ {
+ return $this->id;
+ }
+
+ /**
+ * Gets the current shared memory block permissions
+ *
+ * @access public
+ */
+ public function getPermissions()
+ {
+ return $this->perms;
+ }
+
+ /**
+ * Sets the default permission (octal) that will be used in created memory blocks
+ *
+ * @access public
+ * @param string $perms Permissions, in octal form
+ */
+ public function setPermissions($perms)
+ {
+ $this->perms = $perms;
+ }
+
+ /**
+ * Closes the shared memory block and stops manipulation
+ *
+ * @access public
+ */
+ public function __destruct()
+ {
+ shmop_close($this->shmid);
+ }
+}
View
@@ -1,6 +1,6 @@
<?php
-require('SimpleSHM.class.php');
+require('SimpleSHM.php');
/**
* Creating new block, with a random ID
View
@@ -0,0 +1,35 @@
+<?php
+
+require 'SimpleSHM.php';
+
+class SimpleSHMTest extends PHPUnit_Framework_TestCase
+{
+ public function testIsCreatingNewBlock()
+ {
+ $memory = new SimpleSHM;
+ $this->assertInstanceOf('SimpleSHM', $memory);
+
+ $memory->write('Sample');
+ $data = $memory->read();
+ $this->assertEquals('Sample', $data);
+ }
+
+ public function testIsCreatingNewBlockWithId()
+ {
+ $memory = new SimpleSHM(897);
+ $this->assertInstanceOf('SimpleSHM', $memory);
+ $this->assertEquals(897, $memory->getId());
+
+ $memory->write('Sample 2');
+ $data = $memory->read();
+ $this->assertEquals('Sample 2', $data);
+ }
+
+ public function testIsMarkingBlockForDeletion()
+ {
+ $memory = new SimpleSHM(897);
+ $memory->delete();
+ $data = $memory->read();
+ $this->assertEquals('Sample 2', $data);
+ }
+}

0 comments on commit 002f8be

Please sign in to comment.