Skip to content
This repository
Browse code

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

…with Travis CI
  • Loading branch information...
commit 002f8beecc884c025d32326fa3510314f1c76c90 1 parent 7ba2de0
Klaus Silveira authored March 28, 2012
5  .travis.yml
... ...
@@ -0,0 +1,5 @@
  1
+language: php
  2
+php:
  3
+  - 5.3
  4
+  - 5.4
  5
+script: phpunit tests
1  README.markdown
Source Rendered
... ...
@@ -1,4 +1,5 @@
1 1
 # SimpleSHM
  2
+[![Build Status](https://secure.travis-ci.org/klaussilveira/SimpleSHM.png)](http://travis-ci.org/klaussilveira/SimpleSHM)
2 3
 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. 
3 4
 
4 5
 ## Authors and contributors
159  SimpleSHM.class.php
... ...
@@ -1,159 +0,0 @@
1  
-<?php
2  
-/**
3  
- * SimpleSHM
4  
- *
5  
- * A simple and small abstraction layer for Shared Memory manipulation using PHP
6  
- *
7  
- * @author Klaus Silveira <contact@klaussilveira.com>
8  
- * @package simpleshm
9  
- * @license http://www.opensource.org/licenses/bsd-license.php BSD License
10  
- * @version 0.1
11  
- */
12  
-class SimpleSHM {
13  
-	
14  
-	/**
15  
-	 * Holds the system id for the shared memory block
16  
-	 *
17  
-	 * @var int
18  
-	 * @access private
19  
-	 */
20  
-	private $id;
21  
-	
22  
-	/**
23  
-	 * Holds the shared memory block id returned by shmop_open
24  
-	 *
25  
-	 * @var int
26  
-	 * @access private
27  
-	 */
28  
-	private $shmid;
29  
-	
30  
-	/**
31  
-	 * Holds the default permission (octal) that will be used in created memory blocks
32  
-	 *
33  
-	 * @var int
34  
-	 * @access private
35  
-	 */
36  
-	private $perms = 0644;
37  
-	
38  
-	
39  
-	/**
40  
-	 * Shared memory block instantiation
41  
-	 *
42  
-	 * In the constructor we'll check if the block we're going to manipulate
43  
-	 * already exists or needs to be created. If it exists, let's open it.
44  
-	 *
45  
-	 * @access public
46  
-	 * @param string $id (optional) ID of the shared memory block you want to manipulate
47  
-	 */
48  
-	public function __construct($id = null) {
49  
-		if($id === null) {
50  
-			$this->id = $this->generateID();
51  
-		} else {
52  
-			$this->id = $id;
53  
-			
54  
-			if($this->exists($this->id)) {
55  
-				$this->shmid = shmop_open($this->id, "w", 0, 0);
56  
-			}
57  
-		}
58  
-	}
59  
-	
60  
-	/**
61  
-	 * Generates a random ID for a shared memory block
62  
-	 *
63  
-	 * @access private
64  
-	 * @return int Randomly generated ID, between 1 and 65535
65  
-	 */
66  
-	private function generateID() {
67  
-		$id = mt_rand(1, 65535);
68  
-		return $id;
69  
-	}
70  
-	
71  
-	/**
72  
-	 * Checks if a shared memory block with the provided exists or not
73  
-	 * 
74  
-	 * In order to check for shared memory existance, we have to open it with
75  
-	 * reading access. If it doesn't exist, warnings will be cast, therefore we
76  
-	 * suppress those with the @ operator.
77  
-	 * 
78  
-	 * @access private
79  
-	 * @param string $id ID of the shared memory block you want to check
80  
-	 * @return boolean True if the block exists, false if it doesn't
81  
-	 */
82  
-	private function exists($id) {
83  
-		$status = @shmop_open($id, "a", 0, 0);
84  
-		
85  
-		if($status) {
86  
-			$status = true;
87  
-		} else {
88  
-			$status = false;
89  
-		}
90  
-		
91  
-		return $status;
92  
-	}
93  
-	
94  
-	/**
95  
-	 * Sets the default permission (octal) that will be used in created memory blocks
96  
-	 * 
97  
-	 * @access public
98  
-	 * @param string $perms Permission, in octal form, that will be used in created memory blocks
99  
-	 */
100  
-	public function setPermissions($perms) {
101  
-		$this->perms = $perms;
102  
-	}
103  
-	
104  
-	/**
105  
-	 * Writes on a shared memory block
106  
-	 * 
107  
-	 * First we check for the block existance, and if it doesn't, we'll create it. Now, if the
108  
-	 * block already exists, we need to delete it and create it again with a new byte allocation that
109  
-	 * matches the size of the data that we want to write there. We mark for deletion,  close the semaphore 
110  
-	 * and create it again.
111  
-	 * 
112  
-	 * @access public
113  
-	 * @param string $data The data that you wan't to write into the shared memory block
114  
-	 */
115  
-	public function write($data) {
116  
-		$size = mb_strlen($data, 'UTF-8');
117  
-		
118  
-		if($this->exists($this->id)) {
119  
-			shmop_delete($this->shmid);
120  
-			shmop_close($this->shmid);
121  
-			$this->shmid = shmop_open($this->id, "c", $this->perms, $size);
122  
-			shmop_write($this->shmid, $data, 0);
123  
-		} else {
124  
-			$this->shmid = shmop_open($this->id, "c", $this->perms, $size);
125  
-			shmop_write($this->shmid, $data, 0);
126  
-		}
127  
-	}
128  
-	
129  
-	/**
130  
-	 * Reads from a shared memory block
131  
-	 * 
132  
-	 * @access public
133  
-	 * @return string The data read from the shared memory block
134  
-	 */
135  
-	public function read() {
136  
-		$size = shmop_size($this->shmid);
137  
-		$data = shmop_read($this->shmid, 0, $size);
138  
-		
139  
-		return $data;
140  
-	}
141  
-	
142  
-	/**
143  
-	 * Mark a shared memory block for deletion
144  
-	 * 
145  
-	 * @access public
146  
-	 */
147  
-	public function delete() {
148  
-		shmop_delete($this->shmid);
149  
-	}
150  
-	
151  
-	/**
152  
-	 * Closes the shared memory block and stops manipulation
153  
-	 * 
154  
-	 * @access public
155  
-	 */
156  
-	public function __destruct() {
157  
-		shmop_close($this->shmid);
158  
-	}
159  
-}
182  SimpleSHM.php
... ...
@@ -0,0 +1,182 @@
  1
+<?php
  2
+
  3
+/**
  4
+ * SimpleSHM
  5
+ *
  6
+ * A simple and small abstraction layer for Shared Memory manipulation using PHP
  7
+ *
  8
+ * @author Klaus Silveira <contact@klaussilveira.com>
  9
+ * @package simpleshm
  10
+ * @license http://www.opensource.org/licenses/bsd-license.php BSD License
  11
+ * @version 0.1
  12
+ */
  13
+class SimpleSHM
  14
+{
  15
+    /**
  16
+     * Holds the system id for the shared memory block
  17
+     *
  18
+     * @var int
  19
+     * @access protected
  20
+     */
  21
+    protected $id;
  22
+    
  23
+    /**
  24
+     * Holds the shared memory block id returned by shmop_open
  25
+     *
  26
+     * @var int
  27
+     * @access protected
  28
+     */
  29
+    protected $shmid;
  30
+    
  31
+    /**
  32
+     * Holds the default permission (octal) that will be used in created memory blocks
  33
+     *
  34
+     * @var int
  35
+     * @access protected
  36
+     */
  37
+    protected $perms = 0644;
  38
+    
  39
+    
  40
+    /**
  41
+     * Shared memory block instantiation
  42
+     *
  43
+     * In the constructor we'll check if the block we're going to manipulate
  44
+     * already exists or needs to be created. If it exists, let's open it.
  45
+     *
  46
+     * @access public
  47
+     * @param string $id (optional) ID of the shared memory block you want to manipulate
  48
+     */
  49
+    public function __construct($id = null)
  50
+    {
  51
+        if($id === null) {
  52
+            $this->id = $this->generateID();
  53
+        } else {
  54
+            $this->id = $id;
  55
+            
  56
+            if($this->exists($this->id)) {
  57
+                $this->shmid = shmop_open($this->id, "w", 0, 0);
  58
+            }
  59
+        }
  60
+    }
  61
+    
  62
+    /**
  63
+     * Generates a random ID for a shared memory block
  64
+     *
  65
+     * @access protected
  66
+     * @return int Randomly generated ID, between 1 and 65535
  67
+     */
  68
+    protected function generateID()
  69
+    {
  70
+        $id = mt_rand(1, 65535);
  71
+        return $id;
  72
+    }
  73
+    
  74
+    /**
  75
+     * Checks if a shared memory block with the provided id exists or not
  76
+     * 
  77
+     * In order to check for shared memory existance, we have to open it with
  78
+     * reading access. If it doesn't exist, warnings will be cast, therefore we
  79
+     * suppress those with the @ operator.
  80
+     * 
  81
+     * @access public
  82
+     * @param string $id ID of the shared memory block you want to check
  83
+     * @return boolean True if the block exists, false if it doesn't
  84
+     */
  85
+    public function exists($id)
  86
+    {
  87
+        $status = @shmop_open($id, "a", 0, 0);
  88
+        
  89
+        return $status;
  90
+    }
  91
+    
  92
+    /**
  93
+     * Writes on a shared memory block
  94
+     * 
  95
+     * First we check for the block existance, and if it doesn't, we'll create it. Now, if the
  96
+     * block already exists, we need to delete it and create it again with a new byte allocation that
  97
+     * matches the size of the data that we want to write there. We mark for deletion,  close the semaphore 
  98
+     * and create it again.
  99
+     * 
  100
+     * @access public
  101
+     * @param string $data The data that you wan't to write into the shared memory block
  102
+     */
  103
+    public function write($data)
  104
+    {
  105
+        $size = mb_strlen($data, 'UTF-8');
  106
+        
  107
+        if($this->exists($this->id)) {
  108
+            shmop_delete($this->shmid);
  109
+            shmop_close($this->shmid);
  110
+            $this->shmid = shmop_open($this->id, "c", $this->perms, $size);
  111
+            shmop_write($this->shmid, $data, 0);
  112
+        } else {
  113
+            $this->shmid = shmop_open($this->id, "c", $this->perms, $size);
  114
+            shmop_write($this->shmid, $data, 0);
  115
+        }
  116
+    }
  117
+    
  118
+    /**
  119
+     * Reads from a shared memory block
  120
+     * 
  121
+     * @access public
  122
+     * @return string The data read from the shared memory block
  123
+     */
  124
+    public function read()
  125
+    {
  126
+        $size = shmop_size($this->shmid);
  127
+        $data = shmop_read($this->shmid, 0, $size);
  128
+        
  129
+        return $data;
  130
+    }
  131
+    
  132
+    /**
  133
+     * Mark a shared memory block for deletion
  134
+     * 
  135
+     * @access public
  136
+     */
  137
+    public function delete()
  138
+    {
  139
+        shmop_delete($this->shmid);
  140
+    }
  141
+    
  142
+    /**
  143
+     * Gets the current shared memory block id
  144
+     * 
  145
+     * @access public
  146
+     */
  147
+    public function getId()
  148
+    {
  149
+        return $this->id;
  150
+    }
  151
+    
  152
+    /**
  153
+     * Gets the current shared memory block permissions
  154
+     * 
  155
+     * @access public
  156
+     */
  157
+    public function getPermissions()
  158
+    {
  159
+        return $this->perms;
  160
+    }
  161
+    
  162
+    /**
  163
+     * Sets the default permission (octal) that will be used in created memory blocks
  164
+     * 
  165
+     * @access public
  166
+     * @param string $perms Permissions, in octal form
  167
+     */
  168
+    public function setPermissions($perms)
  169
+    {
  170
+        $this->perms = $perms;
  171
+    }
  172
+    
  173
+    /**
  174
+     * Closes the shared memory block and stops manipulation
  175
+     * 
  176
+     * @access public
  177
+     */
  178
+    public function __destruct()
  179
+    {
  180
+        shmop_close($this->shmid);
  181
+    }
  182
+}
2  demo.php
... ...
@@ -1,6 +1,6 @@
1 1
 <?php
2 2
 
3  
-require('SimpleSHM.class.php');
  3
+require('SimpleSHM.php');
4 4
 
5 5
 /**
6 6
  * Creating new block, with a random ID
35  tests/SimpleSHMTest.php
... ...
@@ -0,0 +1,35 @@
  1
+<?php
  2
+
  3
+require 'SimpleSHM.php';
  4
+
  5
+class SimpleSHMTest extends PHPUnit_Framework_TestCase
  6
+{    
  7
+    public function testIsCreatingNewBlock()
  8
+    {
  9
+        $memory = new SimpleSHM;
  10
+        $this->assertInstanceOf('SimpleSHM', $memory);
  11
+        
  12
+        $memory->write('Sample');
  13
+        $data = $memory->read();
  14
+        $this->assertEquals('Sample', $data);
  15
+    }
  16
+    
  17
+    public function testIsCreatingNewBlockWithId()
  18
+    {
  19
+        $memory = new SimpleSHM(897);
  20
+        $this->assertInstanceOf('SimpleSHM', $memory);
  21
+        $this->assertEquals(897, $memory->getId());
  22
+        
  23
+        $memory->write('Sample 2');
  24
+        $data = $memory->read();
  25
+        $this->assertEquals('Sample 2', $data);
  26
+    }
  27
+    
  28
+    public function testIsMarkingBlockForDeletion()
  29
+    {
  30
+        $memory = new SimpleSHM(897);
  31
+        $memory->delete();
  32
+        $data = $memory->read();
  33
+        $this->assertEquals('Sample 2', $data);
  34
+    }
  35
+}

0 notes on commit 002f8be

Please sign in to comment.
Something went wrong with that request. Please try again.