Passed
Branch master (350f1b)
by Jan
04:53
created

LabelGeneratorTest   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 24
dl 0
loc 63
rs 10
c 1
b 0
f 0
wmc 3

8 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 4 1
A supportsDataProvider() 0 6 1
A hp$0 ➔ testSupports() 0 12 1
testGenerateLabel() 0 16 ?
A hp$0 ➔ testGenerateLabel() 0 16 1
testSupports() 0 12 ?
testMmToPointsArray() 0 5 ?
A hp$0 ➔ testMmToPointsArray() 0 5 1
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
7
 *
8
 * Copyright (C) 2019 - 2020 Jan Böhmer (https://github.com/jbtronics)
9
 *
10
 * This program is free software: you can redistribute it and/or modify
11
 * it under the terms of the GNU Affero General Public License as published
12
 * by the Free Software Foundation, either version 3 of the License, or
13
 * (at your option) any later version.
14
 *
15
 * This program is distributed in the hope that it will be useful,
16
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
 * GNU Affero General Public License for more details.
19
 *
20
 * You should have received a copy of the GNU Affero General Public License
21
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
22
 */
23
24
namespace App\Tests\Services\LabelSystem;
25
26
use App\Entity\Base\AbstractDBElement;
27
use App\Entity\LabelSystem\LabelOptions;
28
use App\Entity\Parts\Part;
29
use App\Entity\Parts\PartLot;
30
use App\Entity\Parts\Storelocation;
31
use App\Services\LabelSystem\LabelGenerator;
32
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
33
34
class LabelGeneratorTest extends WebTestCase
35
{
36
    /**
37
     * @var LabelGenerator
38
     */
39
    protected $service;
40
41
    protected function setUp(): void
42
    {
43
        self::bootKernel();
44
        $this->service = self::$container->get(LabelGenerator::class);
45
    }
46
47
    public function supportsDataProvider(): array
48
    {
49
        return [
50
            ['part', Part::class],
51
            ['part_lot', PartLot::class],
52
            ['storelocation', Storelocation::class],
53
        ];
54
    }
55
56
    /**
57
     * @dataProvider supportsDataProvider
58
     */
59
    public function testSupports(string $type, string $class): void
60
    {
61
        $options = new LabelOptions();
62
        $options->setSupportedElement($type);
63
64
        //Ensure that the given class is supported
65
        $this->assertTrue($this->service->supports($options, new $class()));
66
67
        //Ensure that another class is not supported
68
        $not_supported = new class() extends AbstractDBElement {};
69
70
        $this->assertFalse($this->service->supports($options, $not_supported));
71
    }
72
73
    public function testMmToPointsArray(): void
74
    {
75
        $this->assertSame(
76
            [0.0, 0.0, 141.7325, 85.0395],
77
            $this->service->mmToPointsArray(50.0, 30.0)
78
        );
79
    }
80
81
    public function testGenerateLabel(): void
82
    {
83
        $part = new Part();
84
        $options = new LabelOptions();
85
        $options->setLines('Test');
86
        $options->setSupportedElement('part');
87
88
        //Test for a single passed element:
89
        $pdf = $this->service->generateLabel($options, $part);
90
        //Just a simple check if a PDF is returned
91
        $this->assertStringStartsWith('%PDF-', $pdf);
92
93
        //Test for an array of elements
94
        $pdf = $this->service->generateLabel($options, [$part, $part]);
95
        //Just a simple check if a PDF is returned
96
        $this->assertStringStartsWith('%PDF-', $pdf);
97
    }
98
}
99