Skip to content

Commit

Permalink
Adding some unit tests that were missing and integrating the project …
Browse files Browse the repository at this point in the history
…with Travis CI
  • Loading branch information
klaussilveira committed Mar 28, 2012
1 parent 7ba2de0 commit 002f8be
Show file tree
Hide file tree
Showing 6 changed files with 224 additions and 160 deletions.
5 changes: 5 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
language: php
php:
- 5.3
- 5.4
script: phpunit tests
1 change: 1 addition & 0 deletions README.markdown
Original file line number Diff line number Diff line change
@@ -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
Expand Down
159 changes: 0 additions & 159 deletions SimpleSHM.class.php

This file was deleted.

182 changes: 182 additions & 0 deletions SimpleSHM.php
Original file line number Diff line number Diff line change
@@ -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);
}
}
2 changes: 1 addition & 1 deletion demo.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

require('SimpleSHM.class.php');
require('SimpleSHM.php');

/**
* Creating new block, with a random ID
Expand Down
35 changes: 35 additions & 0 deletions tests/SimpleSHMTest.php
Original file line number Diff line number Diff line change
@@ -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.