| 1 |
lars |
1 |
<?php
|
|
|
2 |
/**
|
|
|
3 |
* Unit tests for HTTP_Request2 package
|
|
|
4 |
*
|
|
|
5 |
* PHP version 5
|
|
|
6 |
*
|
|
|
7 |
* LICENSE:
|
|
|
8 |
*
|
|
|
9 |
* Copyright (c) 2008-2011, Alexey Borzov <avb@php.net>
|
|
|
10 |
* All rights reserved.
|
|
|
11 |
*
|
|
|
12 |
* Redistribution and use in source and binary forms, with or without
|
|
|
13 |
* modification, are permitted provided that the following conditions
|
|
|
14 |
* are met:
|
|
|
15 |
*
|
|
|
16 |
* * Redistributions of source code must retain the above copyright
|
|
|
17 |
* notice, this list of conditions and the following disclaimer.
|
|
|
18 |
* * Redistributions in binary form must reproduce the above copyright
|
|
|
19 |
* notice, this list of conditions and the following disclaimer in the
|
|
|
20 |
* documentation and/or other materials provided with the distribution.
|
|
|
21 |
* * The names of the authors may not be used to endorse or promote products
|
|
|
22 |
* derived from this software without specific prior written permission.
|
|
|
23 |
*
|
|
|
24 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
|
|
|
25 |
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
|
|
|
26 |
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
|
|
27 |
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
|
|
|
28 |
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
|
|
29 |
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
|
|
30 |
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
|
|
31 |
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
|
|
|
32 |
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
|
|
33 |
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
|
|
34 |
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
35 |
*
|
|
|
36 |
* @category HTTP
|
|
|
37 |
* @package HTTP_Request2
|
|
|
38 |
* @author Alexey Borzov <avb@php.net>
|
|
|
39 |
* @license http://opensource.org/licenses/bsd-license.php New BSD License
|
|
|
40 |
* @version SVN: $Id: ObserverTest.php 309665 2011-03-24 21:03:48Z avb $
|
|
|
41 |
* @link http://pear.php.net/package/HTTP_Request2
|
|
|
42 |
*/
|
|
|
43 |
|
|
|
44 |
/**
|
|
|
45 |
* Class representing a HTTP request
|
|
|
46 |
*/
|
|
|
47 |
require_once 'HTTP/Request2.php';
|
|
|
48 |
|
|
|
49 |
/** Helper for PHPUnit includes */
|
|
|
50 |
require_once dirname(__FILE__) . '/TestHelper.php';
|
|
|
51 |
|
|
|
52 |
/**
|
|
|
53 |
* Mock observer
|
|
|
54 |
*/
|
|
|
55 |
class HTTP_Request2_MockObserver implements SplObserver
|
|
|
56 |
{
|
|
|
57 |
public $calls = 0;
|
|
|
58 |
|
|
|
59 |
public $event;
|
|
|
60 |
|
|
|
61 |
public function update (SplSubject $subject)
|
|
|
62 |
{
|
|
|
63 |
$this->calls++;
|
|
|
64 |
$this->event = $subject->getLastEvent();
|
|
|
65 |
}
|
|
|
66 |
}
|
|
|
67 |
|
|
|
68 |
/**
|
|
|
69 |
* Unit test for subject-observer pattern implementation in HTTP_Request2
|
|
|
70 |
*/
|
|
|
71 |
class HTTP_Request2_ObserverTest extends PHPUnit_Framework_TestCase
|
|
|
72 |
{
|
|
|
73 |
public function testSetLastEvent()
|
|
|
74 |
{
|
|
|
75 |
$request = new HTTP_Request2();
|
|
|
76 |
$observer = new HTTP_Request2_MockObserver();
|
|
|
77 |
$request->attach($observer);
|
|
|
78 |
|
|
|
79 |
$request->setLastEvent('foo', 'bar');
|
|
|
80 |
$this->assertEquals(1, $observer->calls);
|
|
|
81 |
$this->assertEquals(array('name' => 'foo', 'data' => 'bar'), $observer->event);
|
|
|
82 |
|
|
|
83 |
$request->setLastEvent('baz');
|
|
|
84 |
$this->assertEquals(2, $observer->calls);
|
|
|
85 |
$this->assertEquals(array('name' => 'baz', 'data' => null), $observer->event);
|
|
|
86 |
}
|
|
|
87 |
|
|
|
88 |
public function testAttachOnlyOnce()
|
|
|
89 |
{
|
|
|
90 |
$request = new HTTP_Request2();
|
|
|
91 |
$observer = new HTTP_Request2_MockObserver();
|
|
|
92 |
$observer2 = new HTTP_Request2_MockObserver();
|
|
|
93 |
$request->attach($observer);
|
|
|
94 |
$request->attach($observer2);
|
|
|
95 |
$request->attach($observer);
|
|
|
96 |
|
|
|
97 |
$request->setLastEvent('event', 'data');
|
|
|
98 |
$this->assertEquals(1, $observer->calls);
|
|
|
99 |
$this->assertEquals(1, $observer2->calls);
|
|
|
100 |
}
|
|
|
101 |
|
|
|
102 |
public function testDetach()
|
|
|
103 |
{
|
|
|
104 |
$request = new HTTP_Request2();
|
|
|
105 |
$observer = new HTTP_Request2_MockObserver();
|
|
|
106 |
$observer2 = new HTTP_Request2_MockObserver();
|
|
|
107 |
|
|
|
108 |
$request->attach($observer);
|
|
|
109 |
$request->detach($observer2); // should not be a error
|
|
|
110 |
$request->setLastEvent('first');
|
|
|
111 |
|
|
|
112 |
$request->detach($observer);
|
|
|
113 |
$request->setLastEvent('second');
|
|
|
114 |
$this->assertEquals(1, $observer->calls);
|
|
|
115 |
$this->assertEquals(array('name' => 'first', 'data' => null), $observer->event);
|
|
|
116 |
}
|
|
|
117 |
}
|
|
|
118 |
?>
|