Skip to content

Commit 232f664

Browse files
committed
Implemented excersices
1 parent dac1b6c commit 232f664

File tree

4 files changed

+127
-0
lines changed

4 files changed

+127
-0
lines changed

src/People.php

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
3+
namespace App;
4+
5+
use Iterator;
6+
7+
/*
8+
* Ejercicio
9+
* Implementar el código faltante en la clase People para que el código de ejemplo funcione
10+
* TIP: Iterator es una interfaz
11+
* TIP: Métodos mágicos
12+
*/
13+
14+
/* POWER TIP Pueden extender la clase con otras clases o interfaces */
15+
16+
class People implements Iterator
17+
{
18+
private $name;
19+
private $age;
20+
private $salary;
21+
private $attrs;
22+
private $current = 3;
23+
24+
public function __construct($name, $age, $salary)
25+
{
26+
$this->name = $name;
27+
$this->age = $age;
28+
$this->salary = $salary;
29+
}
30+
31+
public function current()
32+
{
33+
return array_values($this->attrs[$this->current])[0];
34+
}
35+
36+
public function key()
37+
{
38+
return array_keys($this->attrs[$this->current])[0];
39+
}
40+
41+
public function valid()
42+
{
43+
return isset($this->attrs[$this->current]);
44+
}
45+
46+
public function __toString()
47+
{
48+
return json_encode([
49+
'private' => '',
50+
'public' => ''
51+
]);
52+
}
53+
54+
}

src/helpers.php

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?php
2+
3+
function ajax($url, $callback)
4+
{
5+
6+
}

tests/AjaxCallbackTest.php

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
use PHPUnit\Framework\TestCase;
4+
5+
class AjaxCallbackTest extends TestCase
6+
{
7+
public function test_if_ajax_callback_is_defined()
8+
{
9+
ajax('https://jsonip.com', function ($data) {
10+
$this->assertTrue($data['success']);
11+
$this->assertStringStartsWith('{"ip',$data['data']);
12+
});
13+
}
14+
}

tests/PeopleTest.php

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
3+
use App\People;
4+
use PHPUnit\Framework\TestCase;
5+
6+
class PeopleTest extends TestCase
7+
{
8+
private $people;
9+
private $randomWord;
10+
11+
protected function setUp()
12+
{
13+
parent::setUp();
14+
$this->people = new People('Oscar', 33, 10);
15+
$this->randomWord = uniqid('randomMethod', false);
16+
$this->people->{$this->randomWord} = 'lol'; //Agregando una nueva propiedad pública
17+
$this->people->fino = true; //Agregando una nueva propiedad pública
18+
}
19+
20+
public function test_if_private_variables_cannot_be_overriden()
21+
{
22+
$this->people->name = 'Mauricio'; //Intentando cambiar un atributo privado
23+
$this->people->age = 22; //Intentando cambiar un atributo privado
24+
$this->people->salary = 33; //Intentando cambiar un atributo privado
25+
$this->assertEquals('Oscar', $this->people->name);
26+
$this->assertEquals(33, $this->people->age);
27+
$this->assertEquals(10, $this->people->salary);
28+
}
29+
30+
public function test_if_custom_public_variables_are_stored()
31+
{
32+
$this->assertEquals('Oscar', $this->people->name);
33+
$this->assertEquals('lol', $this->people->{$this->randomWord});
34+
}
35+
36+
public function test_if_iterator_only_return_custom_public_variables()
37+
{
38+
foreach ($this->people as $key => $val) { //Acceder a las propiedades "públicas" únicamente al iterar el objeto
39+
$keys[] = $key;
40+
$values[] = $val;
41+
}
42+
$this->assertEquals($this->randomWord, $keys[0]);
43+
$this->assertEquals('fino', $keys[1]);
44+
$this->assertEquals('lol', $values[0]);
45+
$this->assertEquals(1, $values[1]);
46+
}
47+
48+
public function test_if_string_object_prints_all_signature()
49+
{
50+
$this->assertEquals('{"private":{"name":"Oscar","age":33,"salary":10},"public":[{"' . $this->randomWord . '":"lol"},{"fino":true}]}',
51+
(string)$this->people);
52+
}
53+
}

0 commit comments

Comments
 (0)