UnscTest::setUp()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 14
rs 9.4285
cc 1
eloc 7
nc 1
nop 0
1
<?php
2
3
namespace ADiaz\AML\OpenList\parsers;
4
5
use ADiaz\AML\OpenList\models\Entity;
6
use ADiaz\AML\OpenList\utils\Exporter;
7
8
/**
9
 * This file is part of the OpenList Parser utility.
10
 *
11
 *
12
 * @category  PHP
13
 *
14
 * @author    Alberto Diaz <[email protected]>
15
 * @copyright 2016 Alberto Diaz <[email protected]>
16
 * @license   This source file is subject to the MIT license that is bundled
17
 *
18
 * @version   Release: @package_version@
19
 *
20
 * @link      http://tytem.com
21
 */
22
class UnscTest extends \PHPUnit_Framework_TestCase
23
{
24
    protected $parser;
25
    protected $output_file;
26
    protected $output_folder;
27
28
    protected function setUp()
29
    {
30
        // output file and folder
31
        $this->output_file = __DIR__.'/../fixtures/output/test_unsc_2016-03-26.json';
32
33
        // parser
34
        $this->parser = new Unsc();
35
        $this->parser->setSourcePath(__DIR__.'/../fixtures/lists/unsc_2016-03-25.xml');
36
        $this->parser->run();
37
38
        // export
39
        $exporter = new Exporter(__DIR__.'/../../../output/');
40
        $exporter->json($this->parser->getEntities(), 'test_unsc');
41
    }
42
43
    /**
44
     * Test run.
45
     */
46
    public function testCheckTotalEntities()
47
    {
48
        $entitiesProcessed = json_decode(file_get_contents($this->output_file), true);
49
50
        $this->assertEquals(count($this->parser->getEntities()), count($entitiesProcessed), 'The number of sanctions should be the same');
51
    }
52
53
    /**
54
     * Validate first_name and last name.
55
     */
56
    public function testValidateEmptyFields()
57
    {
58
        $this->validateEmptyFields(66560, ['address_list', 'place_of_birth_list', 'id_list']);
59
        $this->validateEmptyFields(97463, ['address_list', 'place_of_birth_list', 'id_list']);
60
    }
61
62
    /**
63
     * Validate first_name and last name.
64
     */
65
    public function testValidateName()
66
    {
67
        $this->validateFields(66560, ['first_name' => 'Gene VonRueden', 'last_name' => 'Macejkovic']);
68
        $this->validateFields(77391, ['first_name' => 'Dr. Francisca Ritchie I', 'last_name' => 'Quitzon']);
69
        $this->validateFields(44675, ['first_name' => 'Izabella Hills', 'last_name' => 'Kohler']);
70
        $this->validateFields(97463, ['first_name' => 'Lorena Powlowski', 'last_name' => 'Ward']);
71
    }
72
73
    /**
74
     * Validate position.
75
     */
76
    public function testValidatePosition()
77
    {
78
        $this->validateFields(66560, ['position' => 'General/IRGC officer']);
79
        $this->validateFields(77391, ['position' => 'Major General/Commander, IRGC (Pasdaran)']);
80
        $this->validateFields(32760, ['position' => 'Brigadier-General/Former Deputy Chief of Armed Forces General Staff for Logistics and Industrial Research/Head of State Anti-Smuggling Headquarters']);
81
    }
82
83
    /**
84
     * @param $id
85
     * @param array $fields it must be an associate value 'name_field'=>'value'
86
     */
87
    protected function validateFields($id, $fields)
88
    {
89
        $entitiesProcessed = json_decode(file_get_contents($this->output_file), true);
90
91
        $found = false;
92
93
        foreach ($entitiesProcessed as $entity) {
94
            if ($entity['external_id'] === (string) $id) {
95
                foreach ($fields as $key => $value) {
96
                    $this->assertEquals($entity[$key], $value, "A value of a entity does not match. Found: {$entity[$key]} expected: $value");
97
                }
98
                $found = true;
99
            }
100
        }
101
        $this->assertTrue($found, "An entity was not found, entity id: $id");
102
    }
103
104
    /**
105
     * Validate empty values of an entity.
106
     *
107
     * @param $id
108
     * @param array $fields it must be an associate value 'name_field'=>'value'
109
     */
110
    protected function validateEmptyFields($id, $fields)
111
    {
112
        $entitiesProcessed = json_decode(file_get_contents($this->output_file), true);
113
114
        $found = false;
115
116
        foreach ($entitiesProcessed as $entity) {
117
            if ($entity['external_id'] === (string) $id) {
118
                $fieldsUsed = array_keys($entity);
119
                $arrayIntersect = array_intersect($fieldsUsed, $fields);
120
121
                $this->assertTrue(count($arrayIntersect) === 0);
122
                $found = true;
123
            }
124
        }
125
        $this->assertTrue($found, "An entity was not found, entity id: $id");
126
    }
127
128
    /**
129
     * Validate Akas.
130
     */
131
    public function testValidateAkas()
132
    {
133
        $entitiesProcessed = json_decode(file_get_contents($this->output_file), true);
134
135
        $found = false;
136
137
        foreach ($entitiesProcessed as $entity) {
138
            if ($entity['external_id'] === '66560'
139
                && $entity['aka_list'][0]['first_name'] === 'Mohammad Bakr Zolqadr'
140
                && $entity['aka_list'][1]['first_name'] === 'Mohammad Bakr Zolkadr'
141
                && $entity['aka_list'][2]['first_name'] === 'Mohammad Baqer Zolqadir'
142
                && $entity['aka_list'][3]['first_name'] === 'Mohammad Baqer Zolqader'
143
            ) {
144
                $found = true;
145
                break;
146
            }
147
        }
148
        $this->assertTrue($found, 'An entity was not found');
149
    }
150
}
151