SpecifyTest.php 10.2 KB
Newer Older
Marojahan Sigiro committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328
<?php

class SpecifyTest extends \SpecifyUnitTest
{
    protected $user;
    protected $a;

    private $private = false;

    public function testSpecification()
    {
        $this->user = new stdClass();
        $this->user->name = 'davert';
        $this->specify("i can change my name", function() {
           $this->user->name = 'jon';
           $this->assertEquals('jon', $this->user->name);
        });

        $this->assertEquals('davert', $this->user->name);

        $this->specify('i can fail here but test goes on', function() {
            $this->markTestIncomplete();
        });
        $this->assertTrue(true);
    }

    function testBeforeCallback()
    {
        $this->beforeSpecify(function() {
            $this->user = "davert";
        });
        $this->specify("user should be davert", function() {
            $this->assertEquals('davert', $this->user);
        });
    }

    function testMultiBeforeCallback()
    {
        $this->beforeSpecify(function() {
            $this->user = "davert";
        });
        $this->beforeSpecify(function() {
            $this->user .= "jon";
        });
        $this->specify("user should be davertjon", function() {
            $this->assertEquals('davertjon', $this->user);
        });
    }

    function testAfterCallback()
    {
        $this->afterSpecify(function() {
            $this->user = "davert";
        });
        $this->specify("user should be davert", function() {
            $this->user = "jon";
        });
        $this->assertEquals('davert', $this->user);
    }

    function testMultiAfterCallback()
    {
        $this->afterSpecify(function() {
            $this->user = "davert";
        });
        $this->afterSpecify(function() {
            $this->user .= "jon";
        });
        $this->specify("user should be davertjon", function() {
            $this->user = "jon";
        });
        $this->assertEquals('davertjon', $this->user);
    }

    function testCleanSpecifyCallbacks()
    {
        $this->afterSpecify(function() {
            $this->user = "davert";
        });
        $this->cleanSpecify();
        $this->specify("user should be davert", function() {
            $this->user = "jon";
        });
        $this->assertNull($this->user);
    }

    public function testExceptions()
    {
        $this->specify('user is invalid', function() {
            throw new Exception;
        }, ['throws' => 'Exception']);

        $this->specify('user is invalid', function() {
            throw new RuntimeException;
        }, ['throws' => 'RuntimeException']);

        $this->specify('user is invalid', function() {
            throw new RuntimeException;
        }, ['throws' => new RuntimeException()]);

        $this->specify('i can handle fails', function() {
            $this->fail("Ok, I'm failing");
        }, ['throws' => 'fail']);
    }

    public function testExceptionsWithMessages()
    {
        $this->specify('user is invalid', function() {
            throw new Exception("test message");
        }, ['throws' => ['Exception', 'test message']]);

        $this->specify('user is invalid', function() {
            throw new RuntimeException("test message");
        }, ['throws' => ['RuntimeException', 'test message']]);

        $this->specify('user is invalid', function() {
            throw new RuntimeException("test message");
        }, ['throws' => [new RuntimeException(), "test message"]]);

        $this->specify('i can handle fails', function() {
            $this->fail("test message");
        }, ['throws' => ['fail', 'test message']]);

        $this->specify('ignores an empty message', function() {
            $this->fail("test message");
        }, ['throws' => ['fail']]);

        $this->specify('mixed case exception messages', function() {
            throw new RuntimeException("teSt mESSage");
        }, ['throws' => ['RuntimeException', 'Test MessaGE']]);
    }

    /**
     * @expectedException RuntimeException
     */
    public function testFailWhenUnexpectedExceptionHappens()
    {
        $this->specify('i bubble exception up if no throws is defined', function() {
            throw new RuntimeException;
        });
    }

    public function testExamples()
    {
        $this->specify('specify may contain examples', function($a, $b) {
            $this->assertEquals($b, $a*$a);
        }, ['examples' => [
            ['2', '4'],
            ['3', '9']
        ]]);
    }

    function testOnlySpecifications()
    {
        $this->specify('should be valid');
    }

    public function testDeepCopy()
    {
        $this->a = new TestOne();
        $this->a->prop = new TestOne();
        $this->a->prop->prop = 1;
        $this->specify('nested object can be changed', function() {
            $this->assertEquals(1, $this->a->prop->prop);
            $this->a->prop->prop = 2;
            $this->assertEquals(2, $this->a->prop->prop);
        });
        $this->assertEquals(1, $this->a->prop->prop);

    }

    public function testConfiguration()
    {
        $this->specifyConfig()
            ->ignore('user');

        $this->specify("user should be jon", function() {
            $this->user = "jon";
        });

        $this->specifyConfig()
            ->ignore(['user']);

        $this->specify("user should be davert", function() {
            $this->user = "davert";
        });

        $this->a = new TestOne();
        $this->a->prop = new TestOne();
        $this->a->prop->prop = 1;

        $this->specifyConfig()
            ->shallowClone('a');

        $this->specify("user should be davert", function() {
            $this->a->prop->prop = "davert";
        });

        $this->assertEquals("davert", $this->a->prop->prop);
    }

    public function testCloneOnly()
    {
        $this->specifyConfig()
            ->cloneOnly('user');

        $this->user = "bob";
        $this->a = "rob";
        $this->specify("user should be jon", function() {
            $this->user = "jon";
            $this->a = 'alice';
        });
        $this->assertEquals('bob', $this->user);
        $this->assertEquals('alice', $this->a);
    }

    /**
     * @Issue https://github.com/Codeception/Specify/issues/6
     */
    function testPropertyRestore()
    {
        $this->testOne = new testOne();
        $this->testOne->prop = ['hello', 'world'];

        $this->specify('array contains hello+world', function ($testData) {
            $this->testOne->prop = ['bye', 'world'];
            $this->assertContains($testData, $this->testOne->prop);
        }, ['examples' => [
            ['bye'],
            ['world'],
        ]]);

        $this->assertEquals(['hello', 'world'], $this->testOne->prop);
        $this->assertFalse($this->private);
        $this->assertTrue($this->getPrivateProperty());

        $this->specify('property $private should be restored properly', function() {
            $this->private = 'i\'m protected';
            $this->setPrivateProperty('i\'m private');
            $this->assertEquals('i\'m private', $this->getPrivateProperty());
        });

        $this->assertFalse($this->private);
        $this->assertTrue($this->getPrivateProperty());
    }

    public function testExamplesIndexInName()
    {
        $name = $this->getName();

        $this->specify('it appends index of an example to a test case name', function ($idx, $example) use ($name) {
            $name .= ' | it appends index of an example to a test case name';
            $this->assertEquals($name . ' | examples index ' . $idx, $this->getName());

            $this->specify('nested specification without examples', function () use ($idx, $name) {
                $name .= ' | examples index ' . $idx;
                $name .= ' | nested specification without examples';
                $this->assertEquals($name, $this->getName());
            });

            $this->specify('nested specification with examples', function () use ($idx, $name) {
                $name .= ' | examples index ' . $idx;
                $name .= ' | nested specification with examples';
                $name .= ' | examples index 0';
                $this->assertEquals($name, $this->getName());
            }, ['examples' => [
                [$example]
            ]]);
        }, ['examples' => [
            [0, ''],
            [1, '0'],
            [2, null],
            [3, 'bye'],
            [4, 'world'],
        ]]);

        $this->specify('it does not append index to a test case name if there are no examples', function () use ($name) {
            $name .= ' | it does not append index to a test case name if there are no examples';
            $this->assertEquals($name, $this->getName());

            $this->specify('nested specification without examples', function () use ($name) {
                $this->assertEquals($name . ' | nested specification without examples', $this->getName());
            });

            $this->specify('nested specification with examples', function () use ($name) {
                $this->assertEquals($name . ' | nested specification with examples | examples index 0', $this->getName());
            }, ['examples' => [
                [null]
            ]]);
        });
    }

    public function testMockObjectsIsolation()
    {
        $mock = $this->getMock(get_class($this), ['testMockObjectsIsolation']);
        $mock->expects($this->once())->method('testMockObjectsIsolation');

        $this->specify('this should fail', function () {
            $mock = $this->getMock(get_class($this), ['testMockObjectsIsolation']);
            $mock->expects($this->exactly(100500))->method('testMockObjectsIsolation');
        }, ['throws' => 'PHPUnit_Framework_ExpectationFailedException']);

        $this->specify('this should not fail', function () {
            $mock = $this->getMock(get_class($this), ['testMockObjectsIsolation']);
            $mock->expects($this->never())->method('testMockObjectsIsolation');
        });

        $mock->testMockObjectsIsolation();
    }

//    public function testFail()
//    {
//        $this->specify('this will fail', function(){
//            $this->assertTrue(false);
//        });
//
//        $this->specify('this will fail too', function(){
//            echo "executed";
//            $this->assertTrue(true);
//        }, ['throws' => 'Exception']);
//    }

}

class TestOne
{
    public $prop;
}