From db401d972dd272959caf8e03baefff5a64e191e1 Mon Sep 17 00:00:00 2001 From: 1stthomas Date: Thu, 10 Mar 2022 23:20:44 +0100 Subject: [PATCH] #30 fix repository --- tests/unit/EnumTest.php | 66 ------- tests/unit/Lists/AbstractItemListTest.php | 217 ---------------------- tests/unit/Lists/HashListTest.php | 207 --------------------- tests/unit/Lists/ItemListTest.php | 88 --------- 4 files changed, 578 deletions(-) delete mode 100644 tests/unit/EnumTest.php delete mode 100644 tests/unit/Lists/AbstractItemListTest.php delete mode 100644 tests/unit/Lists/HashListTest.php delete mode 100644 tests/unit/Lists/ItemListTest.php diff --git a/tests/unit/EnumTest.php b/tests/unit/EnumTest.php deleted file mode 100644 index a69ea0a..0000000 --- a/tests/unit/EnumTest.php +++ /dev/null @@ -1,66 +0,0 @@ -object = new EnumImplementation(); - } - - /** - * Tears down the fixture, for example, closes a network connection. - * This method is called after a test is executed. - */ - protected function tearDown() - { - - } - - /** - * Test to get a defined constant by comparing the expected value. An undefined - * constant will throw an exception. This case is tested by this method too. - */ - public function testGetConstant() - { - $this->assertEquals('test 1', EnumImplementation::getConstant('test_1')); - - $this->expectException(UndefinedConstantException::class); - - EnumImplementation::getConstant('test_11111'); - } - - /** - * Test setting an undefined property on the enum, which should throw an - * exception. - * This functionallity is implemented by a trait. - */ - public function testSet() - { - $this->expectException(InvalidArgumentException::class); - - $this->object->test = 'Should throw an exception.'; - } - -} diff --git a/tests/unit/Lists/AbstractItemListTest.php b/tests/unit/Lists/AbstractItemListTest.php deleted file mode 100644 index 3cf03d0..0000000 --- a/tests/unit/Lists/AbstractItemListTest.php +++ /dev/null @@ -1,217 +0,0 @@ -getMockForAbstractClass($className, [], '', true, true, true, ['load']); - - $stub->expects($this->once()) - ->method('load') - ->with($this->equalTo($items)); - - $reflectedClass = new \ReflectionClass($className); - $constructor = $reflectedClass->getConstructor(); - $constructor->invoke($stub, $items); - } - - public function testCount() - { - $className = AbstractItemList::class; - $items = [1, 2, 3, 4, 5]; - - $stub = $this->getMockForAbstractClass($className, [], '', true, true, true, ['getAll']); - - $stub->expects($this->once()) - ->method('getAll') - ->willReturn($items); - - $this->assertCount(5, $stub); - } - - public function testGet() - { - $className = AbstractItemList::class; - $items = [1, 2, 3, 4, 5]; - - $stub = $this->getMockForAbstractClass($className); - - $reflectedClass = new \ReflectionClass($className); - $property = $reflectedClass->getProperty('items'); - $property->setAccessible(true); - $property->setValue($stub, $items); - - foreach ($items as $key => $value) { - $this->assertEquals($value, $stub->get($key)); - } - } - - public function testGetAll() - { - $className = AbstractItemList::class; - $items = [1, 2, 3, 4, 5]; - - $stub = $this->getMockForAbstractClass($className); - - $reflectedClass = new \ReflectionClass($className); - $property = $reflectedClass->getProperty('items'); - $property->setAccessible(true); - $property->setValue($stub, $items); - - $this->assertEquals($items, $stub->getAll()); - } - - public function testGetIterator() - { - $className = AbstractItemList::class; - $stub = $this->getMockForAbstractClass($className); - - $this->assertInstanceOf(\ArrayIterator::class, $stub->getIterator()); - } - - public function testGetNext() - { - $className = AbstractItemList::class; - - $items = [1, 20, 300, 4000, 50000]; - - $stub = $this->getMockForAbstractClass($className); - - $reflectedClass = new \ReflectionClass($className); - $property = $reflectedClass->getProperty('items'); - $property->setAccessible(true); - $property->setValue($stub, $items); - - $this->assertEquals(50000, $stub->getNext(3)); - - $this->assertEquals(null, $stub->getNext(4)); - } - - public function testGetPrevious() - { - $className = AbstractItemList::class; - - $items = [1, 20, 300, 4000, 50000]; - - $stub = $this->getMockForAbstractClass($className); - - $reflectedClass = new \ReflectionClass($className); - $property = $reflectedClass->getProperty('items'); - $property->setAccessible(true); - $property->setValue($stub, $items); - - $this->assertEquals(300, $stub->getPrevious(3)); - - $this->assertEquals(null, $stub->getPrevious(0)); - } - - public function testHas() - { - $className = AbstractItemList::class; - $items = [1, 2, 0, 3, 4, 5]; - - $stub = $this->getMockForAbstractClass($className); - - $reflectedClass = new \ReflectionClass($className); - $property = $reflectedClass->getProperty('items'); - $property->setAccessible(true); - $property->setValue($stub, $items); - - $this->assertTrue($stub->has(0)); - $this->assertTrue($stub->has(2)); - $this->assertTrue($stub->has(5)); - $this->assertFalse($stub->has(6)); - $this->assertFalse($stub->has(10)); - } - - public function testHasByValue() - { - $className = AbstractItemList::class; - $items = [1, 2, 0, 3, 4, 5]; - - $stub = $this->getMockForAbstractClass($className); - - $reflectedClass = new \ReflectionClass($className); - $property = $reflectedClass->getProperty('items'); - $property->setAccessible(true); - $property->setValue($stub, $items); - - $this->assertTrue($stub->hasByValue(0)); - $this->assertTrue($stub->hasByValue(2)); - $this->assertTrue($stub->hasByValue(5)); - $this->assertFalse($stub->hasByValue(6)); - $this->assertFalse($stub->hasByValue(10)); - } - - public function testLoad() - { - $className = AbstractItemList::class; - $items = [1, 2, 0, 3, 4, 5]; - - $stub = $this->getMockForAbstractClass($className); - - $stub->expects($this->exactly(6)) - ->method('add') - ->withConsecutive( - [$this->equalTo(1)], - [$this->equalTo(2)], - [$this->equalTo(0)], - [$this->equalTo(3)], - [$this->equalTo(4)], - [$this->equalTo(5)] - ); - - $stub->load($items); - } - - public function testRemove() - { - $className = AbstractItemList::class; - $items = [1, 2, 0, 3, 3, 4, 5]; - $itemsNew = [ - 0 => 1, - 1 => 2, - 2 => 0, - 3 => 3, - 5 => 4, - 6 => 5 - ]; - - $stub = $this->getMockForAbstractClass($className); - - $reflectedClass = new \ReflectionClass($className); - $property = $reflectedClass->getProperty('items'); - $property->setAccessible(true); - $property->setValue($stub, $items); - - $stub->remove(4); - - $this->assertEquals($itemsNew, $property->getValue($stub)); - } - - public function testHandleInvalidIndex() - { - $className = AbstractItemList::class; - - $stub = $this->getMockForAbstractClass($className); - - $reflectedClass = new \ReflectionClass($className); - $method = $reflectedClass->getMethod('handleInvalidIndex'); - $method->setAccessible(true); - - $this->expectException(\InvalidArgumentException::class); - $this->expectExceptionMessageRegExp('/Invalid index(.)+invalid_index/'); - - $method->invoke($stub, 'invalid_index'); - } - -} diff --git a/tests/unit/Lists/HashListTest.php b/tests/unit/Lists/HashListTest.php deleted file mode 100644 index f2e88ee..0000000 --- a/tests/unit/Lists/HashListTest.php +++ /dev/null @@ -1,207 +0,0 @@ -getMockBuilder($className) - ->setMethods(['load']) - ->disableOriginalConstructor() - ->getMock(); - - $mock->expects($this->once()) - ->method('load') - ->with($this->equalTo($items)); - - $reflectedClass = new \ReflectionClass($className); - $constructor = $reflectedClass->getConstructor(); - $constructor->invoke($mock, $items); - } - - public function testAdd() - { - $mock = $this->createMock(HashListItemImplementation::class); - - $mock->expects($this->once()) - ->method('getHash') - ->willReturn('key1'); - - $stub = $this->getMockBuilder(HashList::class) - ->setMethods(['load']) - ->disableOriginalConstructor() - ->getMock(); - - $stub->add($mock); - - $reflectedClass = new \ReflectionClass(HashList::class); - $property = $reflectedClass->getProperty('items'); - $property->setAccessible(true); - - $this->assertArrayHasKey('key1', $property->getValue($stub)); - - $mock2 = $this->createMock(HashListItemImplementation::class); - - $mock2->expects($this->once()) - ->method('getHash') - ->willReturn('key2'); - - $stub->add($mock2); - - $this->assertArrayHasKey('key2', $property->getValue($stub)); - } - - public function testaddWithExpection() - { - $mock = $this->getMockBuilder(HashList::class) - ->setMethods(['load']) - ->disableOriginalConstructor() - ->getMock(); - - $this->expectException(\InvalidArgumentException::class); - - $mock->add('123 yeah!'); - } - - public function testGetNext() - { - $className = HashList::class; - - $items = [ - 'test_1' => 'test 1', - 'test_2' => 'test 2', - 'test_3' => 'test 3', - ]; - $indexInvalid = 'test_4'; - - $stub = $this->getMockBuilder($className) - ->setMethods(['handleInvalidIndex']) - ->disableOriginalConstructor() - ->getMock(); - - $reflectedClass = new \ReflectionClass($className); - $property = $reflectedClass->getProperty('items'); - $property->setAccessible(true); - $property->setValue($stub, $items); - - $this->assertEquals('test 3', $stub->getNext('test_2')); - - $this->assertEquals(null, $stub->getNext('test_3')); - - $stub->expects($this->once()) - ->method('handleInvalidIndex') - ->with($indexInvalid); - - $stub->getNext('test_4'); - } - - public function testGetPrevious() - { - $className = HashList::class; - - $items = [ - 'test_1' => 'test 1', - 'test_2' => 'test 2', - 'test_3' => 'test 3', - ]; - $indexInvalid = 'test_4'; - - $stub = $this->getMockBuilder($className) - ->setMethods(['handleInvalidIndex']) - ->disableOriginalConstructor() - ->getMock(); - - $reflectedClass = new \ReflectionClass($className); - $property = $reflectedClass->getProperty('items'); - $property->setAccessible(true); - $property->setValue($stub, $items); - - $this->assertEquals('test 1', $stub->getPrevious('test_2')); - - $this->assertEquals(null, $stub->getPrevious('test_1')); - - $stub->expects($this->once()) - ->method('handleInvalidIndex') - ->with($indexInvalid); - - $stub->getPrevious($indexInvalid); - } - - public function testMerge() - { - $className = HashList::class; - $items1 = [ - 'item1' => 'val11', - 'item2' => 'val12', - 'item3' => 'val13', - 'item4' => 'val14', - 'item5' => 'val15', - 'item6' => 'val16', - 'item7' => 'val17', - ]; - $items2 = [ - 'item1' => 'val21', - 'item12' => 'val22', - 'item3' => 'val23', - 'item4' => 'val24', - 'item5' => 'val25', - 'item16' => 'val26', - 'item7' => 'val27', - ]; - $expected = [ - 'item1' => 'val11', - 'item12' => 'val22', - 'item3' => 'val13', - 'item4' => 'val14', - 'item5' => 'val15', - 'item16' => 'val26', - 'item7' => 'val17', - 'item2' => 'val12', - 'item6' => 'val16' - ]; - - $mock1 = $this->getMockBuilder($className) - ->setMethods(['getAll']) - ->disableOriginalConstructor() - ->getMock(); - $mock1->expects($this->once()) - ->method('getAll') - ->willReturn($items1); - - $mock2 = $this->getMockBuilder($className) - ->setMethods(['getAll']) - ->disableOriginalConstructor() - ->getMock(); - $mock2->expects($this->once()) - ->method('getAll') - ->willReturn($items2); - - $mock1->merge($mock2); - - $reflectedClass = new \ReflectionClass($className); - $property = $reflectedClass->getProperty('items'); - $property->setAccessible(true); - $actual = $property->getValue($mock1); - - $this->assertSame($expected, $actual); - } - -} diff --git a/tests/unit/Lists/ItemListTest.php b/tests/unit/Lists/ItemListTest.php deleted file mode 100644 index f226e63..0000000 --- a/tests/unit/Lists/ItemListTest.php +++ /dev/null @@ -1,88 +0,0 @@ -getMockBuilder($className) - ->setMethods(['load']) - ->disableOriginalConstructor() - ->getMock(); - - $mock->expects($this->once()) - ->method('load') - ->with($this->equalTo($items)); - - $reflectedClass = new \ReflectionClass($className); - $constructor = $reflectedClass->getConstructor(); - $constructor->invoke($mock, $items); - } - - public function testAdd() - { - // see: https://www.webtipblog.com/unit-testing-private-methods-and-properties-with-phpunit/ - $className = ItemList::class; - - $mock = $this->getMockBuilder($className) - ->setMethods(['load']) - ->getMock(); - - $reflectedClass = new \ReflectionClass($className); - $property = $reflectedClass->getProperty('items'); - $property->setAccessible(true); - - $this->assertEquals([], $property->getValue($mock)); - - $mock->add('test text'); - - $this->assertEquals(['test text'], $property->getValue($mock)); - - $mock->add(123); - - $this->assertEquals(['test text', 123], $property->getValue($mock)); - } - - public function testMerge() - { - $className = ItemList::class; - $items1 = [1, 2, 3]; - $items2 = [1, 2, 4, 3]; - $expected = [1, 2, 3, 1, 2, 4, 3]; - - $mock1 = $this->getMockBuilder($className) - ->setMethods(['getAll']) - ->disableOriginalConstructor() - ->getMock(); - $mock1->expects($this->once()) - ->method('getAll') - ->willReturn($items1); - - $mock2 = $this->getMockBuilder($className) - ->setMethods(['getAll']) - ->disableOriginalConstructor() - ->getMock(); - $mock2->expects($this->once()) - ->method('getAll') - ->willReturn($items2); - - $mock1->merge($mock2); - - $reflectedClass = new \ReflectionClass($className); - $property = $reflectedClass->getProperty('items'); - $property->setAccessible(true); - $actual = $property->getValue($mock1); - - $this->assertSame($expected, $actual); - } - -}