|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * CROSS PHPunit Utils |
| 4 | + * |
| 5 | + * @filesource |
| 6 | + * @copyright 2019 Cross Solution <https://www.cross-solution.de> |
| 7 | + * @license MIT |
| 8 | + */ |
| 9 | + |
| 10 | +declare(strict_types=1); |
| 11 | + |
| 12 | +namespace Cross\TestUtilsTest\TestCase; |
| 13 | + |
| 14 | +use Cross\TestUtils\Exception\InvalidUsageException; |
| 15 | + |
| 16 | +use Cross\TestUtils\TestCase\CreateProphecyTrait; |
| 17 | + |
| 18 | +use Prophecy\Prophecy\ObjectProphecy; |
| 19 | + |
| 20 | +/** |
| 21 | + * Tests for \Cross\TestUtils\TestCase\CreateProphecyTrait |
| 22 | + * |
| 23 | + * @covers \Cross\TestUtils\TestCase\CreateProphecyTrait |
| 24 | + * @author Mathias Gelhausen <gelhausen@cross-solution.de> |
| 25 | + * |
| 26 | + * @group Cross.TestUtils |
| 27 | + * @group Cross.TestUtils.TestCase |
| 28 | + * @group Cross.TestUtils.TestCase.CreateProphecyTrait |
| 29 | + */ |
| 30 | +class CreateProphecyTraitTest extends \PHPUnit_Framework_TestCase |
| 31 | +{ |
| 32 | + private $target; |
| 33 | + |
| 34 | + public function setup() |
| 35 | + { |
| 36 | + $this->target = new class |
| 37 | + { |
| 38 | + use CreateProphecyTrait; |
| 39 | + |
| 40 | + public $prophecy; |
| 41 | + |
| 42 | + public function prophesize($class) |
| 43 | + { |
| 44 | + $this->prophecy = new class($class) extends ObjectProphecy |
| 45 | + { |
| 46 | + public $class; |
| 47 | + public $called = []; |
| 48 | + |
| 49 | + public function __construct($class) |
| 50 | + { |
| 51 | + $this->class = $class; |
| 52 | + } |
| 53 | + |
| 54 | + public function __call($method, $args) |
| 55 | + { |
| 56 | + $this->called[$method][] = $args; |
| 57 | + return $this; |
| 58 | + } |
| 59 | + |
| 60 | + public function willExtend($class) |
| 61 | + { |
| 62 | + return $this->__call(__FUNCTION__, [$class]); |
| 63 | + } |
| 64 | + |
| 65 | + public function willImplement($interface) |
| 66 | + { |
| 67 | + return $this->__call(__FUNCTION__, [$interface]); |
| 68 | + } |
| 69 | + |
| 70 | + public function willBeConstructedWith( |
| 71 | + array $arguments = null |
| 72 | + ) { |
| 73 | + return $this->__call(__FUNCTION__, [$arguments]); |
| 74 | + } |
| 75 | + |
| 76 | + public function reveal() |
| 77 | + { |
| 78 | + return $this->__call(__FUNCTION__, []); |
| 79 | + } |
| 80 | + }; |
| 81 | + |
| 82 | + return $this->prophecy; |
| 83 | + } |
| 84 | + }; |
| 85 | + } |
| 86 | + |
| 87 | + public function testThrowsExceptionIfInvalidSpecificationIsPassed() |
| 88 | + { |
| 89 | + $this->expectException(InvalidUsageException::class); |
| 90 | + $this->expectExceptionMessage('Expected string or array'); |
| 91 | + |
| 92 | + $this->target->createProphecy(new \stdClass); |
| 93 | + } |
| 94 | + |
| 95 | + public function testThrowsExceptionIfFqcnNotFound() |
| 96 | + { |
| 97 | + $this->expectException(InvalidUsageException::class); |
| 98 | + $this->expectExceptionMessage('No FQCN found'); |
| 99 | + |
| 100 | + $this->target->createProphecy([]); |
| 101 | + } |
| 102 | + |
| 103 | + public function testCreateSimpleProphecy() |
| 104 | + { |
| 105 | + $prophecy = $this->target->createProphecy(\stdClass::class); |
| 106 | + |
| 107 | + static::assertEquals(\stdClass::class, $prophecy->class); |
| 108 | + } |
| 109 | + |
| 110 | + public function testCreateProphecyFromSimpleArray() |
| 111 | + { |
| 112 | + $prophecy = $this->target->createProphecy([\stdClass::class, 'arg1']); |
| 113 | + |
| 114 | + static::assertArrayHasKey('willBeConstructedWith', $prophecy->called); |
| 115 | + static::assertEquals([[1 => 'arg1']], $prophecy->called['willBeConstructedWith'][0]); |
| 116 | + } |
| 117 | + |
| 118 | + public function testCreateProphecyFromArrayWithArguments() |
| 119 | + { |
| 120 | + $prophecy = $this->target->createProphecy([\stdClass::class, 'arguments' => ['arg1']]); |
| 121 | + |
| 122 | + static::assertArrayHasKey('willBeConstructedWith', $prophecy->called); |
| 123 | + static::assertEquals([['arg1']], $prophecy->called['willBeConstructedWith'][0]); |
| 124 | + } |
| 125 | + |
| 126 | + public function testCreateProphecyThatExtendsAndImplements() |
| 127 | + { |
| 128 | + $prophecy = $this->target->createProphecy([ |
| 129 | + \stdClass::class, |
| 130 | + 'extends' => \ArrayObject::class, |
| 131 | + 'implements' => [\Serializable::class, \Countable::class] |
| 132 | + ]); |
| 133 | + |
| 134 | + static::assertArrayHasKey('willExtend', $prophecy->called); |
| 135 | + static::assertArrayHasKey('willImplement', $prophecy->called); |
| 136 | + static::assertEquals([\ArrayObject::class], $prophecy->called['willExtend'][0]); |
| 137 | + static::assertEquals([[\Serializable::class], [\Countable::class]], $prophecy->called['willImplement']); |
| 138 | + } |
| 139 | + |
| 140 | + public function testCreateProphecyWithMethodPromises() |
| 141 | + { |
| 142 | + $prophecies = [ |
| 143 | + ['method1', 'willReturn' => 'test'], |
| 144 | + ['method2', 'other' => ['arg1', 'arg2']] |
| 145 | + ]; |
| 146 | + |
| 147 | + $prophecy = $this->target->createProphecy(\stdClass::class, $prophecies); |
| 148 | + |
| 149 | + static::assertArrayHasKey('method1', $prophecy->called); |
| 150 | + static::assertArrayHasKey('willReturn', $prophecy->called); |
| 151 | + static::assertEquals(['test'], $prophecy->called['willReturn'][0]); |
| 152 | + |
| 153 | + static::assertArrayHasKey('method2', $prophecy->called); |
| 154 | + static::assertArrayHasKey('other', $prophecy->called); |
| 155 | + static::assertEquals(['arg1', 'arg2'], $prophecy->called['other'][0]); |
| 156 | + } |
| 157 | + |
| 158 | + public function testCreateDouble() |
| 159 | + { |
| 160 | + $prophecy = $this->target->createDouble(\stdClass::class); |
| 161 | + |
| 162 | + static::assertArrayHasKey('reveal', $prophecy->called); |
| 163 | + } |
| 164 | +} |
0 commit comments